Configuration   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 10
c 0
b 0
f 0
ccs 43
cts 43
cp 1
wmc 20
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCollection() 0 4 1
A setCollection() 0 4 1
A set() 0 4 1
A add() 0 12 3
B get() 0 28 6
A has() 0 4 1
A parse() 0 8 2
A parseCallback() 0 4 2
A isClosure() 0 4 2
1
<?php declare(strict_types=1);
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\Configuration;
9
10
use Deployer\Collection\Collection;
11
use Deployer\Deployer;
12
use Deployer\Exception\ConfigurationException;
13
use function Deployer\Support\array_merge_alternate;
14
15
class Configuration
16
{
17
    /**
18
     * @var Collection
19
     */
20
    private $collection;
21
22 45
    public function __construct()
23
    {
24 45
        $this->collection = new Collection();
25 45
    }
26
27 5
    public function getCollection(): Collection
28
    {
29 5
        return $this->collection;
30
    }
31
32
    /**
33
     * @return void
34
     */
35 5
    public function setCollection(Collection $collection)
36
    {
37 5
        $this->collection = $collection;
38 5
    }
39
40
    /**
41
     * @param mixed $value
42
     * @return void
43
     */
44 29
    public function set(string $name, $value)
45
    {
46 29
        $this->collection[$name] = $value;
47 29
    }
48
49
    /**
50
     * @param mixed[] $array
51
     * @return void
52
     */
53 8
    public function add(string $name, array $array)
54
    {
55 8
        if ($this->has($name)) {
56 7
            $config = $this->get($name);
57 7
            if (!is_array($config)) {
58 1
                throw new ConfigurationException("Configuration parameter `$name` isn't array.");
59
            }
60 6
            $this->set($name, array_merge_alternate($config, $array));
61
        } else {
62 1
            $this->set($name, $array);
63
        }
64 7
    }
65
66
    /**
67
     * @param mixed $default
68
     * @return mixed
69
     */
70 31
    public function get(string $name, $default = null)
71
    {
72 31
        if ($this->collection->has($name)) {
73 27
            if ($this->isClosure($this->collection[$name])) {
74 3
                $value = $this->collection[$name] = call_user_func($this->collection[$name]);
75
            } else {
76 27
                $value = $this->collection[$name];
77
            }
78
        } else {
79 18
            $config = Deployer::get()->config;
80
81 18
            if (isset($config[$name])) {
82 8
                if ($this->isClosure($config[$name])) {
83 8
                    $value = $this->collection[$name] = call_user_func($config[$name]);
84
                } else {
85 8
                    $value = $this->collection[$name] = $config[$name];
86
                }
87
            } else {
88 18
                if (null === $default) {
89 1
                    throw new ConfigurationException("Configuration parameter `$name` does not exist.");
90
                } else {
91 18
                    $value = $default;
92
                }
93
            }
94
        }
95
96 31
        return $this->parse($value);
97
    }
98
99
    /**
100
     * Checks if set var exists
101
     */
102 29
    public function has(string $name): bool
103
    {
104 29
        return $this->collection->has($name);
105
    }
106
107
    /**
108
     * Parse set values
109
     *
110
     * @param mixed $value
111
     * @return mixed
112
     */
113 42
    public function parse($value)
114
    {
115 42
        if (is_string($value)) {
116 37
            return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $value);
117
        }
118
119 28
        return $value;
120
    }
121
122
    /**
123
     * Replace set values callback for parse
124
     *
125
     * @param string[] $matches
126
     * @return mixed
127
     */
128 11
    private function parseCallback(array $matches)
129
    {
130 11
        return isset($matches[1]) ? $this->get($matches[1]) : null;
131
    }
132
133
    /**
134
     * @param mixed $var
135
     */
136 27
    private function isClosure($var): bool
137
    {
138 27
        return is_object($var) && ($var instanceof \Closure);
139
    }
140
}
141