1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_semaphore\Semaphore; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_paths\Stuff; |
7
|
|
|
use kalanis\kw_semaphore\Interfaces\ISemaphore; |
8
|
|
|
use kalanis\kw_semaphore\SemaphoreException; |
9
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
10
|
|
|
use kalanis\kw_storage\StorageException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Storage |
15
|
|
|
* @package kalanis\kw_semaphore\Semaphore |
16
|
|
|
* Data source for semaphore is storage |
17
|
|
|
*/ |
18
|
|
|
class Storage implements ISemaphore |
19
|
|
|
{ |
20
|
|
|
protected string $rootPath = ''; |
21
|
|
|
protected IStorage $storage; |
22
|
|
|
|
23
|
8 |
|
public function __construct(IStorage $storage, string $rootPath) |
24
|
|
|
{ |
25
|
8 |
|
$this->rootPath = Stuff::removeEndingSlash($rootPath) . static::EXT_SEMAPHORE; |
26
|
8 |
|
$this->storage = $storage; |
27
|
8 |
|
} |
28
|
|
|
|
29
|
4 |
|
public function want(): bool |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
4 |
|
return $this->storage->write($this->rootPath, static::TEXT_SEMAPHORE); |
33
|
1 |
|
} catch (StorageException $ex) { |
34
|
1 |
|
throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
6 |
|
public function has(): bool |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
6 |
|
return $this->storage->exists($this->rootPath); |
42
|
1 |
|
} catch (StorageException $ex) { |
43
|
1 |
|
throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
public function remove(): bool |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
4 |
|
return $this->storage->remove($this->rootPath); |
51
|
1 |
|
} catch (StorageException $ex) { |
52
|
1 |
|
throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|