Passed
Push — master ( 072d04...55dded )
by Jorge
01:39
created

FixedWaitTimeToRetry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace JVelasco\CircuitBreaker\AvailabilityStrategy;
4
5
use JVelasco\CircuitBreaker\AvailabilityStrategy;
6
use JVelasco\CircuitBreaker\StorageException;
7
8
final class FixedWaitTimeToRetry implements AvailabilityStrategy
9
{
10
    private $storage;
11
    private $maxFailures;
12
    private $waitTime;
13
14 4
    public function __construct(Storage $storage, int $maxFailures, int $waitTime)
15
    {
16 4
        $this->storage = $storage;
17 4
        $this->maxFailures = $maxFailures;
18 4
        $this->waitTime = $waitTime;
19 4
    }
20 4
    public function isAvailable(string $serviceName): bool
21
    {
22
        try {
23 4
            if ($this->storage->numberOfFailures($serviceName) < $this->maxFailures) {
24 1
                return true;
25
            }
26
27 2
            $lastRetry = $this->getLastTryTime();
28 2
            if (time() - $lastRetry > $this->waitTime) {
29 1
                return true;
30
            }
31
32 1
            return false;
33 1
        } catch (StorageException $ex) {
34 1
            return true;
35
        }
36
    }
37
38
    public function getId(): string
39
    {
40
        return "fixed_time_to_retry";
41
    }
42
43 2
    private function getLastTryTime(): int
44
    {
45 2
        $lastTryTimestamp = $this->storage->getStrategyData($this, "last_try");
46 2
        return $lastTryTimestamp ? $lastTryTimestamp : time();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $lastTryTimestamp...stTryTimestamp : time() could return the type string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
47
    }
48
}
49