forked from sroze/messenger-enqueue-transport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageBusProcessor.php
64 lines (55 loc) · 1.9 KB
/
MessageBusProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Enqueue\MessengerAdapter;
use Interop\Queue\PsrContext;
use Interop\Queue\PsrMessage;
use Interop\Queue\PsrProcessor;
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Transport\Serialization\DecoderInterface;
use Enqueue\MessengerAdapter\Exception\RejectMessageException;
use Enqueue\MessengerAdapter\Exception\RequeueMessageException;
/**
* The processor could be used with any queue interop compatible consumer, for example Enqueue's QueueConsumer.
*
* @author Max Kotliar <kotlyar.maksim@gmail.com>
* @author Samuel Roze <samuel.roze@gmail.com>
*/
class MessageBusProcessor implements PsrProcessor
{
private $bus;
private $messageDecoder;
public function __construct(MessageBusInterface $bus, DecoderInterface $messageDecoder)
{
$this->bus = $bus;
$this->messageDecoder = $messageDecoder;
}
public function process(PsrMessage $message, PsrContext $context)
{
$busMessage = $this->messageDecoder->decode(array(
'body' => $message->getBody(),
'headers' => $message->getHeaders(),
'properties' => $message->getProperties(),
));
if (!$busMessage instanceof ReceivedMessage) {
$busMessage = new ReceivedMessage($message);
}
try {
$this->bus->dispatch($busMessage);
return self::ACK;
} catch (RejectMessageException $e) {
return self::REJECT;
} catch (RequeueMessageException $e) {
return self::REQUEUE;
} catch (\Throwable $e) {
return self::REJECT;
}
}
}