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.
Test Failed
Push — master ( 31f734...513d45 )
by Anton
06:23
created

Storage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 36.11 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 26
loc 72
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 13 13 3
A flush() 13 13 3
A setup() 0 6 1
A persist() 0 19 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
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Host;
9
10
use Deployer\Collection\PersistentCollection;
11
use Deployer\Exception\Exception;
12
use function Deployer\on;
13
use function Deployer\Support\array_flatten;
14
15
class Storage
16
{
17
    /**
18
     * @param Host[] $hosts
19
     */
20
    public static function persist(array $hosts)
21
    {
22
        on($hosts, function (Host $host) {
23
            $values = [];
24
25
            // Materialize config values
26
            foreach ($host->getConfig()->getCollection() as $key => $value) {
27
                $values[$key] = $host->get($key);
28
            }
29
30
            $file = sys_get_temp_dir() . '/' . uniqid('deployer-') . '-' . $host->getHostname() . '.dep';
31
            $values['host_config_storage'] = $file;
32
33
            $persistentCollection = new PersistentCollection($file, $values);
34
            $persistentCollection->flush();
35
36
            $host->getConfig()->setCollection($persistentCollection);
37
        });
38
    }
39
40
    /**
41
     * @param Host[] $hosts
42
     * @throws Exception
43
     */
44 View Code Duplication
    public static function load(...$hosts)
45
    {
46
        $hosts = array_flatten($hosts);
47
        foreach ($hosts as $host) {
48
            $collection = $host->getConfig()->getCollection();
49
50
            if ($collection instanceof PersistentCollection) {
51
                $collection->load();
52
            } else {
53
                throw new Exception("Can't load data for `$host` host. Host doesn't persistent.");
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param Host[] $hosts
60
     * @throws Exception
61
     */
62 View Code Duplication
    public static function flush(...$hosts)
63
    {
64
        $hosts = array_flatten($hosts);
65
        foreach ($hosts as $host) {
66
            $collection = $host->getConfig()->getCollection();
67
68
            if ($collection instanceof PersistentCollection) {
69
                $collection->flush();
70
            } else {
71
                throw new Exception("Can't load data for `$host` host. Host doesn't persistent.");
72
            }
73
        }
74
    }
75
76
    /**
77
     * @param Host $host
78
     * @param string $file
79
     */
80
    public static function setup(Host $host, string $file)
81
    {
82
        $persistentCollection = new PersistentCollection($file);
83
        $persistentCollection->load();
84
        $host->getConfig()->setCollection($persistentCollection);
85
    }
86
}
87