|
22 | 22 |
|
23 | 23 | namespace Uecode\Bundle\QPushBundle\EventListener; |
24 | 24 |
|
25 | | -use Psr\Log\LoggerInterface; |
26 | | -use Symfony\Component\HttpKernel\HttpKernel; |
| 25 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
27 | 26 | use Symfony\Component\HttpFoundation\Response; |
28 | 27 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
29 | | -use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
30 | | -use Uecode\Bundle\QPushBundle\Message\Notification; |
| 28 | +use Symfony\Component\HttpKernel\HttpKernel; |
31 | 29 | use Uecode\Bundle\QPushBundle\Event\Events; |
32 | 30 | use Uecode\Bundle\QPushBundle\Event\NotificationEvent; |
| 31 | +use Uecode\Bundle\QPushBundle\Message\Notification; |
33 | 32 |
|
34 | 33 | /** |
35 | 34 | * @author Keith Kirk <kkirk@undergroundelephant.com> |
36 | 35 | */ |
37 | | -class RequestListener |
38 | | -{ |
39 | | - /** |
40 | | - * Symfony Event Dispatcher |
41 | | - * |
42 | | - * @var EventDispatcherInterface |
43 | | - */ |
44 | | - private $dispatcher; |
45 | | - |
46 | | - /** |
47 | | - * Constructor. |
48 | | - * |
49 | | - * @param EventDispatcherInterface $dispatcher A Symfony Event Dispatcher |
50 | | - */ |
51 | | - public function __construct(EventDispatcherInterface $dispatcher) |
52 | | - { |
53 | | - $this->dispatcher = $dispatcher; |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * Kernel Request Event Handler for QPush Notifications |
58 | | - * |
59 | | - * @param GetResponseEvent $event The Kernel Request's GetResponseEvent |
60 | | - */ |
61 | | - public function onKernelRequest(GetResponseEvent $event) |
62 | | - { |
63 | | - if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) { |
64 | | - return; |
65 | | - } |
66 | | - |
67 | | - if ($event->getRequest()->headers->has('x-amz-sns-message-type')) { |
68 | | - $result = $this->handleSnsNotifications($event); |
69 | | - $event->setResponse(new Response($result, 200)); |
70 | | - } |
71 | | - |
72 | | - if ($event->getRequest()->headers->has('iron-message-id')) { |
73 | | - $result = $this->handleIronMqNotifications($event); |
74 | | - $event->setResponse(new Response($result, 200)); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - /** |
79 | | - * Handles Messages sent from a IronMQ Push Queue |
80 | | - * |
81 | | - * @param GetResponseEvent $event The Kernel Request's GetResponseEvent |
82 | | - * @return string|void |
83 | | - */ |
84 | | - private function handleIronMqNotifications(GetResponseEvent $event) |
85 | | - { |
86 | | - $headers = $event->getRequest()->headers; |
87 | | - $messageId = $headers->get('iron-message-id'); |
88 | | - |
89 | | - if (null === ($message = json_decode($event->getRequest()->getContent(), true))) { |
90 | | - throw new \InvalidArgumentException('Unable to decode JSON'); |
91 | | - } |
92 | | - |
93 | | - $queue = $this->getIronMqQueueName($event, $message); |
94 | | - $metadata = [ |
95 | | - 'iron-subscriber-message-id' => $headers->get('iron-subscriber-message-id'), |
96 | | - 'iron-subscriber-message-url' => $headers->get('iron-subscriber-message-url') |
97 | | - ]; |
98 | | - |
99 | | - $notification = new Notification( |
100 | | - $messageId, |
101 | | - $message, |
102 | | - $metadata |
103 | | - ); |
104 | | - |
105 | | - $this->dispatcher->dispatch( |
106 | | - Events::Notification($queue), |
107 | | - new NotificationEvent($queue, NotificationEvent::TYPE_MESSAGE, $notification) |
108 | | - ); |
109 | | - |
110 | | - return "IronMQ Notification Received."; |
111 | | - } |
112 | | - |
113 | | - /** |
114 | | - * Handles Notifications sent from AWS SNS |
115 | | - * |
116 | | - * @param GetResponseEvent $event The Kernel Request's GetResponseEvent |
117 | | - * @return string |
118 | | - */ |
119 | | - private function handleSnsNotifications(GetResponseEvent $event) |
120 | | - { |
121 | | - $notification = json_decode((string)$event->getRequest()->getContent(), true); |
122 | | - |
123 | | - $type = $event->getRequest()->headers->get('x-amz-sns-message-type'); |
124 | | - |
125 | | - $metadata = [ |
126 | | - 'Type' => $notification['Type'], |
127 | | - 'TopicArn' => $notification['TopicArn'], |
128 | | - 'Timestamp' => $notification['Timestamp'], |
129 | | - ]; |
130 | | - |
131 | | - if ($type === 'Notification') { |
132 | | - |
133 | | - // We put the queue name in the Subject field |
134 | | - $queue = $notification['Subject']; |
135 | | - $metadata['Subject'] = $queue; |
136 | | - |
137 | | - $notification = new Notification( |
138 | | - $notification['MessageId'], |
139 | | - $notification['Message'], |
140 | | - $metadata |
141 | | - ); |
142 | | - |
143 | | - $this->dispatcher->dispatch( |
144 | | - Events::Notification($queue), |
145 | | - new NotificationEvent($queue, NotificationEvent::TYPE_MESSAGE, $notification) |
146 | | - ); |
147 | | - |
148 | | - return "SNS Message Notification Received."; |
149 | | - } |
150 | | - |
151 | | - // For subscription notifications, we need to parse the Queue from |
152 | | - // the Topic ARN |
153 | | - $arnParts = explode(':', $notification['TopicArn']); |
154 | | - $last = end($arnParts); |
155 | | - $queue = str_replace('qpush_', '', $last); |
156 | | - |
157 | | - // Get the token for the Subscription Confirmation |
158 | | - $metadata['Token'] = $notification['Token']; |
159 | | - |
160 | | - $notification = new Notification( |
161 | | - $notification['MessageId'], |
162 | | - $notification['Message'], |
163 | | - $metadata |
164 | | - ); |
165 | | - |
166 | | - $this->dispatcher->dispatch( |
167 | | - Events::Notification($queue), |
168 | | - new NotificationEvent($queue, NotificationEvent::TYPE_SUBSCRIPTION, $notification) |
169 | | - ); |
170 | | - |
171 | | - return "SNS Subscription Confirmation Received."; |
172 | | - } |
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 | | - } |
| 36 | + |
| 37 | +class RequestListener { |
| 38 | + /** |
| 39 | + * Symfony Event Dispatcher |
| 40 | + * |
| 41 | + * @var EventDispatcherInterface |
| 42 | + */ |
| 43 | + private $dispatcher; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructor. |
| 47 | + * |
| 48 | + * @param EventDispatcherInterface $dispatcher A Symfony Event Dispatcher |
| 49 | + */ |
| 50 | + public function __construct(EventDispatcherInterface $dispatcher) { |
| 51 | + $this->dispatcher = $dispatcher; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Kernel Request Event Handler for QPush Notifications |
| 56 | + * |
| 57 | + * @param GetResponseEvent $event The Kernel Request's GetResponseEvent |
| 58 | + */ |
| 59 | + public function onKernelRequest(GetResponseEvent $event) { |
| 60 | + if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if ($event->getRequest()->headers->has('x-amz-sns-message-type')) { |
| 65 | + $result = $this->handleSnsNotifications($event); |
| 66 | + $event->setResponse(new Response($result, 200)); |
| 67 | + } |
| 68 | + |
| 69 | + if ($event->getRequest()->headers->has('iron-message-id')) { |
| 70 | + $result = $this->handleIronMqNotifications($event); |
| 71 | + $event->setResponse(new Response($result, 200)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Handles Messages sent from a IronMQ Push Queue |
| 77 | + * |
| 78 | + * @param GetResponseEvent $event The Kernel Request's GetResponseEvent |
| 79 | + * @return string|void |
| 80 | + */ |
| 81 | + private function handleIronMqNotifications(GetResponseEvent $event) { |
| 82 | + $headers = $event->getRequest()->headers; |
| 83 | + $messageId = $headers->get('iron-message-id'); |
| 84 | + |
| 85 | + if (null === ($message = json_decode($event->getRequest()->getContent(), true))) { |
| 86 | + throw new \InvalidArgumentException('Unable to decode JSON'); |
| 87 | + } |
| 88 | + |
| 89 | + $queue = $this->getIronMqQueueName($event, $message); |
| 90 | + $metadata = [ |
| 91 | + 'iron-subscriber-message-id' => $headers->get('iron-subscriber-message-id'), |
| 92 | + 'iron-subscriber-message-url' => $headers->get('iron-subscriber-message-url') |
| 93 | + ]; |
| 94 | + |
| 95 | + unset($message['_qpush_queue']); |
| 96 | + |
| 97 | + $notification = new Notification( |
| 98 | + $messageId, |
| 99 | + $message, |
| 100 | + $metadata |
| 101 | + ); |
| 102 | + |
| 103 | + $this->dispatcher->dispatch( |
| 104 | + Events::Notification($queue), |
| 105 | + new NotificationEvent($queue, NotificationEvent::TYPE_MESSAGE, $notification) |
| 106 | + ); |
| 107 | + |
| 108 | + return "IronMQ Notification Received."; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Handles Notifications sent from AWS SNS |
| 113 | + * |
| 114 | + * @param GetResponseEvent $event The Kernel Request's GetResponseEvent |
| 115 | + * @return string |
| 116 | + */ |
| 117 | + private function handleSnsNotifications(GetResponseEvent $event) { |
| 118 | + $notification = json_decode((string) $event->getRequest()->getContent(), true); |
| 119 | + |
| 120 | + $type = $event->getRequest()->headers->get('x-amz-sns-message-type'); |
| 121 | + |
| 122 | + $metadata = [ |
| 123 | + 'Type' => $notification['Type'], |
| 124 | + 'TopicArn' => $notification['TopicArn'], |
| 125 | + 'Timestamp' => $notification['Timestamp'], |
| 126 | + ]; |
| 127 | + |
| 128 | + if ($type === 'Notification') { |
| 129 | + |
| 130 | + // We put the queue name in the Subject field |
| 131 | + $queue = $notification['Subject']; |
| 132 | + $metadata['Subject'] = $queue; |
| 133 | + |
| 134 | + $notification = new Notification( |
| 135 | + $notification['MessageId'], |
| 136 | + $notification['Message'], |
| 137 | + $metadata |
| 138 | + ); |
| 139 | + |
| 140 | + $this->dispatcher->dispatch( |
| 141 | + Events::Notification($queue), |
| 142 | + new NotificationEvent($queue, NotificationEvent::TYPE_MESSAGE, $notification) |
| 143 | + ); |
| 144 | + |
| 145 | + return "SNS Message Notification Received."; |
| 146 | + } |
| 147 | + |
| 148 | + // For subscription notifications, we need to parse the Queue from |
| 149 | + // the Topic ARN |
| 150 | + $arnParts = explode(':', $notification['TopicArn']); |
| 151 | + $last = end($arnParts); |
| 152 | + $queue = str_replace('qpush_', '', $last); |
| 153 | + |
| 154 | + // Get the token for the Subscription Confirmation |
| 155 | + $metadata['Token'] = $notification['Token']; |
| 156 | + |
| 157 | + $notification = new Notification( |
| 158 | + $notification['MessageId'], |
| 159 | + $notification['Message'], |
| 160 | + $metadata |
| 161 | + ); |
| 162 | + |
| 163 | + $this->dispatcher->dispatch( |
| 164 | + Events::Notification($queue), |
| 165 | + new NotificationEvent($queue, NotificationEvent::TYPE_SUBSCRIPTION, $notification) |
| 166 | + ); |
| 167 | + |
| 168 | + return "SNS Subscription Confirmation Received."; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Get the name of the IronMq queue. |
| 173 | + * |
| 174 | + * @param GetResponseEvent $event |
| 175 | + * @param array $message |
| 176 | + * |
| 177 | + * @return string |
| 178 | + */ |
| 179 | + private function getIronMqQueueName(GetResponseEvent $event, array&$message) { |
| 180 | + if (array_key_exists('_qpush_queue', $message)) { |
| 181 | + return $message['_qpush_queue']; |
| 182 | + } else if (null !== ($subscriberUrl = $event->getRequest()->headers->get('iron-subscriber-message-url'))) { |
| 183 | + if (preg_match('#/queues/([a-z0-9_-]+)/messages/#i', $subscriberUrl, $matches)) { |
| 184 | + $queue = $matches[1]; |
| 185 | + if (substr($queue, 0, 6) == 'qpush_') { |
| 186 | + $queue = substr($queue, 6); |
| 187 | + } |
| 188 | + |
| 189 | + return $queue; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + throw new \RuntimeException('Unable to get queue name'); |
| 194 | + } |
199 | 195 | } |
0 commit comments