ConfigurationManager::create()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 8.5126
cc 5
eloc 12
nc 16
nop 1
crap 5
1
<?php
2
3
namespace Onigoetz\Deployer\Configuration;
4
5
use Onigoetz\Deployer\Configuration\Containers\ConfigurationContainer;
6
7
class ConfigurationManager
8
{
9
    protected $configurations = [];
10
11
    public $logs = [];
12
13 18
    public static function create($data)
14
    {
15 18
        $manager = new self();
16
17 18
        $manager->setDefaultDirectories(new Directories('default', $data['directories'], $manager));
18
19 18
        foreach ($data['servers'] as $name => $server) {
20 6
            $manager->set(new Server($name, $server, $manager));
21 12
        }
22
23 18
        foreach ($data['sources'] as $name => $source) {
24 6
            $manager->set(Source::make($name, $source, $manager));
25 12
        }
26
27 18
        foreach ($data['environments'] as $name => $environment) {
28 18
            $manager->set(new Environment($name, $environment, $manager));
29 12
        }
30
31 18
        foreach ($data['tasks'] as $name => $tasks) {
32 15
            $manager->set(new Tasks($name, $tasks, $manager));
33 12
        }
34
35 18
        return $manager;
36
    }
37
38
    /**
39
     * @param string $type
40
     * @param string $key
41
     *
42
     * @throws \LogicException
43
     * @return ConfigurationContainer
44
     */
45 69
    public function get($type, $key)
46
    {
47 69
        if (!$this->has($type, $key)) {
48 6
            throw new \LogicException("No item of type '$type' with name '$key' in the configuration manager");
49
        }
50
51 63
        return $this->configurations[$type][$key];
52
    }
53
54
    /**
55
     * @param string $type
56
     * @param string $key
57
     *
58
     * @return bool
59
     */
60 69
    public function has($type, $key)
61
    {
62 69
        if (!array_key_exists($type, $this->configurations) || !array_key_exists($key, $this->configurations[$type])) {
63 6
            return false;
64
        }
65
66 63
        return true;
67
    }
68
69
    /**
70
     * @param ConfigurationContainer $value
71
     */
72 69
    public function set(ConfigurationContainer $value)
73
    {
74 69
        $this->configurations[$value->getContainerType()][$value->getName()] = $value;
75 69
    }
76
77 51
    public function setDefaultDirectories(Directories $value)
78
    {
79 51
        $this->set($value);
80 51
    }
81
82
    /**
83
     * @return Directories
84
     */
85 24
    public function getDefaultDirectories()
86
    {
87 24
        return $this->get('directories', 'default');
88
    }
89
90 30
    public function log($message)
91
    {
92 30
        $this->logs[] = $message;
93 30
    }
94
95 3
    public function getLogs()
96
    {
97 3
        return $this->logs;
98
    }
99
}
100