GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( bb1070...b3065a )
by Alfons
02:22
created

CircuitBreaker::setFailures()   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
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Cmp\CircuitBreaker;
4
5
use Cmp\CircuitBreaker\Exception\ServiceAlreadyTrackedException;
6
use Cmp\CircuitBreaker\Exception\ServiceNotTrackedException;
7
use Psr\SimpleCache\CacheInterface;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
11
/**
12
 * Class CircuitBreaker
13
 *
14
 * @package Cmp\CircuitBreaker
15
 */
16
class CircuitBreaker
17
{
18
    protected $cache;
19
20
    protected $logger;
21
22
    protected $timeFactory;
23
24
    protected $services    = [];
25
26
    protected $cachePrefix = 'cb';
27
28
    protected $ttl         = 3360;
29
30
    /**
31
     * CircuitBreaker constructor.
32
     *
33
     * @param CacheInterface       $cache       Cache to store the failures and times
34
     * @param LoggerInterface|null $logger      To log stuff
35
     * @param TimeFactory|null     $timeFactory Time Factory to mock the class
36
     */
37 1
    public function __construct(
38
        CacheInterface $cache,
39
        LoggerInterface $logger = null,
40
        TimeFactory $timeFactory = null
41
    ) {
42 1
        $this->cache       = $cache;
43 1
        $this->logger      = $logger instanceof LoggerInterface ? $logger : new NullLogger();
44 1
        $this->timeFactory = $timeFactory instanceof TimeFactory ? $timeFactory : new TimeFactory();
45 1
    }
46
47
    /**
48
     * @param string $serviceName Name of the service
49
     * @param string $field       Field you want to fetch
50
     *
51
     * @return string
52
     */
53 1
    protected function getCacheKey($serviceName, $field)
54
    {
55 1
        return $this->cachePrefix.'-'.$serviceName.'-'.$field;
56
    }
57
58
    /**
59
     * @param string $serviceName Name of the service
60
     *
61
     * @return Service|null
62
     */
63 1
    protected function getService($serviceName)
64
    {
65 1
        if (array_key_exists($serviceName, $this->services)) {
66 1
            return $this->services[$serviceName];
67
        }
68
69 1
        return null;
70
    }
71
72
    /**
73
     * @param string $serviceName Name of the service
74
     *
75
     * @return int
76
     */
77 1
    protected function getFailures($serviceName)
78
    {
79 1
        return (int) $this->cache->get($this->getCacheKey($serviceName, 'failures'));
80
    }
81
82
    /**
83
     * @param string $serviceName Name of the service
84
     *
85
     * @return int
86
     */
87 1
    protected function getLastTest($serviceName)
88
    {
89 1
        return (int) $this->cache->get($this->getCacheKey($serviceName, 'lastTest'));
90
    }
91
92
    /**
93
     * @param string $serviceName Name of the service
94
     * @param int    $newValue    New value to store
95
     */
96 1
    protected function setFailures($serviceName, $newValue)
97
    {
98 1
        $this->cache->set($this->getCacheKey($serviceName, 'failures'), $newValue, $this->ttl);
99 1
        $this->cache->set($this->getCacheKey($serviceName, 'lastTest'), $this->timeFactory->time(), $this->ttl);
100 1
    }
101
102
    /**
103
     * @param Service $service Service to track
104
     *
105
     * @throws ServiceAlreadyTrackedException
106
     */
107 1
    public function trackService(Service $service)
108
    {
109 1
        if ($this->getService($service->getName()) !== null) {
110 1
            throw new ServiceAlreadyTrackedException($service->getName());
111
        }
112 1
        $this->services[$service->getName()] = $service;
113 1
    }
114
115
    /**
116
     * @param string $serviceName Name of the service
117
     *
118
     * @return bool
119
     * @throws ServiceNotTrackedException
120
     */
121 1
    public function isAvailable($serviceName)
122
    {
123 1
        $service = $this->getService($serviceName);
124 1
        if ($service == null) {
125 1
            throw new ServiceNotTrackedException($serviceName);
126
        }
127
128 1
        $failures    = $this->getFailures($serviceName);
129 1
        $maxFailures = $service->getMaxFailures();
130
131 1
        if ($failures < $maxFailures) {
132 1
            return true;
133
        }
134
135 1
        $lastTest     = $this->getLastTest($serviceName);
136 1
        $retryTimeout = $service->getRetryTimeout();
137
138 1
        if (($lastTest + $retryTimeout) < $this->timeFactory->time()) {
139 1
            $this->setFailures($serviceName, $failures);
140 1
            $this->logger->info('Attempting service '.$serviceName.' one more time');
141
142 1
            return true;
143
        }
144
145 1
        return false;
146
    }
147
148
    /**
149
     * @param string $serviceName Name of the service
150
     *
151
     * @throws ServiceNotTrackedException
152
     */
153 1
    public function reportFailure($serviceName)
154
    {
155 1
        if ($this->getService($serviceName) == null) {
156 1
            throw new ServiceNotTrackedException($serviceName);
157
        }
158
159 1
        $this->setFailures($serviceName, $this->getFailures($serviceName) + 1);
160 1
        $this->logger->warning('Service '.$serviceName.' is shaky');
161 1
    }
162
163
    /**
164
     * @param string $serviceName Name of the service
165
     *
166
     * @throws ServiceNotTrackedException
167
     */
168 1
    public function reportSuccess($serviceName)
169
    {
170 1
        $service = $this->getService($serviceName);
171 1
        if ($service == null) {
172 1
            throw new ServiceNotTrackedException($serviceName);
173
        }
174
175 1
        $failures    = $this->getFailures($serviceName);
176 1
        $maxFailures = $service->getMaxFailures();
177
178 1
        if ($failures > $maxFailures) {
179 1
            $this->setFailures($serviceName, $maxFailures - 1);
180 1
            $this->logger->alert('Service '.$serviceName.' is down');
181 1
        } elseif ($failures > 0) {
182 1
            $this->setFailures($serviceName, $failures - 1);
183
        }
184 1
    }
185
}
186