|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_semaphore\Semaphore; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
|
7
|
|
|
use kalanis\kw_files\FilesException; |
|
8
|
|
|
use kalanis\kw_paths\ArrayPath; |
|
9
|
|
|
use kalanis\kw_paths\PathsException; |
|
10
|
|
|
use kalanis\kw_semaphore\Interfaces\ISemaphore; |
|
11
|
|
|
use kalanis\kw_semaphore\SemaphoreException; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Files |
|
16
|
|
|
* @package kalanis\kw_semaphore\Semaphore |
|
17
|
|
|
* Data source for semaphore is files |
|
18
|
|
|
*/ |
|
19
|
|
|
class Files implements ISemaphore |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var string[] */ |
|
22
|
|
|
protected array $rootPath = []; |
|
23
|
|
|
protected CompositeAdapter $lib; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param CompositeAdapter $lib |
|
27
|
|
|
* @param string[] $rootPath |
|
28
|
|
|
*/ |
|
29
|
8 |
|
public function __construct(CompositeAdapter $lib, array $rootPath) |
|
30
|
|
|
{ |
|
31
|
8 |
|
$libArr = new ArrayPath(); |
|
32
|
8 |
|
$libArr->setArray($rootPath); |
|
33
|
8 |
|
$this->rootPath = array_merge( |
|
34
|
8 |
|
$libArr->getArrayDirectory(), |
|
35
|
8 |
|
[$libArr->getFileName() . static::EXT_SEMAPHORE] |
|
36
|
|
|
); |
|
37
|
8 |
|
$this->lib = $lib; |
|
38
|
8 |
|
} |
|
39
|
|
|
|
|
40
|
4 |
|
public function want(): bool |
|
41
|
|
|
{ |
|
42
|
|
|
try { |
|
43
|
4 |
|
return $this->lib->saveFile($this->rootPath, static::TEXT_SEMAPHORE); |
|
44
|
1 |
|
} catch (FilesException | PathsException $ex) { |
|
45
|
1 |
|
throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
6 |
|
public function has(): bool |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
6 |
|
return $this->lib->exists($this->rootPath); |
|
53
|
1 |
|
} catch (FilesException | PathsException $ex) { |
|
54
|
1 |
|
throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
public function remove(): bool |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
4 |
|
return $this->lib->deleteFile($this->rootPath); |
|
62
|
1 |
|
} catch (FilesException | PathsException $ex) { |
|
63
|
1 |
|
throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|