LocalArray   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 10.34 %

Importance

Changes 0
Metric Value
dl 3
loc 29
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadStatus() 3 17 4
A saveStatus() 0 5 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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