SimpleCacheAdapter::incrementAmountOfFailures()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace HolidayPirates\CircuitBreaker\Storage\Adapter;
4
5
use HolidayPirates\CircuitBreaker\Service\ServiceInterface;
6
use HolidayPirates\CircuitBreaker\Storage\Adapter\Exception\StorageAdapterException;
7
use HolidayPirates\CircuitBreaker\Storage\StorageInterface;
8
use Psr\SimpleCache\CacheException;
9
use Psr\SimpleCache\CacheInterface;
10
11
class SimpleCacheAdapter implements StorageInterface
12
{
13
    public const CACHE_PREFIX = 'circuit_breaker';
14
    public const CIRCUIT_OPEN_SUFFIX = 'circuit_open';
15
    public const FAILURE_SUFFIX = 'failures';
16
17
    private $cache;
18
19
    public function __construct(CacheInterface $cache)
20
    {
21
        $this->cache = $cache;
22
    }
23
24
    public function getAmountOfFailures(ServiceInterface $service): int
25
    {
26
        $cacheKey = $this->getFailureCacheKey($service);
27
28
        return $this->cacheGet($cacheKey, 0);
29
    }
30
31
    public function incrementAmountOfFailures(ServiceInterface $service): void
32
    {
33
        $cacheKey = $this->getFailureCacheKey($service);
34
        $amountOfFailures = $this->getAmountOfFailures($service);
35
        $this->cacheSet($cacheKey, ++$amountOfFailures);
36
    }
37
38
39
    /**
40
     * fakeTODO rename this method to investInAppleInThe90s
41
     * @throws StorageAdapterException
42
     */
43
    public function incrementAmountOfSuccess(ServiceInterface $service): void
44
    {
45
        $cacheKey = $this->getFailureCacheKey($service);
46
        $amount = $this->getAmountOfFailures($service);
47
        $amount = max(--$amount, 0); //This is to ensure that any negative number will turn into 0
48
        $this->cacheSet($cacheKey, $amount);
49
    }
50
51
    public function setOpenCircuit(ServiceInterface $service): void
52
    {
53
        $cacheKey = $this->getOpenCircuitCacheKey($service);
54
55
        $this->cacheSet(
56
            $cacheKey,
57
            true,
58
            $service->getRetryTimeOut()
59
        );
60
    }
61
62
    public function isCircuitOpen(ServiceInterface $service): bool
63
    {
64
        $cacheKey = $this->getOpenCircuitCacheKey($service);
65
66
        return (bool)$this->cacheGet($cacheKey, false);
67
    }
68
69
    private function getOpenCircuitCacheKey(ServiceInterface $service): string
70
    {
71
        return sprintf(
72
            '%s_%s_%s',
73
            self::CACHE_PREFIX,
74
            $this->normalizeServiceCacheKey($service->getIdentifier()),
75
            self::CIRCUIT_OPEN_SUFFIX
76
        );
77
    }
78
79
    private function getFailureCacheKey(ServiceInterface $service): string
80
    {
81
        return sprintf(
82
            '%s_%s_%s',
83
            self::CACHE_PREFIX,
84
            $this->normalizeServiceCacheKey($service->getIdentifier()),
85
            self::FAILURE_SUFFIX
86
        );
87
    }
88
89
    private function normalizeServiceCacheKey(string $key): string
90
    {
91
        return str_replace('\\', '_', mb_strtolower($key));
92
    }
93
94
    /**
95
     * @throws StorageAdapterException
96
     */
97
    private function cacheSet(string $key, $value, $ttl = null): void
98
    {
99
        try {
100
            $this->cache->set($key, $value, $ttl);
101
        } catch (CacheException $e) {
102
            $message = "There was some problem with the driver while trying to set the key : {$key}";
103
104
            throw new StorageAdapterException($message, 0, $e);
105
        }
106
    }
107
108
    /**
109
     * @throws StorageAdapterException
110
     */
111
    private function cacheGet(string $key, $default = null)
112
    {
113
        try {
114
            return $this->cache->get($key, $default);
115
        } catch (CacheException $e) {
116
            $message = "There was some problem with the driver while trying to get the key : {$key}";
117
118
            throw new StorageAdapterException($message, 0, $e);
119
        }
120
    }
121
}
122