Completed
Push — master ( 284085...99e334 )
by Fumio
02:01
created

NamespacedRepository::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
crap 2.0078
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel\Repository;
4
5
use ArrayAccess;
6
use Illuminate\Support\NamespacedItemResolver;
7
8
class NamespacedRepository extends NamespacedItemResolver implements ArrayAccess
9
{
10
    /**
11
     * The loader implementation.
12
     *
13
     * @var \LaravelPlus\Extension\Repository\LoaderInterface
14
     */
15
    protected $loader;
16
17
    /**
18
     * All of the configuration items.
19
     *
20
     * @var array
21
     */
22
    protected $items = [];
23
24
    /**
25
     * Create a new configuration repository.
26
     *
27
     * @param \LaravelPlus\Extension\Repository\LoaderInterface $loader
28
     */
29 6
    public function __construct(LoaderInterface $loader)
30
    {
31 6
        $this->loader = $loader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $loader of type object<Jumilla\Addomnipo...sitory\LoaderInterface> is incompatible with the declared type object<LaravelPlus\Exten...sitory\LoaderInterface> of property $loader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32 6
    }
33
34
    /**
35
     * Determine if the given configuration value exists.
36
     *
37
     * @param string $key
38
     *
39
     * @return bool
40
     */
41 1
    public function has($key)
42
    {
43 1
        $default = microtime(true);
44
45 1
        return $this->get($key, $default) !== $default;
46
    }
47
48
    /**
49
     * Determine if a configuration group exists.
50
     *
51
     * @param string $key
52
     *
53
     * @return bool
54
     */
55 1
    public function hasGroup($key)
56
    {
57 1
        list($namespace, $group, $item) = $this->parseKey($key);
0 ignored issues
show
Unused Code introduced by
The assignment to $item is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
58
59 1
        return $this->loader->exists($group, $namespace);
60
    }
61
62
    /**
63
     * Get the specified configuration value.
64
     *
65
     * @param string $key
66
     * @param mixed  $default
67
     *
68
     * @return mixed
69
     */
70 5
    public function get($key, $default = null)
71
    {
72 5
        list($namespace, $group, $item) = $this->parseKey($key);
73
74
        // Configuration items are actually keyed by "collection", which is simply a
75
        // combination of each namespace and groups, which allows a unique way to
76
        // identify the arrays of configuration items for the particular files.
77 5
        $collection = $this->getCollection($group, $namespace);
78
79 5
        $this->load($group, $namespace, $collection);
80
81 5
        return array_get($this->items[$collection], $item, $default);
82
    }
83
84
    /**
85
     * Set a given configuration value.
86
     *
87
     * @param string $key
88
     * @param mixed  $value
89
     */
90 1
    public function set($key, $value)
91
    {
92 1
        list($namespace, $group, $item) = $this->parseKey($key);
93
94 1
        $collection = $this->getCollection($group, $namespace);
95
96
        // We'll need to go ahead and lazy load each configuration groups even when
97
        // we're just setting a configuration item so that the set item does not
98
        // get overwritten if a different item in the group is requested later.
99 1
        $this->load($group, $namespace, $collection);
100
101 1
        if (is_null($item)) {
102
            $this->items[$collection] = $value;
103
        } else {
104 1
            array_set($this->items[$collection], $item, $value);
105
        }
106 1
    }
107
108
    /**
109
     * Load the configuration group for the key.
110
     *
111
     * @param string $group
112
     * @param string $namespace
113
     * @param string $collection
114
     */
115 5
    protected function load($group, $namespace, $collection)
116
    {
117
        // If we've already loaded this collection, we will just bail out since we do
118
        // not want to load it again. Once items are loaded a first time they will
119
        // stay kept in memory within this class and not loaded from disk again.
120 5
        if (isset($this->items[$collection])) {
121 4
            return;
122
        }
123
124 5
        $items = $this->loader->load($group, $namespace);
125
126 5
        $this->items[$collection] = $items;
127 5
    }
128
129
    /**
130
     * Get the configuration namespace for a package.
131
     *
132
     * @param string $package
133
     * @param string $namespace
134
     *
135
     * @return string
136
     */
137
    protected function getPackageNamespace($package, $namespace)
138
    {
139
        if (is_null($namespace)) {
140
            list($vendor, $namespace) = explode('/', $package);
0 ignored issues
show
Unused Code introduced by
The assignment to $vendor is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
141
        }
142
143
        return $namespace;
144
    }
145
146
    /**
147
     * Get the collection identifier.
148
     *
149
     * @param string $group
150
     * @param string $namespace
151
     *
152
     * @return string
153
     */
154 5
    protected function getCollection($group, $namespace = null)
155
    {
156 5
        $namespace = $namespace ?: '*';
157
158 5
        return $namespace.'::'.$group;
159
    }
160
161
    /**
162
     * Add a new namespace to the loader.
163
     *
164
     * @param string $namespace
165
     * @param string $hint
166
     */
167
    public function addNamespace($namespace, $hint)
168
    {
169
        $this->loader->addNamespace($namespace, $hint);
170
    }
171
172
    /**
173
     * Returns all registered namespaces with the config
174
     * loader.
175
     *
176
     * @return array
177
     */
178
    public function getNamespaces()
179
    {
180
        return $this->loader->getNamespaces();
181
    }
182
183
    /**
184
     * Get the loader implementation.
185
     *
186
     * @return \LaravelPlus\Extension\Repository\LoaderInterface
187
     */
188 6
    public function getLoader()
189
    {
190 6
        return $this->loader;
191
    }
192
193
    /**
194
     * Set the loader implementation.
195
     *
196
     * @param \LaravelPlus\Extension\Repository\LoaderInterface $loader
197
     */
198
    public function setLoader(LoaderInterface $loader)
199
    {
200
        $this->loader = $loader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $loader of type object<Jumilla\Addomnipo...sitory\LoaderInterface> is incompatible with the declared type object<LaravelPlus\Exten...sitory\LoaderInterface> of property $loader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
201
    }
202
203
    /**
204
     * Get all of the configuration items.
205
     *
206
     * @return array
207
     */
208
    public function getItems()
209
    {
210
        return $this->items;
211
    }
212
213
    /**
214
     * Determine if the given configuration option exists.
215
     *
216
     * @param string $key
217
     *
218
     * @return bool
219
     */
220
    public function offsetExists($key)
221
    {
222
        return $this->has($key);
223
    }
224
225
    /**
226
     * Get a configuration option.
227
     *
228
     * @param string $key
229
     *
230
     * @return mixed
231
     */
232
    public function offsetGet($key)
233
    {
234
        return $this->get($key);
235
    }
236
237
    /**
238
     * Set a configuration option.
239
     *
240
     * @param string $key
241
     * @param mixed  $value
242
     */
243
    public function offsetSet($key, $value)
244
    {
245
        $this->set($key, $value);
246
    }
247
248
    /**
249
     * Unset a configuration option.
250
     *
251
     * @param string $key
252
     */
253
    public function offsetUnset($key)
254
    {
255
        $this->set($key, null);
256
    }
257
}
258