|
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\Exception\OperationExceptionFactory; |
|
10
|
|
|
use Johmanx10\Transaction\Exception\OperationExceptionFactoryInterface; |
|
11
|
|
|
use Johmanx10\Transaction\Exception\OperationExceptionInterface; |
|
12
|
|
|
use Johmanx10\Transaction\Visitor\OperationVisitorAwareInterface; |
|
13
|
|
|
use Johmanx10\Transaction\Visitor\OperationVisitorInterface; |
|
14
|
|
|
use Johmanx10\Transaction\Visitor\TransactionFactory; |
|
15
|
|
|
use Johmanx10\Transaction\Visitor\TransactionFactoryInterface; |
|
16
|
|
|
use Throwable; |
|
17
|
|
|
|
|
18
|
|
|
class OperationHandler implements |
|
19
|
|
|
OperationHandlerInterface, |
|
20
|
|
|
OperationVisitorAwareInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var OperationVisitorInterface[] */ |
|
23
|
|
|
private $visitors = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var TransactionFactoryInterface */ |
|
26
|
|
|
private $transactionFactory; |
|
27
|
|
|
|
|
28
|
|
|
/** @var OperationExceptionFactoryInterface */ |
|
29
|
|
|
private $exceptionFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param TransactionFactoryInterface|null $transactionFactory |
|
35
|
|
|
* @param OperationExceptionFactoryInterface|null $exceptionFactory |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function __construct( |
|
38
|
|
|
TransactionFactoryInterface $transactionFactory = null, |
|
39
|
|
|
OperationExceptionFactoryInterface $exceptionFactory = null |
|
40
|
|
|
) { |
|
41
|
1 |
|
$this->transactionFactory = $transactionFactory ?? new TransactionFactory(); |
|
42
|
1 |
|
$this->exceptionFactory = $exceptionFactory ?? new OperationExceptionFactory(); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Handle the given operations. |
|
47
|
|
|
* |
|
48
|
|
|
* @param OperationInterface ...$operations |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
* |
|
52
|
|
|
* @throws OperationExceptionInterface When an operation or the transaction |
|
53
|
|
|
* fails. |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function handle(OperationInterface ...$operations): void |
|
56
|
|
|
{ |
|
57
|
|
|
$transaction = $this |
|
58
|
2 |
|
->transactionFactory |
|
59
|
2 |
|
->createTransaction(...$operations); |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
2 |
|
$transaction->commit(...array_values($this->visitors)); |
|
63
|
1 |
|
} catch (Throwable $exception) { |
|
64
|
1 |
|
throw $this->exceptionFactory->createFromThrowable($exception); |
|
65
|
|
|
} |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Attach operation visitors to the current container. |
|
70
|
|
|
* |
|
71
|
|
|
* @param OperationVisitorInterface ...$visitors |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function attachVisitor(OperationVisitorInterface ...$visitors): void |
|
76
|
|
|
{ |
|
77
|
1 |
|
foreach ($visitors as $visitor) { |
|
78
|
1 |
|
$this->visitors[spl_object_hash($visitor)] = $visitor; |
|
79
|
|
|
} |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Detach operation visitors to the current container. |
|
84
|
|
|
* |
|
85
|
|
|
* @param OperationVisitorInterface ...$visitors |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function detachVisitor(OperationVisitorInterface ...$visitors): void |
|
90
|
|
|
{ |
|
91
|
1 |
|
foreach ($visitors as $visitor) { |
|
92
|
1 |
|
unset($this->visitors[spl_object_hash($visitor)]); |
|
93
|
|
|
} |
|
94
|
1 |
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|