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 IronMqProvider extends AbstractProvider |
||
38 | { |
||
39 | /** |
||
40 | * IronMQ Client |
||
41 | * |
||
42 | * @var IronMQ |
||
43 | */ |
||
44 | private $ironmq; |
||
45 | |||
46 | /** |
||
47 | * IronMQ Queue |
||
48 | * |
||
49 | * @var object |
||
50 | */ |
||
51 | private $queue; |
||
52 | |||
53 | /** |
||
54 | * @var Message[] |
||
55 | */ |
||
56 | private $reservedMessages = []; |
||
57 | |||
58 | 10 | View Code Duplication | public function __construct($name, array $options, $client, Cache $cache, Logger $logger) |
66 | |||
67 | 1 | public function getProvider() |
|
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | 4 | public function create() |
|
112 | |||
113 | /** |
||
114 | * {@inheritDoc} |
||
115 | */ |
||
116 | 1 | public function destroy() |
|
137 | |||
138 | /** |
||
139 | * {@inheritDoc} |
||
140 | * |
||
141 | * @return int |
||
142 | */ |
||
143 | 1 | public function publish(array $message, array $options = []) |
|
170 | |||
171 | /** |
||
172 | * {@inheritDoc} |
||
173 | */ |
||
174 | 1 | public function receive(array $options = []) |
|
175 | { |
||
176 | 1 | $options = $this->mergeOptions($options); |
|
177 | |||
178 | 1 | if (!$this->queueExists()) { |
|
179 | 1 | $this->create(); |
|
180 | } |
||
181 | |||
182 | 1 | $messages = $this->ironmq->reserveMessages( |
|
183 | 1 | $this->getNameWithPrefix(), |
|
184 | 1 | $options['messages_to_receive'], |
|
185 | 1 | $options['message_timeout'], |
|
186 | 1 | $options['receive_wait_time'] |
|
187 | ); |
||
188 | |||
189 | 1 | if (!is_array($messages)) { |
|
190 | $this->log(200, "No messages found in queue."); |
||
191 | |||
192 | return []; |
||
193 | } |
||
194 | |||
195 | // Convert to Message Class |
||
196 | 1 | foreach ($messages as &$message) { |
|
197 | 1 | $id = $message->id; |
|
198 | 1 | $body = json_decode($message->body, true); |
|
199 | $metadata = [ |
||
200 | 1 | 'reserved_count' => $message->reserved_count, |
|
201 | 1 | 'reservation_id' => $message->reservation_id |
|
202 | ]; |
||
203 | |||
204 | 1 | unset($body['_qpush_queue']); |
|
205 | |||
206 | 1 | $message = new Message($id, json_encode($body), $metadata); |
|
207 | |||
208 | 1 | $this->log(200, "Message has been received.", ['message_id' => $id]); |
|
209 | } |
||
210 | |||
211 | 1 | $this->reservedMessages = array_combine(array_values(array_map(function (Message $message) { |
|
212 | 1 | return $message->getId(); |
|
213 | 1 | }, $messages)), $messages); |
|
214 | |||
215 | 1 | return $messages; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * {@inheritDoc} |
||
220 | */ |
||
221 | 2 | public function delete($id) |
|
238 | |||
239 | /** |
||
240 | * Checks whether or not the Queue exists |
||
241 | * |
||
242 | * This method relies on in-memory cache and the Cache provider |
||
243 | * to reduce the need to needlessly call the create method on an existing |
||
244 | * Queue. |
||
245 | * @return bool |
||
246 | * @throws \Exception |
||
247 | */ |
||
248 | 4 | public function queueExists() |
|
249 | { |
||
250 | 4 | if (isset($this->queue)) { |
|
251 | 2 | return true; |
|
252 | } |
||
253 | |||
254 | 4 | $queueName = $this->getNameWithPrefix(); |
|
255 | 4 | if ($this->cache->contains($queueName)) { |
|
256 | $this->queue = json_decode($this->cache->fetch($queueName)); |
||
257 | |||
258 | return true; |
||
259 | } |
||
260 | try { |
||
261 | 4 | $this->queue = $this->ironmq->getQueue($queueName); |
|
262 | 4 | $this->cache->save($queueName, json_encode($this->queue)); |
|
263 | } catch (\Exception $e) { |
||
264 | View Code Duplication | if (false !== strpos($e->getMessage(), "Queue not found")) { |
|
265 | $this->log(400, "Queue did not exist"); |
||
266 | } else { |
||
267 | throw $e; |
||
268 | } |
||
269 | } |
||
270 | |||
271 | 4 | return false; |
|
272 | } |
||
273 | |||
274 | /** |
||
275 | * Polls the Queue on Notification from IronMQ |
||
276 | * |
||
277 | * Dispatches the `{queue}.message_received` event |
||
278 | * |
||
279 | * @param NotificationEvent $event The Notification Event |
||
280 | * @param string $eventName Name of the event |
||
281 | * @param EventDispatcherInterface $dispatcher |
||
282 | * @return void |
||
283 | */ |
||
284 | 1 | public function onNotification(NotificationEvent $event, $eventName, EventDispatcherInterface $dispatcher) |
|
285 | { |
||
286 | 1 | $message = new Message( |
|
287 | 1 | $event->getNotification()->getId(), |
|
288 | 1 | $event->getNotification()->getBody(), |
|
289 | 1 | $event->getNotification()->getMetadata()->toArray() |
|
290 | ); |
||
291 | |||
292 | 1 | $this->log( |
|
293 | 1 | 200, |
|
294 | 1 | "Message has been received from Push Notification.", |
|
295 | 1 | ['message_id' => $event->getNotification()->getId()] |
|
296 | ); |
||
297 | |||
298 | 1 | $messageEvent = new MessageEvent($this->name, $message); |
|
299 | |||
300 | 1 | $dispatcher->dispatch( |
|
301 | 1 | Events::Message($this->name), |
|
302 | 1 | $messageEvent |
|
303 | ); |
||
304 | 1 | } |
|
305 | |||
306 | /** |
||
307 | * Removes the message from queue after all other listeners have fired |
||
308 | * |
||
309 | * If an earlier listener has failed or stopped propagation, this method |
||
310 | * will not fire and the Queued Message should become visible in queue again. |
||
311 | * |
||
312 | * Stops Event Propagation after removing the Message |
||
313 | * |
||
314 | * @param MessageEvent $event The SQS Message Event |
||
315 | * @return void |
||
316 | */ |
||
317 | 1 | public function onMessageReceived(MessageEvent $event) |
|
328 | |||
329 | /** |
||
330 | * Get queue info |
||
331 | * |
||
332 | * This allows to get queue size. Allowing to know if processing is finished or not |
||
333 | * |
||
334 | * @return mixed |
||
335 | */ |
||
336 | 1 | public function queueInfo() |
|
347 | |||
348 | /** |
||
349 | * Publishes multiple message at once |
||
350 | * |
||
351 | * @param array $messages |
||
352 | * @param array $options |
||
353 | * |
||
354 | * @return array |
||
355 | */ |
||
356 | public function publishMessages(array $messages, array $options = []) |
||
388 | |||
389 | /** |
||
390 | * @param $id |
||
391 | * @return string|null |
||
392 | */ |
||
393 | 2 | private function getReservationId($id) |
|
406 | } |
||
407 |
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.