|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* event-async-queue-interop (https://github.com/phpgears/event-async-queue-interop). |
|
5
|
|
|
* Queue-interop async decorator for Event bus. |
|
6
|
|
|
* |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @link https://github.com/phpgears/event-async-queue-interop |
|
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Gears\Event\Async\QueueInterop; |
|
15
|
|
|
|
|
16
|
|
|
use Gears\Event\Async\AbstractEventQueue; |
|
17
|
|
|
use Gears\Event\Async\Exception\EventQueueException; |
|
18
|
|
|
use Gears\Event\Async\Serializer\EventSerializer; |
|
19
|
|
|
use Gears\Event\Event; |
|
20
|
|
|
use Interop\Queue\Context; |
|
21
|
|
|
use Interop\Queue\Destination; |
|
22
|
|
|
use Interop\Queue\Message; |
|
23
|
|
|
use Interop\Queue\Producer; |
|
24
|
|
|
|
|
25
|
|
|
class QueueInteropEventQueue extends AbstractEventQueue |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Queue context. |
|
29
|
|
|
* |
|
30
|
|
|
* @var Context |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $context; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Destination |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $destination; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* EnqueueEventBus constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param EventSerializer $serializer |
|
43
|
|
|
* @param Context $context |
|
44
|
|
|
* @param Destination $destination |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(EventSerializer $serializer, Context $context, Destination $destination) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct($serializer); |
|
49
|
|
|
|
|
50
|
|
|
$this->context = $context; |
|
51
|
|
|
$this->destination = $destination; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
final public function send(Event $event): void |
|
58
|
|
|
{ |
|
59
|
|
|
// @codeCoverageIgnoreStart |
|
60
|
|
|
try { |
|
61
|
|
|
$this->getMessageProducer()->send($this->destination, $this->getMessage($event)); |
|
62
|
|
|
} catch (\Exception $exception) { |
|
63
|
|
|
throw new EventQueueException('Failure enqueueing event', 0, $exception); |
|
64
|
|
|
} |
|
65
|
|
|
// @codeCoverageIgnoreEnd |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get message from event. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Event $event |
|
72
|
|
|
* |
|
73
|
|
|
* @return Message |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getMessage(Event $event): Message |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->context->createMessage($this->getSerializedEvent($event)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get message producer. |
|
82
|
|
|
* |
|
83
|
|
|
* @return Producer |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getMessageProducer(): Producer |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->context->createProducer(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|