|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2012-2014 Janos Szurovecz |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
7
|
|
|
* the Software without restriction, including without limitation the rights to |
|
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
|
10
|
|
|
* so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
13
|
|
|
* copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
* SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace predaddy\util; |
|
25
|
|
|
|
|
26
|
|
|
use precore\util\Preconditions; |
|
27
|
|
|
use predaddy\commandhandling\CommandBus; |
|
28
|
|
|
use predaddy\commandhandling\DirectCommandBus; |
|
29
|
|
|
use predaddy\domain\EventPublisher; |
|
30
|
|
|
use predaddy\domain\Repository; |
|
31
|
|
|
use predaddy\eventhandling\EventBus; |
|
32
|
|
|
use predaddy\messagehandling\DispatchInterceptor; |
|
33
|
|
|
use predaddy\messagehandling\interceptors\BlockerInterceptor; |
|
34
|
|
|
use predaddy\messagehandling\interceptors\BlockerInterceptorManager; |
|
35
|
|
|
use predaddy\messagehandling\interceptors\ExceptionHandlerDelegate; |
|
36
|
|
|
use predaddy\messagehandling\interceptors\WrapInTransactionInterceptor; |
|
37
|
|
|
use RuntimeException; |
|
38
|
|
|
use trf4php\TransactionManager; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Class TransactionalBusesBuilder |
|
42
|
|
|
* |
|
43
|
|
|
* @package predaddy\util |
|
44
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
45
|
|
|
*/ |
|
46
|
|
|
final class TransactionalBusesBuilder |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* @var DispatchInterceptor[] |
|
50
|
|
|
*/ |
|
51
|
|
|
private $outsideTransactionCommandInterceptors = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var DispatchInterceptor[] |
|
55
|
|
|
*/ |
|
56
|
|
|
private $withinTransactionCommandInterceptors = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var DispatchInterceptor[] |
|
60
|
|
|
*/ |
|
61
|
|
|
private $outsideTransactionEventInterceptors = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var DispatchInterceptor[] |
|
65
|
|
|
*/ |
|
66
|
|
|
private $withinTransactionEventInterceptors = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var boolean |
|
70
|
|
|
*/ |
|
71
|
|
|
private $useDirectCommandBus = false; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var BlockerInterceptor |
|
75
|
|
|
*/ |
|
76
|
|
|
private $blockerInterceptor; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var BlockerInterceptorManager |
|
80
|
|
|
*/ |
|
81
|
|
|
private $blockerIntManager; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var WrapInTransactionInterceptor |
|
85
|
|
|
*/ |
|
86
|
|
|
private $txInterceptor; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var ExceptionHandlerDelegate |
|
90
|
|
|
*/ |
|
91
|
|
|
private $exceptionHandler; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var Repository |
|
95
|
|
|
*/ |
|
96
|
|
|
private $repository; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param TransactionManager $txManager |
|
100
|
|
|
*/ |
|
101
|
|
|
private function __construct(TransactionManager $txManager) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->blockerInterceptor = new BlockerInterceptor(); |
|
104
|
|
|
$this->blockerIntManager = $this->blockerInterceptor->manager(); |
|
105
|
|
|
$this->txInterceptor = new WrapInTransactionInterceptor($txManager); |
|
106
|
|
|
$this->exceptionHandler = new ExceptionHandlerDelegate([$this->txInterceptor, $this->blockerIntManager]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param TransactionManager $txManager |
|
111
|
|
|
* @return TransactionalBusesBuilder |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function create(TransactionManager $txManager) |
|
114
|
|
|
{ |
|
115
|
|
|
return new self($txManager); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param DispatchInterceptor[] $interceptors |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
|
|
public function interceptCommandsWithinTransaction(array $interceptors) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->withinTransactionCommandInterceptors = $interceptors; |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param DispatchInterceptor[] $interceptors |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
|
|
public function interceptCommandsOutsideTransaction(array $interceptors) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->outsideTransactionCommandInterceptors = $interceptors; |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param DispatchInterceptor[] $interceptors |
|
140
|
|
|
* @return $this |
|
141
|
|
|
*/ |
|
142
|
|
|
public function interceptEventsWithinTransaction(array $interceptors) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->withinTransactionEventInterceptors = $interceptors; |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param DispatchInterceptor[] $interceptors |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
|
|
public function interceptEventsOutsideTransaction(array $interceptors) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->outsideTransactionEventInterceptors = $interceptors; |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* This builder instance will create a {@link DirectCommandBus} |
|
160
|
|
|
* with the given {@link Repository}. |
|
161
|
|
|
* |
|
162
|
|
|
* @param Repository $repository |
|
163
|
|
|
* @return $this |
|
164
|
|
|
*/ |
|
165
|
|
|
public function withRepository(Repository $repository) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->repository = $repository; |
|
168
|
|
|
$this->useDirectCommandBus(); |
|
169
|
|
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param bool $flag If true, repository must be set |
|
174
|
|
|
* @return $this |
|
175
|
|
|
* @throws RuntimeException if flag is true and repository is not set |
|
176
|
|
|
*/ |
|
177
|
|
|
public function useDirectCommandBus($flag = true) |
|
178
|
|
|
{ |
|
179
|
|
|
Preconditions::checkState( |
|
180
|
|
|
!$flag || $this->repository !== null, |
|
181
|
|
|
'You must set a Repository to be able to create DirectCommandBus' |
|
182
|
|
|
); |
|
183
|
|
|
$this->useDirectCommandBus = $flag; |
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return TransactionalBuses |
|
189
|
|
|
*/ |
|
190
|
|
|
public function build() |
|
191
|
|
|
{ |
|
192
|
|
|
$commandBus = $this->createCommandBus(); |
|
193
|
|
|
$eventBus = $this->createEventBus(); |
|
194
|
|
|
EventPublisher::instance()->setEventBus($eventBus); |
|
195
|
|
|
return new TransactionalBuses($commandBus, $eventBus); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return EventBus |
|
200
|
|
|
*/ |
|
201
|
|
|
private function createEventBus() |
|
202
|
|
|
{ |
|
203
|
|
|
return EventBus::builder() |
|
204
|
|
|
->withInterceptors( |
|
205
|
|
|
array_merge( |
|
206
|
|
|
$this->withinTransactionEventInterceptors, |
|
207
|
|
|
[$this->blockerInterceptor], |
|
208
|
|
|
$this->outsideTransactionEventInterceptors |
|
209
|
|
|
) |
|
210
|
|
|
) |
|
211
|
|
|
->build(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return CommandBus|DirectCommandBus |
|
216
|
|
|
*/ |
|
217
|
|
|
private function createCommandBus() |
|
218
|
|
|
{ |
|
219
|
|
|
$commandInterceptorList = array_merge( |
|
|
|
|
|
|
220
|
|
|
$this->outsideTransactionCommandInterceptors, |
|
221
|
|
|
[$this->blockerIntManager, $this->txInterceptor], |
|
222
|
|
|
$this->withinTransactionCommandInterceptors |
|
223
|
|
|
); |
|
224
|
|
|
return $this->useDirectCommandBus |
|
225
|
|
|
? DirectCommandBus::builder($this->repository) |
|
226
|
|
|
->withInterceptors($commandInterceptorList) |
|
227
|
|
|
->withExceptionHandler($this->exceptionHandler) |
|
228
|
|
|
->build() |
|
229
|
|
|
: CommandBus::builder() |
|
230
|
|
|
->withInterceptors($commandInterceptorList) |
|
231
|
|
|
->withExceptionHandler($this->exceptionHandler) |
|
232
|
|
|
->build(); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|