GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Configuration::has()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 10
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\Exception\ConfigurationException;
11
use Deployer\Utility\Httpie;
12
use function Deployer\get;
13
use function Deployer\Support\array_merge_alternate;
14
use function Deployer\Support\is_closure;
15
use function Deployer\Support\normalize_line_endings;
16
17
class Configuration implements \ArrayAccess
18
{
19
    private $parent;
20
    private $values = [];
21
22 46
    public function __construct(Configuration $parent = null)
23
    {
24 46
        $this->parent = $parent;
25 46
    }
26
27 4
    public function update($values)
28
    {
29 4
        $this->values = $values;
30 4
    }
31
32 46
    public function bind(Configuration $parent)
33
    {
34 46
        $this->parent = $parent;
35 46
    }
36
37 24
    public function set(string $name, $value)
38
    {
39 24
        $this->values[$name] = $value;
40 24
    }
41 20
42
    public function has(string $name): bool
43 8
    {
44 3
        $ok = array_key_exists($name, $this->values);
45
        if ($ok) {
46 6
            return true;
47
        }
48
        if ($this->parent) {
49 8
            return $this->parent->has($name);
50
        }
51 8
        return false;
52 7
    }
53 7
54 2
    public function add(string $name, array $array)
55
    {
56 5
        if ($this->has($name)) {
57
            $config = $this->get($name);
58 1
            if (!is_array($config)) {
59
                throw new ConfigurationException("Config option \"$name\" isn't array.");
60 6
            }
61
            $this->set($name, array_merge_alternate($config, $array));
62 39
        } else {
63
            $this->set($name, $array);
64 39
        }
65 37
    }
66 2
67
    public function get(string $name, $default = null)
68 37
    {
69
        if (array_key_exists($name, $this->values)) {
70
            if (is_closure($this->values[$name])) {
71
                return $this->values[$name] = $this->parse(call_user_func($this->values[$name]));
72 26
            } else {
73 21
                return $this->parse($this->values[$name]);
74 21
            }
75 14
        }
76 8
77
        if ($this->parent) {
78 13
            $rawValue = $this->parent->fetch($name);
79
            if ($rawValue !== null) {
80
                if (is_closure($rawValue)) {
81
                    return $this->values[$name] = $this->parse(call_user_func($rawValue));
82
                } else {
83 21
                    return $this->values[$name]= $this->parse($rawValue);
84 20
                }
85
            }
86
        }
87 2
88
        if ($default !== null) {
89
            return $this->parse($default);
90 21
        }
91
92 21
        throw new ConfigurationException("Config option \"$name\" does not exist.");
93 14
    }
94
95 17
    protected function fetch($name)
96 1
    {
97
        if (array_key_exists($name, $this->values)) {
98 16
            return $this->values[$name];
99
        }
100
        if ($this->parent) {
101 38
            return $this->parent->fetch($name);
102
        }
103 38
        return null;
104 33
    }
105 33
106
    public function parse($value)
107
    {
108 28
        if (is_string($value)) {
109
            $normalizedValue = normalize_line_endings($value);
110
            return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $normalizedValue);
111 14
        }
112
113 14
        return $value;
114
    }
115
116 16
    private function parseCallback(array $matches)
117
    {
118 16
        return isset($matches[1]) ? $this->get($matches[1]) : null;
119
    }
120
121 16
    public function offsetExists($offset)
122
    {
123 16
        return $this->has($offset);
124
    }
125
126 21
    public function offsetGet($offset)
127
    {
128 21
        return $this->get($offset);
129 21
    }
130
131 1
    public function offsetSet($offset, $value)
132
    {
133 1
        $this->set($offset, $value);
134 1
    }
135
136
    public function offsetUnset($offset)
137
    {
138
        unset($this->values[$offset]);
139
    }
140
141
    public function load()
142
    {
143
        $values = Httpie::get($this->get('master_url') . '/load')
144
            ->body([
145
                'host' => $this->get('alias'),
146
            ])
147
            ->getJson();
148
        $this->update($values);
149
    }
150
151
    public function save()
152
    {
153
        Httpie::get($this->get('master_url') . '/save')
154
            ->body([
155
                'host' => $this->get('alias'),
156 5
                'config' => $this->persist(),
157
            ])
158 5
            ->getJson();
159 5
    }
160 5
161
    public function persist()
162 5
    {
163 5
        $values = [];
164 5
        if ($this->parent !== null) {
165
            $values = $this->parent->persist();
166 5
        }
167
        foreach ($this->values as $key => $value) {
168 5
            if (is_closure($value)) {
169
                continue;
170
            }
171
            $values[$key] = $value;
172
        }
173
        return $values;
174
    }
175
}
176