Storage   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 36.11 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 84.38%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A persist() 0 19 2
A load() 13 13 3
A flush() 13 13 3
A setup() 0 6 1

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 5
        on($hosts, function (Host $host) {
23 5
            $values = [];
24
25
            // Materialize config values
26 5
            foreach ($host->getConfig()->getCollection() as $key => $value) {
27 5
                $values[$key] = $host->get($key);
28
            }
29
30 5
            $file = sys_get_temp_dir() . '/' . uniqid('deployer-') . '-' . $host->getHostname() . '.dep';
31 5
            $values['host_config_storage'] = $file;
32
33 5
            $persistentCollection = new PersistentCollection($file, $values);
34 5
            $persistentCollection->flush();
35
36 5
            $host->getConfig()->setCollection($persistentCollection);
37 5
        });
38 5
    }
39
40
    /**
41
     * @param Host[] $hosts
42
     * @throws Exception
43
     */
44 2 View Code Duplication
    public static function load(...$hosts)
45
    {
46 2
        $hosts = array_flatten($hosts);
47 2
        foreach ($hosts as $host) {
48 2
            $collection = $host->getConfig()->getCollection();
49
50 2
            if ($collection instanceof PersistentCollection) {
51 2
                $collection->load();
52
            } else {
53 2
                throw new Exception("Can't load data for `$host` host. Host doesn't persistent.");
54
            }
55
        }
56 2
    }
57
58
    /**
59
     * @param Host[] $hosts
60
     * @throws Exception
61
     */
62 2 View Code Duplication
    public static function flush(...$hosts)
63
    {
64 2
        $hosts = array_flatten($hosts);
65 2
        foreach ($hosts as $host) {
66 2
            $collection = $host->getConfig()->getCollection();
67
68 2
            if ($collection instanceof PersistentCollection) {
69 2
                $collection->flush();
70
            } else {
71 2
                throw new Exception("Can't load data for `$host` host. Host doesn't persistent.");
72
            }
73
        }
74 2
    }
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