1 | <?php |
||
9 | final class TransactionalQueuePublisher implements QueuePublisher, Transactional |
||
10 | { |
||
11 | /** @var QueuePublisher */ |
||
12 | private $publisher; |
||
13 | |||
14 | /** @var array */ |
||
15 | private $messages; |
||
16 | |||
17 | /** @var bool */ |
||
18 | private $running; |
||
19 | |||
20 | /** |
||
21 | * Constructor. |
||
22 | * |
||
23 | * @param QueuePublisher $publisher |
||
24 | */ |
||
25 | 12 | public function __construct(QueuePublisher $publisher) |
|
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 9 | public function publish($data, $routingKey = '', array $headers = []) |
|
36 | { |
||
37 | 9 | if (!$this->running) { |
|
38 | 3 | throw new TransactionException('Cannot publish outside a transaction'); |
|
39 | } |
||
40 | |||
41 | 6 | $this->messages[] = [ |
|
42 | 6 | 'data' => $data, |
|
43 | 6 | 'routingKey' => $routingKey, |
|
44 | 'headers' => $headers |
||
45 | 6 | ]; |
|
46 | 6 | } |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 6 | public function beginTransaction() |
|
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 3 | public function commit() |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 3 | public function rollback() |
|
76 | } |
||
77 |