Passed
Push — master ( f968f3...d9c744 )
by Anton
02:00
created

Configuration::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 9.8666
c 0
b 0
f 0
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 41
    public function __construct()
23
    {
24 41
        $this->collection = new Collection();
25 41
    }
26
27
    public function getCollection(): Collection
28
    {
29
        return $this->collection;
30
    }
31
32
    public function setCollection(Collection $collection)
33
    {
34
        $this->collection = $collection;
35
    }
36
37 25
    public function set(string $name, $value)
38
    {
39 25
        $this->collection[$name] = $value;
40 25
    }
41
42 8
    public function add(string $name, array $array)
43
    {
44 8
        if ($this->has($name)) {
45 7
            $config = $this->get($name);
46 7
            if (!is_array($config)) {
47 1
                throw new ConfigurationException("Configuration parameter `$name` isn't array.");
48
            }
49 6
            $this->set($name, array_merge_alternate($config, $array));
50
        } else {
51 1
            $this->set($name, $array);
52
        }
53 7
    }
54
55 26
    public function get(string $name, $default = null)
56
    {
57 26
        if ($this->collection->has($name)) {
58 22
            if ($this->isClosure($this->collection[$name])) {
59 2
                $value = $this->collection[$name] = call_user_func($this->collection[$name]);
60
            } else {
61 22
                $value = $this->collection[$name];
62
            }
63
        } else {
64 13
            $config = Deployer::get()->config;
65
66 13
            if (isset($config[$name])) {
67 7
                if ($this->isClosure($config[$name])) {
68 7
                    $value = $this->collection[$name] = call_user_func($config[$name]);
69
                } else {
70 7
                    $value = $this->collection[$name] = $config[$name];
71
                }
72
            } else {
73 13
                if (null === $default) {
74 1
                    throw new ConfigurationException("Configuration parameter `$name` does not exist.");
75
                } else {
76 13
                    $value = $default;
77
                }
78
            }
79
        }
80
81 26
        return $this->parse($value);
82
    }
83
84 24
    public function has(string $name): bool
85
    {
86 24
        return $this->collection->has($name);
87
    }
88
89
    /**
90
     * Parse set values
91
     *
92
     * @param mixed $value
93
     * @return mixed
94
     */
95 38
    public function parse($value)
96
    {
97 38
        if (is_string($value)) {
98 33
            return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $value);
99
        }
100
101 23
        return $value;
102
    }
103
104
    /**
105
     * Replace set values callback for parse
106
     *
107
     * @param string[] $matches
108
     * @return mixed
109
     */
110 10
    private function parseCallback(array $matches)
111
    {
112 10
        return isset($matches[1]) ? $this->get($matches[1]) : null;
113
    }
114
115 22
    private function isClosure($var): bool
116
    {
117 22
        return is_object($var) && ($var instanceof \Closure);
118
    }
119
}
120