|
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 $visitors = []; |
|
19
|
|
|
|
|
20
|
|
|
/** @var AcceptingTransactionFactoryInterface */ |
|
21
|
|
|
private $factory; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param AcceptingTransactionFactoryInterface|null $factory |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function __construct(AcceptingTransactionFactoryInterface $factory = null) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->factory = $factory ?? new TransactionFactory(); |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Handle the given operations. |
|
35
|
|
|
* |
|
36
|
|
|
* @param OperationInterface ...$operations |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public function handle(OperationInterface ...$operations): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this |
|
43
|
|
|
->factory |
|
44
|
|
|
->createTransaction(...$operations) |
|
45
|
|
|
->commit(...array_values($this->visitors)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Attach operation visitors to the current container. |
|
50
|
|
|
* |
|
51
|
|
|
* @param OperationVisitorInterface ...$visitors |
|
52
|
|
|
* |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function attachVisitor(OperationVisitorInterface ...$visitors): void |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($visitors as $visitor) { |
|
58
|
|
|
$this->visitors[spl_object_hash($visitor)] = $visitor; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Detach operation visitors to the current container. |
|
64
|
|
|
* |
|
65
|
|
|
* @param OperationVisitorInterface ...$visitors |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function detachVisitor(OperationVisitorInterface ...$visitors): void |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($visitors as $visitor) { |
|
72
|
|
|
unset($this->visitors[spl_object_hash($visitor)]); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|