LocalArray::loadStatus()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 3
Ratio 17.65 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 3
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NiR\CircuitBreaker\Storage;
6
7
use NiR\CircuitBreaker\Service\Configuration;
8
use NiR\CircuitBreaker\Service\Status;
9
use NiR\CircuitBreaker\Storage;
10
11
class LocalArray implements Storage
12
{
13
    /** @var array Service status store. */
14
    private $store = [];
15
16
    public function loadStatus(Configuration $config): Status
17
    {
18
        $serviceName = $config->getServiceName();
19
20
        if (!isset($this->store[$serviceName])) {
21
            return new Status($config);
22
        }
23
24
        /** @var $status Status */
25
        list($ttl, $status) = array_values($this->store[$serviceName]);
26
27
        // If a ttl was stored with the status and it is outdated, the default status (closed breaker) is returned
28 View Code Duplication
        if ($ttl !== 0 && $status->getLastUpdate() + $ttl < time()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
            $status = new Status($config);
30
        }
31
32
        return $status;
33
    }
34
35
    public function saveStatus(Configuration $config, Status $status)
36
    {
37
        $this->store[$config->getServiceName()] = [
38
            'ttl' => $status->isClose() ? $config->getDismissDelay() : 0,
39
            'status' => $status,
40
        ];
41
    }
42
}
43