1
|
|
|
<?php |
2
|
|
|
namespace PSB\Persistence\Doctrine2\Outbox; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use PSB\Core\Outbox\OutboxMessage; |
6
|
|
|
use PSB\Core\Outbox\OutboxStorageInterface; |
7
|
|
|
|
8
|
|
|
class Doctrine2OutboxStorage implements OutboxStorageInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var OutboxPersister |
12
|
|
|
*/ |
13
|
|
|
private $persister; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var OutboxMessageConverter |
17
|
|
|
*/ |
18
|
|
|
private $messageConverter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $endpointId; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param OutboxPersister $persister |
27
|
|
|
* @param OutboxMessageConverter $messageConverter |
28
|
|
|
* @param int $endpointId |
29
|
|
|
*/ |
30
|
8 |
|
public function __construct(OutboxPersister $persister, OutboxMessageConverter $messageConverter, $endpointId) |
31
|
|
|
{ |
32
|
8 |
|
$this->persister = $persister; |
33
|
8 |
|
$this->messageConverter = $messageConverter; |
34
|
8 |
|
$this->endpointId = $endpointId; |
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Fetches the given message from the storage. It returns null if no message is found. |
39
|
|
|
* |
40
|
|
|
* @param string $messageId |
41
|
|
|
* |
42
|
|
|
* @return OutboxMessage|null |
43
|
|
|
*/ |
44
|
2 |
|
public function get($messageId) |
45
|
|
|
{ |
46
|
2 |
|
$outboxRecord = $this->persister->get($this->endpointId, $messageId); |
47
|
2 |
|
if ($outboxRecord) { |
|
|
|
|
48
|
1 |
|
return $this->messageConverter->fromDatabaseArray($outboxRecord); |
49
|
|
|
} |
50
|
1 |
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Stores the message to enable deduplication and re-dispatching of transport operations. |
55
|
|
|
* Throws an exception if a message with the same ID already exists. |
56
|
|
|
* |
57
|
|
|
* @param OutboxMessage $message |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
1 |
|
public function store(OutboxMessage $message) |
62
|
|
|
{ |
63
|
1 |
|
$outboxRecord = $this->messageConverter->toDatabaseArray($message); |
64
|
1 |
|
$this->persister->store($this->endpointId, $outboxRecord); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $messageId |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
1 |
|
public function markAsDispatched($messageId) |
73
|
|
|
{ |
74
|
1 |
|
$this->persister->markAsDispatched($this->endpointId, $messageId); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Initiates the transaction |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
1 |
|
public function beginTransaction() |
83
|
|
|
{ |
84
|
1 |
|
$this->persister->beginTransaction(); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Commits the transaction |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
1 |
|
public function commit() |
93
|
|
|
{ |
94
|
1 |
|
$this->persister->commit(); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Rolls back the transaction |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
1 |
|
public function rollBack() |
103
|
|
|
{ |
104
|
1 |
|
$this->persister->rollBack(); |
105
|
1 |
|
} |
106
|
|
|
} |
107
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.