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