Total Complexity | 5 |
Total Lines | 40 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
23 | final class SimpleMutex |
||
24 | { |
||
25 | private MutexFactoryInterface $mutexFactory; |
||
26 | |||
27 | /** |
||
28 | * @var MutexInterface[] |
||
29 | */ |
||
30 | private array $acquired = []; |
||
31 | |||
32 | public function __construct(MutexFactoryInterface $mutexFactory) |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Acquires a lock with a given name. |
||
39 | * |
||
40 | * @param string $name Name of the mutex to acquire. |
||
41 | * @param int $timeout Time (in seconds) to wait for lock to be released. Defaults to zero meaning that method |
||
42 | * will return false immediately in case lock was already acquired. |
||
43 | */ |
||
44 | public function acquire(string $name, int $timeout = 0): bool |
||
45 | { |
||
46 | $mutex = $this->mutexFactory->create($name); |
||
47 | if ($mutex->acquire($timeout)) { |
||
48 | $this->acquired[$name] = $mutex; |
||
49 | return true; |
||
50 | } |
||
51 | return false; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Releases a lock with a given name. |
||
56 | */ |
||
57 | public function release(string $name): void |
||
63 | } |
||
64 | } |
||
65 |