Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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 | 4 | public function __construct(EventDispatcherInterface $dispatcher) { |
|
51 | 4 | $this->dispatcher = $dispatcher; |
|
52 | 4 | } |
|
53 | |||
54 | /** |
||
55 | * Kernel Request Event Handler for QPush Notifications |
||
56 | * |
||
57 | * @param GetResponseEvent $event The Kernel Request's GetResponseEvent |
||
58 | */ |
||
59 | 4 | public function onKernelRequest(GetResponseEvent $event) { |
|
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 | 1 | private function handleIronMqNotifications(GetResponseEvent $event) { |
|
82 | 1 | $headers = $event->getRequest()->headers; |
|
83 | 1 | $messageId = $headers->get('iron-message-id'); |
|
84 | |||
85 | 1 | if (null === ($message = json_decode($event->getRequest()->getContent(), true))) { |
|
86 | throw new \InvalidArgumentException('Unable to decode JSON'); |
||
87 | } |
||
88 | |||
89 | 1 | $queue = $this->getIronMqQueueName($event, $message); |
|
90 | $metadata = [ |
||
91 | 1 | 'iron-subscriber-message-id' => $headers->get('iron-subscriber-message-id'), |
|
92 | 1 | 'iron-subscriber-message-url' => $headers->get('iron-subscriber-message-url') |
|
93 | ]; |
||
94 | |||
95 | 1 | unset($message['_qpush_queue']); |
|
96 | |||
97 | 1 | $notification = new Notification( |
|
98 | 1 | $messageId, |
|
99 | 1 | $message, |
|
100 | 1 | $metadata |
|
101 | ); |
||
102 | |||
103 | 1 | $this->dispatcher->dispatch( |
|
104 | 1 | Events::Notification($queue), |
|
105 | 1 | new NotificationEvent($queue, NotificationEvent::TYPE_MESSAGE, $notification) |
|
106 | ); |
||
107 | |||
108 | 1 | 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 | 2 | private function handleSnsNotifications(GetResponseEvent $event) { |
|
118 | 2 | $notification = json_decode((string) $event->getRequest()->getContent(), true); |
|
119 | |||
120 | 2 | $type = $event->getRequest()->headers->get('x-amz-sns-message-type'); |
|
121 | |||
122 | $metadata = [ |
||
123 | 2 | 'Type' => $notification['Type'], |
|
124 | 2 | 'TopicArn' => $notification['TopicArn'], |
|
125 | 2 | 'Timestamp' => $notification['Timestamp'], |
|
126 | ]; |
||
127 | |||
128 | 2 | if ($type === 'Notification') { |
|
129 | |||
130 | // We put the queue name in the Subject field |
||
131 | 1 | $queue = $notification['Subject']; |
|
132 | 1 | $metadata['Subject'] = $queue; |
|
133 | |||
134 | 1 | $notification = new Notification( |
|
135 | 1 | $notification['MessageId'], |
|
136 | 1 | $notification['Message'], |
|
137 | 1 | $metadata |
|
138 | ); |
||
139 | |||
140 | 1 | $this->dispatcher->dispatch( |
|
141 | 1 | Events::Notification($queue), |
|
142 | 1 | new NotificationEvent($queue, NotificationEvent::TYPE_MESSAGE, $notification) |
|
143 | ); |
||
144 | |||
145 | 1 | return "SNS Message Notification Received."; |
|
146 | } |
||
147 | |||
148 | // For subscription notifications, we need to parse the Queue from |
||
149 | // the Topic ARN |
||
150 | 1 | $arnParts = explode(':', $notification['TopicArn']); |
|
151 | 1 | $last = end($arnParts); |
|
152 | 1 | $queue = str_replace('qpush_', '', $last); |
|
153 | |||
154 | // Get the token for the Subscription Confirmation |
||
155 | 1 | $metadata['Token'] = $notification['Token']; |
|
156 | |||
157 | 1 | $notification = new Notification( |
|
158 | 1 | $notification['MessageId'], |
|
159 | 1 | $notification['Message'], |
|
160 | 1 | $metadata |
|
161 | ); |
||
162 | |||
163 | 1 | $this->dispatcher->dispatch( |
|
164 | 1 | Events::Notification($queue), |
|
165 | 1 | new NotificationEvent($queue, NotificationEvent::TYPE_SUBSCRIPTION, $notification) |
|
166 | ); |
||
167 | |||
168 | 1 | 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 | 1 | private function getIronMqQueueName(GetResponseEvent $event, array&$message) { |
|
195 | } |
||
196 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.