Test Failed
Push — master ( c51706...f968f3 )
by Anton
01:54
created

Configuration::get()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.3541

Importance

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