1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Johmanx10\Transaction; |
8
|
|
|
|
9
|
|
|
use Johmanx10\Transaction\Visitor\AcceptingTransactionFactoryInterface; |
10
|
|
|
use Johmanx10\Transaction\Visitor\OperationVisitorAwareInterface; |
11
|
|
|
use Johmanx10\Transaction\Visitor\OperationVisitorInterface; |
12
|
|
|
|
13
|
|
|
class OperationHandler implements |
14
|
|
|
OperationHandlerInterface, |
15
|
|
|
OperationVisitorAwareInterface |
16
|
|
|
{ |
17
|
|
|
/** @var OperationVisitorInterface[] */ |
18
|
|
|
private array $visitors = []; |
19
|
|
|
|
20
|
|
|
private AcceptingTransactionFactoryInterface $factory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* |
25
|
|
|
* @param AcceptingTransactionFactoryInterface|null $factory |
26
|
|
|
*/ |
27
|
1 |
|
public function __construct(AcceptingTransactionFactoryInterface $factory = null) |
28
|
|
|
{ |
29
|
1 |
|
$this->factory = $factory ?? new TransactionFactory(); |
30
|
1 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Handle the given operations. |
34
|
|
|
* |
35
|
|
|
* @param OperationInterface ...$operations |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function handle(OperationInterface ...$operations): void |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
|
|
->factory |
43
|
|
|
->createTransaction(...$operations) |
44
|
|
|
->commit(...array_values($this->visitors)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Attach operation visitors to the current container. |
49
|
|
|
* |
50
|
|
|
* @param OperationVisitorInterface ...$visitors |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function attachVisitor(OperationVisitorInterface ...$visitors): void |
55
|
|
|
{ |
56
|
|
|
foreach ($visitors as $visitor) { |
57
|
|
|
$this->visitors[spl_object_hash($visitor)] = $visitor; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Detach operation visitors to the current container. |
63
|
|
|
* |
64
|
|
|
* @param OperationVisitorInterface ...$visitors |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function detachVisitor(OperationVisitorInterface ...$visitors): void |
69
|
|
|
{ |
70
|
|
|
foreach ($visitors as $visitor) { |
71
|
|
|
unset($this->visitors[spl_object_hash($visitor)]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|