|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Semaphoro\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Semaphoro\Exception\SemaphoroException; |
|
7
|
|
|
use Semaphoro\Storages\StorageInterface; |
|
8
|
|
|
|
|
9
|
|
|
class RangeHandler implements HandlerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Last number executed |
|
13
|
|
|
*/ |
|
14
|
|
|
const LAST_EXECUTED_NUMBER_KEY = 'last_executed_number'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Range's status |
|
18
|
|
|
* |
|
19
|
|
|
* 0 = Unprocessed |
|
20
|
|
|
* 1 = Processing |
|
21
|
|
|
*/ |
|
22
|
|
|
const STATUS_UNPROCESSED = "0"; |
|
23
|
|
|
const STATUS_PROCESSING = "1"; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var StorageInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $storage; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
private $rangeLength; |
|
34
|
|
|
|
|
35
|
8 |
|
public function __construct(StorageInterface $storage, int $rangeLength = 50) |
|
36
|
|
|
{ |
|
37
|
8 |
|
$this->storage = $storage; |
|
38
|
8 |
|
$this->rangeLength = $rangeLength; |
|
39
|
8 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return boolean|string |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function getProcessOpened(): ?string |
|
45
|
|
|
{ |
|
46
|
4 |
|
$rangeKeys = $this->storage->getKeys('[0-9]*'); |
|
47
|
|
|
|
|
48
|
4 |
|
foreach ($rangeKeys as $rangeKey) { |
|
49
|
|
|
try { |
|
50
|
3 |
|
$rangeKeyStatus = $this->storage->getValue($rangeKey); |
|
51
|
1 |
|
} catch (\Throwable $e) { |
|
52
|
1 |
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
if ($rangeKeyStatus === self::STATUS_UNPROCESSED) { |
|
56
|
1 |
|
$this->setStatusInProcess($rangeKey); |
|
57
|
1 |
|
return $rangeKey; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
return false; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $rangeKey |
|
66
|
|
|
*/ |
|
67
|
1 |
|
private function setStatusInProcess(string $rangeKey): void |
|
68
|
|
|
{ |
|
69
|
1 |
|
$this->storage->save($rangeKey, self::STATUS_PROCESSING); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
* @throws SemaphoroException |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function createProcessKey(): string |
|
77
|
|
|
{ |
|
78
|
2 |
|
$lastExecutedNumber = $this->getLastExecutedNumber(); |
|
79
|
|
|
|
|
80
|
1 |
|
$this->setLastExecutedNumber($this->calculateLastRangeNumber($lastExecutedNumber)); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
1 |
|
return $this->createRangeKey($lastExecutedNumber); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
* @throws SemaphoroException when doesn't have a last executed number |
|
88
|
|
|
*/ |
|
89
|
2 |
|
private function getLastExecutedNumber(): string |
|
90
|
|
|
{ |
|
91
|
|
|
try { |
|
92
|
2 |
|
return $this->storage->getValue(self::LAST_EXECUTED_NUMBER_KEY); |
|
93
|
1 |
|
} catch (\Throwable $e) { |
|
94
|
1 |
|
throw new SemaphoroException('Last executed number is required'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param int $number |
|
100
|
|
|
*/ |
|
101
|
1 |
|
private function setLastExecutedNumber(int $number): void |
|
102
|
|
|
{ |
|
103
|
1 |
|
$this->storage->save(self::LAST_EXECUTED_NUMBER_KEY, $number); |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param int $number |
|
108
|
|
|
* @return int |
|
109
|
|
|
*/ |
|
110
|
1 |
|
private function calculateFirstRangeNumber(int $number): int |
|
111
|
|
|
{ |
|
112
|
1 |
|
return ++$number; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param int $number |
|
117
|
|
|
* @return int |
|
118
|
|
|
*/ |
|
119
|
1 |
|
private function calculateLastRangeNumber(int $number): int |
|
120
|
|
|
{ |
|
121
|
1 |
|
return $number + $this->rangeLength; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $lastExecutedNumber |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
1 |
|
private function createRangeKey(string $lastExecutedNumber): string |
|
129
|
|
|
{ |
|
130
|
1 |
|
return sprintf( |
|
131
|
1 |
|
'%s_%s', |
|
132
|
1 |
|
$this->calculateFirstRangeNumber($lastExecutedNumber), |
|
|
|
|
|
|
133
|
1 |
|
$this->calculateLastRangeNumber($lastExecutedNumber) |
|
|
|
|
|
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $rangeKey |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function setUnprocessedStatus(string $rangeKey): void |
|
141
|
|
|
{ |
|
142
|
1 |
|
$this->storage->save($rangeKey, self::STATUS_UNPROCESSED); |
|
143
|
1 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $rangeKey |
|
147
|
|
|
*/ |
|
148
|
1 |
|
public function remove(string $rangeKey): void |
|
149
|
|
|
{ |
|
150
|
1 |
|
$this->storage->remove($rangeKey); |
|
151
|
|
|
} |
|
152
|
|
|
} |