|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MessageApp\Message\Sender; |
|
4
|
|
|
|
|
5
|
|
|
use MessageApp\Message; |
|
6
|
|
|
use RemiSan\TransactionManager\Exception\BeginException; |
|
7
|
|
|
use RemiSan\TransactionManager\Exception\CommitException; |
|
8
|
|
|
use RemiSan\TransactionManager\Exception\NoRunningTransactionException; |
|
9
|
|
|
use RemiSan\TransactionManager\Exception\RollbackException; |
|
10
|
|
|
use RemiSan\TransactionManager\Transactional; |
|
11
|
|
|
|
|
12
|
|
|
class TransactionalMessageSender implements MessageSender, Transactional |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var MessageSender |
|
16
|
|
|
*/ |
|
17
|
|
|
private $messageSender; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Message[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private $messages; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var boolean |
|
26
|
|
|
*/ |
|
27
|
|
|
private $transactionRunning; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param MessageSender $messageSender |
|
31
|
|
|
*/ |
|
32
|
21 |
|
public function __construct(MessageSender $messageSender) |
|
33
|
|
|
{ |
|
34
|
21 |
|
$this->messageSender = $messageSender; |
|
35
|
21 |
|
$this->messages = []; |
|
36
|
21 |
|
$this->transactionRunning = false; |
|
37
|
21 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Send a message |
|
41
|
|
|
* |
|
42
|
|
|
* @param Message $message |
|
43
|
|
|
* @param object $context |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public function send(Message $message, $context) |
|
47
|
|
|
{ |
|
48
|
6 |
|
$this->messages[] = [ $message, $context ]; |
|
49
|
6 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Open transaction. |
|
53
|
|
|
* |
|
54
|
|
|
* @throws BeginException |
|
55
|
|
|
*/ |
|
56
|
15 |
|
public function beginTransaction() |
|
57
|
|
|
{ |
|
58
|
15 |
|
if ($this->transactionRunning) { |
|
59
|
3 |
|
throw new BeginException('Transaction already running'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
15 |
|
$this->messages = []; |
|
63
|
15 |
|
$this->transactionRunning = true; |
|
64
|
15 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Commit transaction. |
|
68
|
|
|
* |
|
69
|
|
|
* @throws CommitException |
|
70
|
|
|
* @throws NoRunningTransactionException |
|
71
|
|
|
*/ |
|
72
|
9 |
|
public function commit() |
|
73
|
|
|
{ |
|
74
|
9 |
|
if (!$this->transactionRunning) { |
|
75
|
3 |
|
throw new NoRunningTransactionException(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
6 |
|
foreach ($this->messages as $message) { |
|
80
|
6 |
|
$this->messageSender->send($message[0], $message[1]); |
|
81
|
2 |
|
} |
|
82
|
5 |
|
} catch (\Exception $e) { |
|
83
|
3 |
|
throw new CommitException('Error during commit', 0, $e); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
$this->messages = []; |
|
87
|
3 |
|
$this->transactionRunning = false; |
|
88
|
3 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Rollback transaction. |
|
92
|
|
|
* |
|
93
|
|
|
* @throws RollbackException |
|
94
|
|
|
* @throws NoRunningTransactionException |
|
95
|
|
|
*/ |
|
96
|
6 |
|
public function rollback() |
|
97
|
|
|
{ |
|
98
|
6 |
|
if (!$this->transactionRunning) { |
|
99
|
3 |
|
throw new NoRunningTransactionException(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
$this->messages = []; |
|
103
|
3 |
|
$this->transactionRunning = false; |
|
104
|
3 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|