Configuration::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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