Completed
Push — master ( 0cba44...be5c0d )
by Jorge
01:37
created

FixedWaitTimeToRetry::getLastTryTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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 5
    public function __construct(Storage $storage, int $maxFailures, int $waitTime)
15
    {
16 5
        $this->storage = $storage;
17 5
        $this->maxFailures = $maxFailures;
18 5
        $this->waitTime = $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 (time() - $lastRetry > $this->waitTime) {
30 1
                return true;
31
            }
32
33 2
            return false;
34 1
        } catch (StorageException $ex) {
35 1
            return true;
36
        }
37
    }
38
39 1
    public function getId(): string
40
    {
41 1
        return "fixed_time_to_retry";
42
    }
43
44 3
    private function getLastTryTime(): int
45
    {
46 3
        $lastTryTimestamp = $this->storage->getStrategyData($this, "last_try");
47 3
        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...
48
    }
49
}
50