RedisClusterAdapter::incrementFailure()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 6
c 5
b 1
f 0
dl 0
loc 12
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
        $this->resetKeyIfInfiniteTTL($serviceName);
17
18
        if (! $this->redis->get($serviceName)) {
19
            $this->redis->incr($serviceName);
20
            return (bool) $this->redis->expire($serviceName, $timeWindow);
21
        }
22
23
        return (bool) $this->redis->incr($serviceName);
24
    }
25
26
    /**
27
     * @param string $service
28
     */
29
    public function setSuccess(string $service): void
30
    {
31
        $serviceName = $this->makeNamespace($service);
32
33
        $this->redis->del($serviceName . ':open');
34
        $this->redis->del($serviceName . ':failures');
35
        $this->redis->del($serviceName . ':half_open');
36
    }
37
}
38