|
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\OperationVisitorAwareInterface; |
|
10
|
|
|
use Johmanx10\Transaction\Visitor\OperationVisitorInterface; |
|
11
|
|
|
|
|
12
|
|
|
class OperationHandler implements |
|
13
|
|
|
OperationHandlerInterface, |
|
14
|
|
|
OperationVisitorAwareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var OperationVisitorInterface[] */ |
|
17
|
|
|
private $visitors = []; |
|
18
|
|
|
|
|
19
|
|
|
/** @var TransactionFactoryInterface */ |
|
20
|
|
|
private $factory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param TransactionFactoryInterface|null $factory |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function __construct(TransactionFactoryInterface $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
|
1 |
|
public function handle(OperationInterface ...$operations): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this |
|
42
|
1 |
|
->factory |
|
43
|
1 |
|
->createTransaction(...$operations) |
|
44
|
1 |
|
->commit(...array_values($this->visitors)); |
|
|
|
|
|
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Attach operation visitors to the current container. |
|
49
|
|
|
* |
|
50
|
|
|
* @param OperationVisitorInterface ...$visitors |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function attachVisitor(OperationVisitorInterface ...$visitors): void |
|
55
|
|
|
{ |
|
56
|
1 |
|
foreach ($visitors as $visitor) { |
|
57
|
1 |
|
$this->visitors[spl_object_hash($visitor)] = $visitor; |
|
58
|
|
|
} |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Detach operation visitors to the current container. |
|
63
|
|
|
* |
|
64
|
|
|
* @param OperationVisitorInterface ...$visitors |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function detachVisitor(OperationVisitorInterface ...$visitors): void |
|
69
|
|
|
{ |
|
70
|
1 |
|
foreach ($visitors as $visitor) { |
|
71
|
1 |
|
unset($this->visitors[spl_object_hash($visitor)]); |
|
72
|
|
|
} |
|
73
|
1 |
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.