Total Complexity | 5 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | class CircularDependencyDetection |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $executing = []; |
||
15 | |||
16 | /** |
||
17 | * @param mixed $id |
||
18 | * @param Closure $process |
||
19 | * @return mixed |
||
20 | * @throws CircularDependencyException |
||
21 | */ |
||
22 | public function execute($id, Closure $process) |
||
23 | { |
||
24 | if ($this->isExecuting($id)) { |
||
25 | throw CircularDependencyException::forId($id, array_keys($this->executing)); |
||
26 | } |
||
27 | |||
28 | $this->acquire($id); |
||
29 | |||
30 | try { |
||
31 | return $process(); |
||
32 | } finally { |
||
33 | $this->release($id); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | protected function acquire($id): void |
||
40 | } |
||
41 | |||
42 | protected function release($id): void |
||
45 | } |
||
46 | |||
47 | public function isExecuting($id): bool |
||
50 | } |
||
51 | } |
||
52 |