These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\NotificationBundle\Backend; |
||
15 | |||
16 | use Interop\Amqp\AmqpConsumer; |
||
17 | use Interop\Amqp\AmqpContext; |
||
18 | use Interop\Amqp\AmqpMessage; |
||
19 | use Interop\Amqp\AmqpQueue; |
||
20 | use Interop\Amqp\AmqpTopic; |
||
21 | use Interop\Amqp\Impl\AmqpBind; |
||
22 | use Sonata\NotificationBundle\Consumer\ConsumerEvent; |
||
23 | use Sonata\NotificationBundle\Exception\HandlingException; |
||
24 | use Sonata\NotificationBundle\Iterator\AMQPMessageIterator; |
||
25 | use Sonata\NotificationBundle\Model\Message; |
||
26 | use Sonata\NotificationBundle\Model\MessageInterface; |
||
27 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
28 | use ZendDiagnostics\Result\Failure; |
||
29 | use ZendDiagnostics\Result\Success; |
||
30 | |||
31 | /** |
||
32 | * Consumer side of the rabbitMQ backend. |
||
33 | */ |
||
34 | class AMQPBackend implements BackendInterface |
||
35 | { |
||
36 | /** |
||
37 | * @var AMQPBackendDispatcher |
||
38 | */ |
||
39 | private $dispatcher = null; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $exchange; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $queue; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $key; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $recover; |
||
60 | |||
61 | /** |
||
62 | * @var null|string |
||
63 | */ |
||
64 | private $deadLetterExchange; |
||
65 | |||
66 | /** |
||
67 | * @var null|string |
||
68 | */ |
||
69 | private $deadLetterRoutingKey; |
||
70 | |||
71 | /** |
||
72 | * @var null|int |
||
73 | */ |
||
74 | private $ttl; |
||
75 | |||
76 | /** |
||
77 | * @var null|int |
||
78 | */ |
||
79 | private $prefetchCount; |
||
80 | |||
81 | /** |
||
82 | * @var AmqpConsumer |
||
83 | */ |
||
84 | private $consumer; |
||
85 | |||
86 | /** |
||
87 | * @param string $exchange |
||
88 | * @param string $queue |
||
89 | * @param string $recover |
||
90 | * @param string $key |
||
91 | * @param string $deadLetterExchange |
||
92 | * @param string $deadLetterRoutingKey |
||
93 | * @param null|int $ttl |
||
94 | */ |
||
95 | public function __construct($exchange, $queue, $recover, $key, $deadLetterExchange = null, $deadLetterRoutingKey = null, $ttl = null, $prefetchCount = null) |
||
96 | { |
||
97 | $this->exchange = $exchange; |
||
98 | $this->queue = $queue; |
||
99 | $this->recover = $recover; |
||
100 | $this->key = $key; |
||
101 | $this->deadLetterExchange = $deadLetterExchange; |
||
102 | $this->deadLetterRoutingKey = $deadLetterRoutingKey; |
||
103 | $this->ttl = $ttl; |
||
104 | $this->prefetchCount = $prefetchCount; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param AMQPBackendDispatcher $dispatcher |
||
109 | */ |
||
110 | public function setDispatcher(AMQPBackendDispatcher $dispatcher): void |
||
111 | { |
||
112 | $this->dispatcher = $dispatcher; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function initialize(): void |
||
119 | { |
||
120 | $args = []; |
||
121 | if (null !== $this->deadLetterExchange) { |
||
122 | $args['x-dead-letter-exchange'] = $this->deadLetterExchange; |
||
123 | |||
124 | if (null !== $this->deadLetterRoutingKey) { |
||
125 | $args['x-dead-letter-routing-key'] = $this->deadLetterRoutingKey; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | if (null !== $this->ttl) { |
||
130 | $args['x-message-ttl'] = $this->ttl; |
||
131 | } |
||
132 | |||
133 | $queue = $this->getContext()->createQueue($this->queue); |
||
134 | $queue->addFlag(AmqpQueue::FLAG_DURABLE); |
||
135 | $queue->setArguments($args); |
||
136 | $this->getContext()->declareQueue($queue); |
||
0 ignored issues
–
show
|
|||
137 | |||
138 | $topic = $this->getContext()->createTopic($this->exchange); |
||
139 | $topic->setType(AmqpTopic::TYPE_DIRECT); |
||
140 | $topic->addFlag(AmqpTopic::FLAG_DURABLE); |
||
141 | $this->getContext()->declareTopic($topic); |
||
0 ignored issues
–
show
$topic of type object<Interop\Queue\PsrTopic> is not a sub-type of object<Interop\Amqp\AmqpTopic> . It seems like you assume a child interface of the interface Interop\Queue\PsrTopic to be always present.
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass. Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type. ![]() |
|||
142 | |||
143 | $this->getContext()->bind(new AmqpBind($queue, $topic, $this->key)); |
||
0 ignored issues
–
show
$queue is of type object<Interop\Queue\PsrQueue> , but the function expects a object<Interop\Amqp\AmqpDestination> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() $topic is of type object<Interop\Queue\PsrTopic> , but the function expects a object<Interop\Amqp\AmqpDestination> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
144 | |||
145 | if (null !== $this->deadLetterExchange && null === $this->deadLetterRoutingKey) { |
||
146 | $deadLetterTopic = $this->getContext()->createTopic($this->deadLetterExchange); |
||
147 | $deadLetterTopic->setType(AmqpTopic::TYPE_DIRECT); |
||
148 | $deadLetterTopic->addFlag(AmqpTopic::FLAG_DURABLE); |
||
149 | $this->getContext()->declareTopic($deadLetterTopic); |
||
0 ignored issues
–
show
$deadLetterTopic of type object<Interop\Queue\PsrTopic> is not a sub-type of object<Interop\Amqp\AmqpTopic> . It seems like you assume a child interface of the interface Interop\Queue\PsrTopic to be always present.
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass. Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type. ![]() |
|||
150 | |||
151 | $this->getContext()->bind(new AmqpBind($queue, $deadLetterTopic, $this->key)); |
||
0 ignored issues
–
show
$queue is of type object<Interop\Queue\PsrQueue> , but the function expects a object<Interop\Amqp\AmqpDestination> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() $deadLetterTopic is of type object<Interop\Queue\PsrTopic> , but the function expects a object<Interop\Amqp\AmqpDestination> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function publish(MessageInterface $message): void |
||
159 | { |
||
160 | $body = json_encode([ |
||
161 | 'type' => $message->getType(), |
||
162 | 'body' => $message->getBody(), |
||
163 | 'createdAt' => $message->getCreatedAt()->format('U'), |
||
164 | 'state' => $message->getState(), |
||
165 | ]); |
||
166 | |||
167 | $amqpMessage = $this->getContext()->createMessage($body); |
||
168 | $amqpMessage->setContentType('text/plain'); // application/json ? |
||
169 | $amqpMessage->setTimestamp($message->getCreatedAt()->format('U')); |
||
170 | $amqpMessage->setDeliveryMode(AmqpMessage::DELIVERY_MODE_PERSISTENT); |
||
171 | $amqpMessage->setRoutingKey($this->key); |
||
172 | |||
173 | $topic = $this->getContext()->createTopic($this->exchange); |
||
174 | |||
175 | $this->getContext()->createProducer()->send($topic, $amqpMessage); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | public function create($type, array $body) |
||
182 | { |
||
183 | $message = new Message(); |
||
184 | $message->setType($type); |
||
185 | $message->setBody($body); |
||
186 | $message->setState(MessageInterface::STATE_OPEN); |
||
187 | |||
188 | return $message; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function createAndPublish($type, array $body): void |
||
195 | { |
||
196 | $this->publish($this->create($type, $body)); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function getIterator() |
||
203 | { |
||
204 | $context = $this->getContext(); |
||
205 | |||
206 | if (null !== $this->prefetchCount) { |
||
207 | $context->setQos(null, $this->prefetchCount, false); |
||
208 | } |
||
209 | |||
210 | $this->consumer = $this->getContext()->createConsumer($this->getContext()->createQueue($this->queue)); |
||
211 | $this->consumer->setConsumerTag('sonata_notification_'.uniqid()); |
||
212 | |||
213 | return new AMQPMessageIterator($this->consumer); |
||
0 ignored issues
–
show
$this->consumer of type object<Interop\Queue\PsrConsumer> is not a sub-type of object<Interop\Amqp\AmqpConsumer> . It seems like you assume a child interface of the interface Interop\Queue\PsrConsumer to be always present.
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass. Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type. ![]() The return type of
return new \Sonata\Notif...rator($this->consumer); (Sonata\NotificationBundl...tor\AMQPMessageIterator ) is incompatible with the return type declared by the interface Sonata\NotificationBundl...dInterface::getIterator of type Sonata\NotificationBundl...essageIteratorInterface .
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design. Let’s take a look at an example: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function ![]() |
|||
214 | } |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function handle(MessageInterface $message, EventDispatcherInterface $dispatcher): void |
||
220 | { |
||
221 | $event = new ConsumerEvent($message); |
||
222 | |||
223 | /** @var AmqpMessage $amqpMessage */ |
||
224 | $amqpMessage = $message->getValue('interopMessage'); |
||
225 | |||
226 | try { |
||
227 | $dispatcher->dispatch($message->getType(), $event); |
||
228 | |||
229 | $this->consumer->acknowledge($amqpMessage); |
||
230 | |||
231 | $message->setCompletedAt(new \DateTime()); |
||
232 | $message->setState(MessageInterface::STATE_DONE); |
||
233 | } catch (HandlingException $e) { |
||
234 | $message->setCompletedAt(new \DateTime()); |
||
235 | $message->setState(MessageInterface::STATE_ERROR); |
||
236 | |||
237 | $this->consumer->acknowledge($amqpMessage); |
||
238 | |||
239 | throw new HandlingException('Error while handling a message', 0, $e); |
||
240 | } catch (\Exception $e) { |
||
241 | $message->setCompletedAt(new \DateTime()); |
||
242 | $message->setState(MessageInterface::STATE_ERROR); |
||
243 | |||
244 | $this->consumer->reject($amqpMessage, $this->recover); |
||
0 ignored issues
–
show
$this->recover is of type string , but the function expects a boolean .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
245 | |||
246 | throw new HandlingException('Error while handling a message', 0, $e); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | public function getStatus() |
||
254 | { |
||
255 | try { |
||
256 | $this->getContext(); |
||
257 | } catch (\Exception $e) { |
||
258 | return new Failure($e->getMessage()); |
||
259 | } |
||
260 | |||
261 | return new Success('Channel is running (RabbitMQ)'); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function cleanup(): void |
||
268 | { |
||
269 | throw new \RuntimeException('Not implemented'); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @return AmqpContext |
||
274 | */ |
||
275 | private function getContext() |
||
276 | { |
||
277 | if (null === $this->dispatcher) { |
||
278 | throw new \RuntimeException('Unable to retrieve AMQP context without dispatcher.'); |
||
279 | } |
||
280 | |||
281 | return $this->dispatcher->getContext(); |
||
282 | } |
||
283 | } |
||
284 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.