FileLoader   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 92
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A expandOnLoad() 0 15 4
C load() 0 53 11
A getHosts() 0 4 1
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\Exception\Exception;
11
use Symfony\Component\Yaml\Yaml;
12
13
class FileLoader
14
{
15
    /**
16
     * @var Host[]
17
     */
18
    private $hosts = [];
19
    /**
20
     * @param array $datain
21
     * @return array $dataexp
22
     */
23 2
    public function expandOnLoad($datain)
24
    {
25 2
        $dataout = [];
26 2
        foreach ($datain as $hostname => $config) {
27 2
            if (preg_match('/\[(.+?)\]/', $hostname)) {
28 2
                foreach (Range::expand([$hostname]) as $splithost) {
29 2
                    $dataout["$splithost"] = $config;
30
                }
31
            } else {
32 2
                $dataout["$hostname"] = $config;
33
            }
34
        }
35
36 2
        return $dataout;
37
    }
38
    /**
39
     * @param string $file
40
     * @return $this
41
     * @throws Exception
42
     */
43 2
    public function load($file)
44
    {
45 2
        if (!file_exists($file) || !is_readable($file)) {
46
            throw new Exception("File `$file` doesn't exists or isn't readable.");
47
        }
48
49 2
        $data = Yaml::parse(file_get_contents($file));
50 2
        $data = $this->expandOnLoad($data);
51
52 2
        if (!is_array($data)) {
53
            throw new Exception("Hosts file `$file` should contains array of hosts.");
54
        }
55
56 2
        foreach ($data as $hostname => $config) {
57 2
            if (preg_match('/^\./', $hostname)) {
58 2
                continue;
59
            }
60
61 2
            if (isset($config['local'])) {
62 2
                $host = new Localhost($hostname);
63
            } else {
64 2
                $host = new Host($hostname);
65
                $methods = [
66 2
                    'hostname',
67
                    'user',
68
                    'port',
69
                    'configFile',
70
                    'identityFile',
71
                    'forwardAgent',
72
                    'multiplexing',
73
                    'sshOptions',
74
                    'sshFlags',
75
                    'shellCommand',
76
                ];
77
78 2
                foreach ($methods as $method) {
79 2
                    if (isset($config[$method])) {
80 2
                        $host->$method($config[$method]);
81
                    }
82
                }
83
            }
84
85 2
            if (is_array($config)) {
86 2
                foreach ($config as $name => $value) {
87 2
                    $host->set($name, $value);
88
                }
89
            }
90
91 2
            $this->hosts[$hostname] = $host;
92
        }
93
94 2
        return $this;
95
    }
96
97
    /**
98
     * @return Host[]
99
     */
100 2
    public function getHosts()
101
    {
102 2
        return $this->hosts;
103
    }
104
}
105