RedisClusterAdapter::incrementFailure()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 5
c 3
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 2
1
<?php declare(strict_types=1);
2
3
namespace LeoCarmo\CircuitBreaker\Adapters;
4
5
class RedisClusterAdapter extends RedisAdapter
6
{
7
    /**
8
     * @param string $service
9
     * @param int $timeWindow
10
     * @return bool
11
     */
12
    public function incrementFailure(string $service, int $timeWindow): bool
13
    {
14
        $serviceName = $this->makeNamespace($service) . ':failures';
15
16
        if (! $this->redis->get($serviceName)) {
17
            $this->redis->incr($serviceName);
18
            return (bool) $this->redis->expire($serviceName, $timeWindow);
19
        }
20
21
        return (bool) $this->redis->incr($serviceName);
22
    }
23
24
    /**
25
     * @param string $service
26
     */
27
    public function setSuccess(string $service): void
28
    {
29
        $serviceName = $this->makeNamespace($service);
30
31
        $this->redis->del($serviceName . ':open');
32
        $this->redis->del($serviceName . ':failures');
33
        $this->redis->del($serviceName . ':half_open');
34
    }
35
}
36