CircuitBreaker   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A reportSuccess() 0 9 2
A isAvailable() 0 3 1
A reportFailure() 0 9 2
1
<?php
2
3
namespace JVelasco\CircuitBreaker;
4
5
use Throwable;
6
7
class CircuitBreaker
8
{
9
    private $availabilityStrategy;
10
    private $storage;
11
12 6
    public function __construct(
13
        AvailabilityStrategy $availabilityStrategy,
14
        FailuresCounterStorage $storage
15
    ) {
16 6
        $this->availabilityStrategy = $availabilityStrategy;
17 6
        $this->storage = $storage;
18 6
    }
19
20 2
    public function isAvailable(string $serviceName): bool
21
    {
22 2
        return $this->availabilityStrategy->isAvailable($serviceName);
23
    }
24
25 3
    public function reportFailure(string $serviceName)
26
    {
27
        try {
28 3
            $this->storage->incrementFailures($serviceName);
29 1
        } catch (Throwable $ex) {
30 1
            throw new StorageException(
31 1
                "Error incrementing failures",
32 1
                $ex->getCode(),
33 1
                $ex
34
            );
35
        }
36 2
    }
37
38 3
    public function reportSuccess(string $serviceName)
39
    {
40
        try {
41 3
            $this->storage->decrementFailures($serviceName);
42 1
        } catch (Throwable $ex) {
43 1
            throw new StorageException(
44 1
                "Error decrementing failures",
45 1
                $ex->getCode(),
46 1
                $ex
47
            );
48
        }
49 2
    }
50
}
51