Repository::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Magister\Services\Config;
4
5
use Magister\Services\Contracts\Config\Repository as ConfigContract;
6
use Magister\Services\Support\NamespacedItemResolver;
7
8
/**
9
 * Class Repository.
10
 */
11
class Repository extends NamespacedItemResolver implements \ArrayAccess, ConfigContract
12
{
13
    /**
14
     * The loader implementation.
15
     *
16
     * @var \Magister\Services\Config\LoaderInterface
17
     */
18
    protected $loader;
19
20
    /**
21
     * All of the configuration items.
22
     *
23
     * @var array
24
     */
25
    protected $items = [];
26
27
    /**
28
     * Create a new configuration repository.
29
     *
30
     * @param \Magister\Services\Config\LoaderInterface $loader
31
     */
32
    public function __construct(LoaderInterface $loader)
33
    {
34
        $this->loader = $loader;
35
    }
36
37
    /**
38
     * Determine if the given configuration value exists.
39
     *
40
     * @param string $key
41
     *
42
     * @return bool
43
     */
44
    public function has($key)
45
    {
46
        return $this->get($key) !== $key;
47
    }
48
49
    /**
50
     * Set a given configuration value.
51
     *
52
     * @param string $key
53
     * @param mixed  $value
54
     *
55
     * @return void
56
     */
57
    public function set($key, $value)
58
    {
59
        list($namespace, $group, $item) = $this->parseKey($key);
60
61
        $collection = $this->getCollection($group, $namespace);
62
63
        $this->load($group, $namespace, $collection);
64
65
        if (is_null($item)) {
66
            $this->items[$collection] = $value;
67
        } else {
68
            array_set($this->items[$collection], $item, $value);
69
        }
70
    }
71
72
    /**
73
     * Get the specified configuration value.
74
     *
75
     * @param string $key
76
     * @param array  $replace
77
     * @param mixed  $default
78
     *
79
     * @return mixed
80
     */
81
    public function get($key, array $replace = [], $default = null)
82
    {
83
        list($namespace, $group, $item) = $this->parseKey($key);
84
85
        $collection = $this->getCollection($group, $namespace);
86
87
        $this->load($group, $namespace, $collection);
88
89
        $line = $this->getLine(
90
            $collection, $item, $replace, $default
91
        );
92
93
        if (is_null($line)) {
94
            return $key;
95
        }
96
97
        return $line;
98
    }
99
100
    /**
101
     * Replace all occurrences of a string in a group.
102
     *
103
     * @param string $key
104
     * @param string $search
105
     * @param mixed  $replace
106
     *
107
     * @return void
108
     */
109
    public function replace($key, $search, $replace)
110
    {
111
        list($namespace, $group) = $this->parseKey($key);
112
113
        $collection = $this->getCollection($group, $namespace);
114
115
        $this->load($group, $namespace, $collection);
116
117
        $items = [];
118
119
        foreach ($this->items[$collection] as $key => $value) {
120
            $items[$key] = str_replace(':'.$search, $replace, $value);
121
        }
122
123
        $this->items[$collection] = $items;
124
    }
125
126
    /**
127
     * Load the configuration group for the key.
128
     *
129
     * @param string $group
130
     * @param string $namespace
131
     * @param string $collection
132
     *
133
     * @return void
134
     */
135
    protected function load($group, $namespace, $collection)
136
    {
137
        if (isset($this->items[$collection])) {
138
            return;
139
        }
140
141
        $items = $this->loader->load($group, $namespace);
142
143
        $this->items[$collection] = $items;
144
    }
145
146
    /**
147
     * Retrieve a language line out the loaded array.
148
     *
149
     * @param string $collection
150
     * @param string $item
151
     * @param array  $replace
152
     * @param mixed  $default
153
     *
154
     * @return string|null
155
     */
156
    protected function getLine($collection, $item, array $replace, $default = null)
157
    {
158
        $line = array_get($this->items[$collection], $item, $default);
159
160
        if (is_string($line) || is_bool($line)) {
161
            return $this->makeReplacements($line, $replace);
0 ignored issues
show
Bug introduced by
It seems like $line defined by array_get($this->items[$...tion], $item, $default) on line 158 can also be of type boolean; however, Magister\Services\Config...ory::makeReplacements() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
162
        } elseif (is_array($line) && count($line) > 0) {
163
            return $line;
164
        }
165
    }
166
167
    /**
168
     * Make the place-holder replacements on a line.
169
     *
170
     * @param string $line
171
     * @param array  $replace
172
     *
173
     * @return string
174
     */
175
    protected function makeReplacements($line, array $replace)
176
    {
177
        foreach ($replace as $key => $value) {
178
            $line = str_replace(':'.$key, $value, $line);
179
        }
180
181
        return $line;
182
    }
183
184
    /**
185
     * Get the collection identifier.
186
     *
187
     * @param string $group
188
     * @param string $namespace
189
     *
190
     * @return string
191
     */
192
    protected function getCollection($group, $namespace = null)
193
    {
194
        $namespace = $namespace ?: '*';
195
196
        return $namespace.'::'.$group;
197
    }
198
199
    /**
200
     * Get the loader implementation.
201
     *
202
     * @return \Magister\Services\Config\LoaderInterface
203
     */
204
    public function getLoader()
205
    {
206
        return $this->loader;
207
    }
208
209
    /**
210
     * Set the loader implementation.
211
     *
212
     * @param \Magister\Services\Config\LoaderInterface $loader
213
     *
214
     * @return void
215
     */
216
    public function setLoader(LoaderInterface $loader)
217
    {
218
        $this->loader = $loader;
219
    }
220
221
    /**
222
     * Get all of the configuration items for the application.
223
     *
224
     * @return array
225
     */
226
    public function all()
227
    {
228
        return $this->items;
229
    }
230
231
    /**
232
     * Set a configuration option.
233
     *
234
     * @param string $key
235
     * @param mixed  $value
236
     *
237
     * @return void
238
     */
239
    public function offsetSet($key, $value)
240
    {
241
        $this->set($key, $value);
242
    }
243
244
    /**
245
     * Get a configuration option.
246
     *
247
     * @param string $key
248
     *
249
     * @return mixed
250
     */
251
    public function offsetGet($key)
252
    {
253
        return $this->get($key);
254
    }
255
256
    /**
257
     * Determine if the given configuration option exists.
258
     *
259
     * @param string $key
260
     *
261
     * @return bool
262
     */
263
    public function offsetExists($key)
264
    {
265
        return $this->has($key);
266
    }
267
268
    /**
269
     * Unset a configuration option.
270
     *
271
     * @param string $key
272
     *
273
     * @return void
274
     */
275
    public function offsetUnset($key)
276
    {
277
        $this->set($key, null);
278
    }
279
}
280