1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* cqrs-async-queue-interop (https://github.com/phpgears/cqrs-async-queue-interop). |
5
|
|
|
* Queue-interop async decorator for CQRS command bus. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/cqrs-async-queue-interop |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\CQRS\Async\QueueInterop; |
15
|
|
|
|
16
|
|
|
use Gears\CQRS\Async\AbstractCommandQueue; |
17
|
|
|
use Gears\CQRS\Async\Exception\CommandQueueException; |
18
|
|
|
use Gears\CQRS\Async\Serializer\CommandSerializer; |
19
|
|
|
use Gears\CQRS\Command; |
20
|
|
|
use Interop\Queue\PsrContext; |
21
|
|
|
use Interop\Queue\PsrDestination; |
22
|
|
|
use Interop\Queue\PsrMessage; |
23
|
|
|
use Interop\Queue\PsrProducer; |
24
|
|
|
|
25
|
|
|
class QueueInteropCommandQueue extends AbstractCommandQueue |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Queue context. |
29
|
|
|
* |
30
|
|
|
* @var PsrContext |
31
|
|
|
*/ |
32
|
|
|
private $context; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var PsrDestination |
36
|
|
|
*/ |
37
|
|
|
private $destination; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* EnqueueCommandBus constructor. |
41
|
|
|
* |
42
|
|
|
* @param CommandSerializer $serializer |
43
|
|
|
* @param PsrContext $context |
44
|
|
|
* @param PsrDestination $destination |
45
|
|
|
*/ |
46
|
|
|
public function __construct(CommandSerializer $serializer, PsrContext $context, PsrDestination $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(Command $command): void |
58
|
|
|
{ |
59
|
|
|
// @codeCoverageIgnoreStart |
60
|
|
|
try { |
61
|
|
|
$this->getMessageProducer()->send($this->destination, $this->getMessage($command)); |
62
|
|
|
} catch (\Exception $exception) { |
63
|
|
|
throw new CommandQueueException('Failure enqueueing command', 0, $exception); |
64
|
|
|
} |
65
|
|
|
// @codeCoverageIgnoreEnd |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get message from command. |
70
|
|
|
* |
71
|
|
|
* @param Command $command |
72
|
|
|
* |
73
|
|
|
* @return PsrMessage |
74
|
|
|
*/ |
75
|
|
|
protected function getMessage(Command $command): PsrMessage |
76
|
|
|
{ |
77
|
|
|
return $this->context->createMessage($this->getSerializedCommand($command)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get message producer. |
82
|
|
|
* |
83
|
|
|
* @return PsrProducer |
84
|
|
|
*/ |
85
|
|
|
protected function getMessageProducer(): PsrProducer |
86
|
|
|
{ |
87
|
|
|
return $this->context->createProducer(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|