Total Complexity | 9 |
Total Lines | 46 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
8 | final class FixedWaitTimeToRetry implements AvailabilityStrategy |
||
9 | { |
||
10 | private $storage; |
||
11 | private $maxFailures; |
||
12 | private $waitTime; |
||
13 | |||
14 | 5 | public function __construct(Storage $storage, int $maxFailures, int $waitTime) |
|
19 | 5 | } |
|
20 | |||
21 | 5 | public function isAvailable(string $serviceName): bool |
|
22 | { |
||
23 | try { |
||
24 | 5 | if ($this->storage->numberOfFailures($serviceName) < $this->maxFailures) { |
|
25 | 2 | return true; |
|
26 | } |
||
27 | |||
28 | 3 | $lastRetry = $this->getLastTryTime(); |
|
29 | 3 | if ($this->now() - $lastRetry > $this->waitTime) { |
|
30 | 1 | $this->storage->resetFailuresCounter($serviceName); |
|
31 | 1 | return true; |
|
32 | } |
||
33 | |||
34 | 2 | return false; |
|
35 | 1 | } catch (StorageException $ex) { |
|
36 | 1 | return true; |
|
37 | } |
||
38 | } |
||
39 | |||
40 | 1 | public function getId(): string |
|
43 | } |
||
44 | |||
45 | 3 | private function getLastTryTime(): int |
|
46 | { |
||
47 | 3 | $lastTryTimestamp = $this->storage->getStrategyData($this, "last_try"); |
|
48 | 3 | return $lastTryTimestamp ? $lastTryTimestamp : $this->now(); |
|
|
|||
49 | } |
||
50 | |||
51 | 3 | private function now(): int |
|
54 | } |
||
55 | } |
||
56 |