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.
Failed Conditions
Push — master ( 2273d9...7f55dd )
by Jonathan
02:36
created

CircuitBreakerService::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace CircuitBreakerBundle\Service;
4
5
use Symfony\Component\Cache\Adapter\AbstractAdapter;
6
use Psr\Log\LoggerTrait as PsrLoggerTrait;
7
use Psr\Log\LoggerInterface;
8
9
class CircuitBreakerService
10
{
11
    use PsrLoggerTrait;
12
13
    const OPEN = 'open';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
14
    const CLOSED = 'closed';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
15
    const HALFOPEN = 'half-open';
16
17
    /**
18
     * @var AbstractAdapter
19
     */
20
    private $cacheApp;
21
22
    /**
23
     * @var array
24
     */
25
    private $status;
26
27
    /**
28
     * @var int
29
     */
30
    private $threshold;
31
32
    /**
33
     * @var int
34
     */
35
    private $timeout;
36
37
    /**
38
     * @param AbstractAdapter $cacheApp
39
     * @param int $threshold
40
     * @param int $timeout
41
     */
42
    public function __construct(AbstractAdapter $cacheApp, int $threshold, int $timeout)
43
    {
44
        $this->cacheApp = $cacheApp;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
45
        $this->threshold = $threshold;
46
        $this->timeout = $timeout;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
47
    }
48
49
    /**
50
     * @param string $key The service key
51
     * @param bool $status The service status (true: up, false: down)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $status not be boolean|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
52
     */
53
    public function save(string $key, bool $status)
54
    {
55
        if (!isset($this->status[$key])) {
56
            $this->status[$key] = self::CLOSED;
57
        }
58
59
        if ($this->status[$key] === self::OPEN) {
60
            $this->attemptReset($key);
61
        }
62
63
        if (!$status) {
64
            $this->countFailure($key);
65
        } else {
66
            $this->resetCount($key);
67
        }
68
    }
69
70
    /**
71
     * Verify if service is open.
72
     *
73
     * @param string $service
74
     *
75
     * @return bool
76
     */
77
    public function isOpen(string $service): bool
78
    {
79
        if (!isset($this->status[$service]) &&
80
            $this->cacheApp->getItem($service)->get() >= $this->threshold
81
        ) {
82
            $this->status[$service] = self::OPEN;
83
        }
84
85
        return $this->status[$service] === self::OPEN;
86
    }
87
88
    /**
89
     * Increment number of fail to one service
90
     *
91
     * @param string $service
92
     */
93
    private function countFailure(string $service)
94
    {
95
        $this->notice('[CircuitBreaker] call countFailure to ' . $service);
96
        $value = $this->cacheApp->getItem($service);
97
        $fail = $value->get() + 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
98
        $value->set($fail);
99
100
        if ($this->status[$service] === self::HALFOPEN) {
101
            $value->set($this->threshold);
102
        }
103
104
        $value->expiresAfter($this->timeout);
105
106
        if ($fail >= $this->threshold) {
107
            $this->tripBreaker($service);
108
        }
109
110
        $this->cacheApp->save($value);
111
    }
112
113
    /**
114
     * Open circuit breaker.
115
     *
116
     * @param string $service
117
     */
118
    private function tripBreaker(string $service)
119
    {
120
        $this->error('[CircuitBreaker] call tripBreaker to ' . $service);
121
        $this->status[$service] = self::OPEN;
122
    }
123
124
    /**
125
     * CLose circuit breaker, and reset value to fail service.
126
     *
127
     * @param string $service
128
     */
129
    private function resetCount(string $service)
130
    {
131
        $this->info('[CircuitBreaker] call resetCount to ' . $service);
132
        $value = $this->cacheApp->getItem($service);
133
134
        $value->set(0);
135
        $this->status[$service] = self::CLOSED;
136
        $this->cacheApp->save($value);
137
    }
138
139
    /**
140
     * HalfOpen circuit breaker.
141
     *
142
     * @param string $service
143
     */
144
    private function attemptReset(string $service)
145
    {
146
        $this->warning('[CircuitBreaker] call attemptReset to ' . $service);
147
        $this->status[$service] = self::HALFOPEN;
148
    }
149
150
    /**
151
     * @var LoggerInterface
152
     */
153
    private $logger;
154
155
    /**
156
     * @param LoggerInterface $logger
157
     */
158
    public function setLogger(LoggerInterface $logger)
159
    {
160
        $this->logger = $logger;
161
    }
162
163
    /**
164
     * @param string $level
165
     * @param string $message
166
     * @param array $context
167
     */
168
    public function log($level, $message, array $context = array())
169
    {
170
        $this->logger->log($level, $message, $context);
171
    }
172
}
173