Skip to content

Commit 2ad6faf

Browse files
committed
Get IronMq queue name from subscriber-url header if not found in message
1 parent 9ca75e9 commit 2ad6faf

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

src/EventListener/RequestListener.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace Uecode\Bundle\QPushBundle\EventListener;
2424

25+
use Psr\Log\LoggerInterface;
2526
use Symfony\Component\HttpKernel\HttpKernel;
2627
use Symfony\Component\HttpFoundation\Response;
2728
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
@@ -49,7 +50,7 @@ class RequestListener
4950
*/
5051
public function __construct(EventDispatcherInterface $dispatcher)
5152
{
52-
$this->dispatcher = $dispatcher;
53+
$this->dispatcher = $dispatcher;
5354
}
5455

5556
/**
@@ -85,21 +86,16 @@ private function handleIronMqNotifications(GetResponseEvent $event)
8586
$headers = $event->getRequest()->headers;
8687
$messageId = $headers->get('iron-message-id');
8788

88-
// We add the message in an array with Queue as the property name
89-
$message = json_decode($event->getRequest()->getContent(), true);
90-
91-
if (empty($message['_qpush_queue'])) {
92-
return;
89+
if (null === ($message = json_decode($event->getRequest()->getContent(), true))) {
90+
throw new \InvalidArgumentException('Unable to decode JSON');
9391
}
9492

95-
$queue = $message['_qpush_queue'];
96-
$metadata = [
93+
$queue = $this->getIronMqQueueName($event, $message);
94+
$metadata = [
9795
'iron-subscriber-message-id' => $headers->get('iron-subscriber-message-id'),
9896
'iron-subscriber-message-url' => $headers->get('iron-subscriber-message-url')
9997
];
10098

101-
unset($message['_qpush_queue']);
102-
10399
$notification = new Notification(
104100
$messageId,
105101
$message,
@@ -174,4 +170,30 @@ private function handleSnsNotifications(GetResponseEvent $event)
174170

175171
return "SNS Subscription Confirmation Received.";
176172
}
173+
174+
/**
175+
* Get the name of the IronMq queue.
176+
*
177+
* @param GetResponseEvent $event
178+
* @param array $message
179+
*
180+
* @return string
181+
*/
182+
private function getIronMqQueueName(GetResponseEvent $event, array &$message)
183+
{
184+
if (array_key_exists('_qpush_queue', $message)) {
185+
return $message['_qpush_queue'];
186+
} else if (null !== ($subscriberUrl = $event->getRequest()->headers->get('iron-subscriber-message-url'))) {
187+
if (preg_match('#/queues/([a-z0-9_-]+)/messages/#i', $subscriberUrl, $matches)) {
188+
$queue = $matches[1];
189+
if (substr($queue, 0, 6) == 'qpush_') {
190+
$queue = substr($queue, 6);
191+
}
192+
193+
return $queue;
194+
}
195+
}
196+
197+
throw new \RuntimeException('Unable to get queue name');
198+
}
177199
}

0 commit comments

Comments
 (0)