Total Complexity | 10 |
Total Lines | 57 |
Duplicated Lines | 0 % |
Coverage | 62.5% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
10 | final class LockManager implements LockManagerInterface |
||
11 | { |
||
12 | private StuConfigInterface $config; |
||
13 | |||
14 | 2 | public function __construct(StuConfigInterface $config) |
|
17 | } |
||
18 | |||
19 | 1 | public function setLock(int $batchGroupId, LockTypeEnum $type): void |
|
20 | { |
||
21 | 1 | $result = @touch($this->getLockPath($batchGroupId, $type)); |
|
22 | |||
23 | 1 | if ($result === false) { |
|
24 | throw new RuntimeException(sprintf( |
||
25 | 'lock with batchGroupId "%d" of type "%d" could not be created', |
||
26 | $type->value, |
||
27 | $batchGroupId |
||
28 | )); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | 1 | public function clearLock(int $batchGroupId, LockTypeEnum $type): void |
|
33 | { |
||
34 | 1 | $result = @unlink($this->getLockPath($batchGroupId, $type)); |
|
35 | |||
36 | 1 | if ($result === false) { |
|
37 | throw new RuntimeException(sprintf( |
||
38 | 'lock with batchGroupId "%d" of type "%d" could not be deleted', |
||
39 | $type->value, |
||
40 | $batchGroupId |
||
41 | )); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | 1 | public function isLocked(int $entityId, LockTypeEnum $type): bool |
|
46 | { |
||
47 | 1 | return @file_exists($this->getLockPath($entityId % $this->getGroupCount($type) + 1, $type)); |
|
48 | } |
||
49 | |||
50 | 1 | private function getLockPath(int $batchGroupId, LockTypeEnum $type): string |
|
57 | 1 | ); |
|
58 | } |
||
59 | |||
60 | 1 | private function getGroupCount(LockTypeEnum $type): int |
|
67 | } |
||
68 | } |
||
70 |