Completed
Push — master ( 6343d0...45320b )
by Bernhard
06:10
created

ModuleList::replace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Api\Module;
13
14
use ArrayAccess;
15
use ArrayIterator;
16
use Countable;
17
use IteratorAggregate;
18
19
/**
20
 * A collection of Puli modules.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class ModuleList implements IteratorAggregate, Countable, ArrayAccess
27
{
28
    /**
29
     * @var RootModule
30
     */
31
    private $rootModule;
32
33
    /**
34
     * @var Module[]
35
     */
36
    private $modules = array();
37
38 372
    public function __construct(array $modules = array())
39
    {
40 372
        $this->merge($modules);
41 372
    }
42
43
    /**
44
     * Adds a module to the collection.
45
     *
46
     * @param Module $module The added module.
47
     */
48 369
    public function add(Module $module)
49
    {
50 369
        $this->modules[$module->getName()] = $module;
51
52 369
        if ($module instanceof RootModule) {
53 291
            $this->rootModule = $module;
54
        }
55 369
    }
56
57
    /**
58
     * Adds multiple modules to the collection.
59
     *
60
     * @param Module[] $modules The added modules.
61
     */
62 372
    public function merge(array $modules)
63
    {
64 372
        foreach ($modules as $module) {
65 112
            $this->add($module);
66
        }
67 372
    }
68
69
    /**
70
     * Replaces the collection with the given modules.
71
     *
72
     * @param Module[] $modules The modules to set.
73
     */
74 2
    public function replace(array $modules)
75
    {
76 2
        $this->clear();
77 2
        $this->merge($modules);
78 2
    }
79
80
    /**
81
     * Removes a module from the collection.
82
     *
83
     * @param string $name The module name.
84
     */
85 12
    public function remove($name)
86
    {
87 12
        if ($this->rootModule && $name === $this->rootModule->getName()) {
88 2
            $this->rootModule = null;
89
        }
90
91 12
        unset($this->modules[$name]);
92 12
    }
93
94
    /**
95
     * Removes all modules from the collection.
96
     */
97 3
    public function clear()
98
    {
99 3
        if ($this->rootModule) {
100 2
            $this->rootModule = null;
101
        }
102
103 3
        $this->modules = array();
104 3
    }
105
106
    /**
107
     * Returns the module with the given name.
108
     *
109
     * @param string $name The module name.
110
     *
111
     * @return Module The module with the passed name.
112
     *
113
     * @throws NoSuchModuleException If the module was not found.
114
     */
115 51
    public function get($name)
116
    {
117 51
        if (!isset($this->modules[$name])) {
118 2
            throw new NoSuchModuleException(sprintf(
119 2
                'The module "%s" was not found.',
120
                $name
121
            ));
122
        }
123
124 49
        return $this->modules[$name];
125
    }
126
127
    /**
128
     * Returns whether a module with the given name exists.
129
     *
130
     * @param string $name The module name.
131
     *
132
     * @return bool Whether a module with this name exists.
133
     */
134 34
    public function contains($name)
135
    {
136 34
        return isset($this->modules[$name]);
137
    }
138
139
    /**
140
     * Returns the root module.
141
     *
142
     * If the collection contains no root module, `null` is returned.
143
     *
144
     * @return RootModule|null The root module or `null` if none exists.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use RootModule.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
145
     */
146 270
    public function getRootModule()
147
    {
148 270
        return $this->rootModule;
149
    }
150
151
    /**
152
     * Returns the name of the root module.
153
     *
154
     * If the collection contains no root module, `null` is returned.
155
     *
156
     * @return string|null The root module name or `null` if none exists.
157
     */
158 2
    public function getRootModuleName()
159
    {
160 2
        return $this->rootModule ? $this->rootModule->getName() : null;
161
    }
162
163
    /**
164
     * Returns all installed modules.
165
     *
166
     * The installed modules are all modules that are not the root module.
167
     *
168
     * @return Module[] The installed modules indexed by their names.
169
     */
170 6
    public function getInstalledModules()
171
    {
172 6
        $modules = $this->modules;
173
174 6
        if ($this->rootModule) {
175 4
            unset($modules[$this->rootModule->getName()]);
176
        }
177
178 6
        return $modules;
179
    }
180
181
    /**
182
     * Returns the names of all installed modules.
183
     *
184
     * The installed modules are all modules that are not the root module.
185
     *
186
     * @return string[] The names of the installed modules.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
187
     */
188 1
    public function getInstalledModuleNames()
189
    {
190 1
        return array_keys($this->getInstalledModules());
191
    }
192
193
    /**
194
     * Returns the names of all modules.
195
     *
196
     * @return string[] The module names.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
197
     */
198 93
    public function getModuleNames()
199
    {
200 93
        return array_keys($this->modules);
201
    }
202
203
    /**
204
     * Returns the modules in the collection.
205
     *
206
     * @return Module[] The modules in the collection.
207
     */
208 6
    public function toArray()
209
    {
210 6
        return $this->modules;
211
    }
212
213
    /**
214
     * Returns whether the collection is empty.
215
     *
216
     * @return bool Returns `true` if the collection is empty and `false`
217
     *              otherwise.
218
     */
219 2
    public function isEmpty()
220
    {
221 2
        return 0 === count($this->modules);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 268
    public function getIterator()
228
    {
229 268
        return new ArrayIterator($this->modules);
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 33
    public function count()
236
    {
237 33
        return count($this->modules);
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243 1
    public function offsetExists($name)
244
    {
245 1
        return $this->contains($name);
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251 6
    public function offsetGet($name)
252
    {
253 6
        return $this->get($name);
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259 1
    public function offsetSet($name, $module)
260
    {
261 1
        $this->add($module);
262 1
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 1
    public function offsetUnset($name)
268
    {
269 1
        $this->remove($name);
270 1
    }
271
}
272