Total Complexity | 17 |
Total Lines | 150 |
Duplicated Lines | 15.33 % |
Changes | 0 |
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 |
||
15 | final class MessageHandler implements MessageHandlerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var MessageDeliveryServiceInterface |
||
19 | */ |
||
20 | private $messageDeliveryService; |
||
21 | |||
22 | /** |
||
23 | * @var PersistenceClientInterface |
||
24 | */ |
||
25 | private $persistenceClient; |
||
26 | |||
27 | /** |
||
28 | * @var LoggerInterface |
||
29 | */ |
||
30 | private $logger; |
||
31 | |||
32 | /** |
||
33 | * @var MessageInterface |
||
34 | */ |
||
35 | private $message; |
||
36 | |||
37 | public function __construct( |
||
38 | MessageDeliveryServiceInterface $messageDeliveryService, |
||
39 | PersistenceClientInterface $persistenceClient, |
||
40 | LoggerInterface $logger = null |
||
41 | ) { |
||
42 | $this->messageDeliveryService = $messageDeliveryService; |
||
43 | $this->persistenceClient = $persistenceClient; |
||
44 | $this->logger = $logger ?? new NullLogger(); |
||
45 | } |
||
46 | |||
47 | public function handle(MessageInterface $message): void |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public function isHandlingMessage(): bool |
||
70 | { |
||
71 | return $this->message !== null; |
||
72 | } |
||
73 | |||
74 | public function shutdown(): void |
||
75 | { |
||
76 | $error = error_get_last(); |
||
77 | |||
78 | if ($error !== null && $this->isHandlingMessage()) { |
||
79 | $this->onError(new ErrorException($error['message'], 0, E_ERROR, $error['file'], $error['line'])); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | public function onError(Throwable $error): void |
||
84 | { |
||
85 | // If we have a fatal error, we were not able to rollback the transaction, so we will need to do it here, |
||
86 | // otherwise we cannot queue the command below, as it will be in a transaction that will never be committed |
||
87 | $this->persistenceClient->rollbackTransaction(); |
||
88 | |||
89 | if (!is_subclass_of($this->message, MessageInterface::class)) { |
||
90 | // This happens when we have a serialization problem in the queue for whatever reason (e.g. a class |
||
91 | // was moved, or renamed). In that case it's not a Command, but an PHP_Incomplete_Class. |
||
92 | // If we'd call handleFailure() for an Incomplete_Class we'd get a fatal error, this is not something we |
||
93 | // want, because the worker will die. And although supervisor will restart it again, it's not good, |
||
94 | // as supervisor will only restart it for an x amount of tries, before giving up, and we end up with |
||
95 | // a queue that is stuck. |
||
96 | // So in this case we can't log the error with the current logic. It's on the todo to just do an update |
||
97 | // in the database directly here, e.g. 'update QueuedCommand set error = ... where id = .. ' but we are |
||
98 | // working on the project restructure with a hard deadline and want to prioritize that above this. |
||
99 | // At least the fix is there, just not the logging, which we'll do later. |
||
100 | $logMessage = sprintf( |
||
101 | 'Cannot deliver queued message because it is not a MessageInterface %s: %s', |
||
102 | $this->message->getId(), |
||
103 | $error->getMessage() |
||
104 | ); |
||
105 | |||
106 | $this->logger->error($logMessage); |
||
107 | $this->message->fail($error); |
||
108 | $this->persistenceClient->persistUndeliverableMessage($this->message, $logMessage); |
||
109 | } else { |
||
110 | $this->message->fail($error); |
||
111 | |||
112 | try { |
||
113 | $this->persistenceClient->persist($this->message); |
||
114 | } catch (Exception $exception) { |
||
115 | // It can happen that we have some problem with persisting most likely a uniqid constraint because of a |
||
116 | // race condition or if we processed a message twice. In that case persist it in the |
||
117 | // UnRequeueableMessage table so you can do monitoring on this and fix it. |
||
118 | $this->persistenceClient->persistUndeliverableMessage($this->message, $exception->getMessage()); |
||
119 | |||
120 | throw $exception; |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | private function process(): void |
||
126 | { |
||
127 | $this->logger->info('Delivering message: ' . $this->getLogMessage($this->message)); |
||
128 | |||
129 | $this->messageDeliveryService->deliver($this->message); |
||
130 | } |
||
131 | |||
132 | View Code Duplication | private function getLogMessage(MessageInterface $message): string |
|
|
|||
133 | { |
||
134 | return $message->getId() |
||
135 | . ', ' . $message->getDestination() |
||
136 | . ', ' . $message->getDeliverAt()->format(DateTime::ATOM) |
||
137 | . ', ' . $message->getCreatedAt()->format(DateTime::ATOM) |
||
138 | . ', ' . $message->getUpdatedAt()->format(DateTime::ATOM) |
||
139 | . ', ' . $message->getTries() |
||
140 | . ', ' . $message->getPriority() |
||
141 | . ', ' . $this->getPayloadDescription($message); |
||
142 | } |
||
143 | |||
144 | View Code Duplication | private function getPayloadDescription(MessageInterface $message): string |
|
1 ignored issue
–
show
|
|||
145 | { |
||
146 | if (is_object($message->getPayload())) { |
||
147 | return get_class($message->getPayload()); |
||
148 | } |
||
149 | |||
150 | if (is_array($message->getPayload())) { |
||
151 | return 'array'; |
||
152 | } |
||
153 | |||
154 | return (string) $message->getPayload(); |
||
155 | } |
||
156 | |||
157 | private function initiateHandling(MessageInterface $message): void |
||
158 | { |
||
159 | $this->message = $message; |
||
160 | } |
||
161 | |||
162 | private function terminateHandling(): void |
||
165 | } |
||
166 | } |
||
167 |
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.