Failed Conditions
Pull Request — master (#56)
by Bernhard
11:07
created

ModuleFile::hasDependency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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 InvalidArgumentException;
15
use Puli\Discovery\Api\Binding\Binding;
16
use Puli\Manager\Api\Discovery\BindingDescriptor;
17
use Puli\Manager\Api\Discovery\BindingTypeDescriptor;
18
use Puli\Manager\Api\Discovery\NoSuchBindingException;
19
use Puli\Manager\Api\Repository\NoSuchPathMappingException;
20
use Puli\Manager\Api\Repository\PathMapping;
21
use Puli\Manager\Assert\Assert;
22
23
/**
24
 * Stores the configuration of a module.
25
 *
26
 * @since  1.0
27
 *
28
 * @author Bernhard Schussek <[email protected]>
29
 */
30
class ModuleFile
31
{
32
    /**
33
     * @var string
34
     */
35
    private $version;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $moduleName;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $path;
46
47
    /**
48
     * @var PathMapping[]
49
     */
50
    private $pathMappings = array();
51
52
    /**
53
     * @var BindingDescriptor[]
54
     */
55
    private $bindingDescriptors = array();
56
57
    /**
58
     * @var BindingTypeDescriptor[]
59
     */
60
    private $typeDescriptors = array();
61
62
    /**
63
     * @var bool[]
64
     */
65
    private $dependencies = array();
66
67
    /**
68
     * @var array
69
     */
70
    private $extra = array();
71
72
    /**
73
     * Creates a new module file.
74
     *
75
     * @param string|null $moduleName The module name. Optional.
76
     * @param string|null $path       The path where the file is stored or
77
     *                                `null` if this configuration is not
78
     *                                stored on the file system.
79
     *
80
     * @throws InvalidArgumentException If the name/path is not a string or empty.
81
     */
82 612
    public function __construct($moduleName = null, $path = null)
83
    {
84 612
        Assert::nullOrModuleName($moduleName);
85 610
        Assert::nullOrAbsoluteSystemPath($path);
86
87 608
        $this->path = $path;
88 608
        $this->moduleName = $moduleName;
89 608
    }
90
91
    /**
92
     * Returns the version of the module file.
93
     *
94
     * @return string The module file version.
95
     */
96 11
    public function getVersion()
97
    {
98 11
        return $this->version;
99
    }
100
101
    /**
102
     * Sets the version of the module file.
103
     *
104
     * @param string $version The module file version.
105
     */
106 47
    public function setVersion($version)
107
    {
108 47
        Assert::string($version, 'The module file version must be a string. Got: %s');
109 47
        Assert::regex($version, '~^\d\.\d$~', 'The module file version must have the format "<digit>.<digit>". Got: %s</digit>');
110
111 47
        $this->version = $version;
112 47
    }
113
114
    /**
115
     * Returns the module name.
116
     *
117
     * @return string|null The module name or `null` if none is set.
118
     */
119 435
    public function getModuleName()
120
    {
121 435
        return $this->moduleName;
122
    }
123
124
    /**
125
     * Sets the module name.
126
     *
127
     * @param string|null $moduleName The module name or `null` to unset.
128
     *
129
     * @throws InvalidArgumentException If the name is not a string or empty.
130
     */
131 40
    public function setModuleName($moduleName)
132
    {
133 40
        Assert::nullOrModuleName($moduleName);
134
135 38
        $this->moduleName = $moduleName;
136 38
    }
137
138
    /**
139
     * Returns the path to the module file.
140
     *
141
     * @return string|null The path or `null` if this file is not stored on the
142
     *                     file system.
143
     */
144 23
    public function getPath()
145
    {
146 23
        return $this->path;
147
    }
148
149
    /**
150
     * Sets the names of the modules this module depends on.
151
     *
152
     * @param string[] $moduleNames The names of the modules.
153
     */
154 48
    public function setDependencies(array $moduleNames)
155
    {
156 48
        $this->dependencies = array();
157
158 48
        foreach ($moduleNames as $moduleName) {
159 48
            $this->dependencies[$moduleName] = true;
160
        }
161 48
    }
162
163
    /**
164
     * Adds a dependency to a module.
165
     *
166
     * @param string $moduleName The name of the module.
167
     */
168 10
    public function addDependency($moduleName)
169
    {
170 10
        $this->dependencies[$moduleName] = true;
171 10
    }
172
173
    /**
174
     * Adds a dependency to a module.
175
     *
176
     * @param string $moduleName The name of the module.
177
     */
178 2
    public function removeDependency($moduleName)
179
    {
180 2
        unset($this->dependencies[$moduleName]);
181 2
    }
182
183
    /**
184
     * Removes all dependencies.
185
     */
186
    public function clearDependencies()
187
    {
188
        $this->dependencies = array();
189
    }
190
191
    /**
192
     * Returns the names of the modules this module overrides.
193
     *
194
     * @return string[] The names of the overridden 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...
195
     */
196 106
    public function getDependencies()
197
    {
198 106
        return array_keys($this->dependencies);
199
    }
200
201
    /**
202
     * Returns whether the module depends on a given module.
203
     *
204
     * @param string $moduleName The name of the module.
205
     *
206
     * @return bool Returns `true` if the module depends on the given module.
207
     */
208 7
    public function hasDependency($moduleName)
209
    {
210 7
        return isset($this->dependencies[$moduleName]);
211
    }
212
213
    /**
214
     * Returns whether the module has dependencies on other modules.
215
     *
216
     * @return bool Returns `true` if the module depends on other modules and
217
     *              `false` otherwise.
218
     */
219
    public function hasDependencies()
220
    {
221
        return count($this->dependencies) > 0;
222
    }
223
224
    /**
225
     * Returns the path mappings.
226
     *
227
     * @return PathMapping[] The path mappings.
228
     */
229 75
    public function getPathMappings()
230
    {
231 75
        return $this->pathMappings;
232
    }
233
234
    /**
235
     * Returns the path mapping for a repository path.
236
     *
237
     * @param string $repositoryPath The repository path.
238
     *
239
     * @return PathMapping The corresponding path mapping.
240
     *
241
     * @throws NoSuchPathMappingException If the repository path is not mapped.
242
     */
243 15
    public function getPathMapping($repositoryPath)
244
    {
245 15
        if (!isset($this->pathMappings[$repositoryPath])) {
246 1
            throw NoSuchPathMappingException::forRepositoryPath($repositoryPath);
247
        }
248
249 14
        return $this->pathMappings[$repositoryPath];
250
    }
251
252
    /**
253
     * Returns whether the file contains a path mapping for a repository path.
254
     *
255
     * @param string $repositoryPath The repository path.
256
     *
257
     * @return bool Returns `true` if the file contains a mapping for the path.
258
     */
259 30
    public function hasPathMapping($repositoryPath)
260
    {
261 30
        return isset($this->pathMappings[$repositoryPath]);
262
    }
263
264
    /**
265
     * Returns whether the file contains any path mappings.
266
     *
267
     * @return bool Returns `true` if the file contains path mappings and
268
     *              `false` otherwise.
269
     */
270 1
    public function hasPathMappings()
271
    {
272 1
        return count($this->pathMappings) > 0;
273
    }
274
275
    /**
276
     * Adds a path mapping.
277
     *
278
     * @param PathMapping $mapping The path mapping.
279
     */
280 65
    public function addPathMapping(PathMapping $mapping)
281
    {
282 65
        $this->pathMappings[$mapping->getRepositoryPath()] = $mapping;
283
284 65
        ksort($this->pathMappings);
285 65
    }
286
287
    /**
288
     * Removes the path mapping for a repository path.
289
     *
290
     * @param string $repositoryPath The repository path.
291
     */
292 16
    public function removePathMapping($repositoryPath)
293
    {
294 16
        unset($this->pathMappings[$repositoryPath]);
295 16
    }
296
297
    /**
298
     * Removes all path mappings.
299
     */
300
    public function clearPathMappings()
301
    {
302
        $this->pathMappings = array();
303
    }
304
305
    /**
306
     * Returns the binding descriptors.
307
     *
308
     * @return BindingDescriptor[] The binding descriptors.
309
     */
310 72
    public function getBindingDescriptors()
311
    {
312 72
        return array_values($this->bindingDescriptors);
313
    }
314
315
    /**
316
     * Returns the binding descriptor with the given UUID.
317
     *
318
     * @param Binding $binding The UUID of the binding descriptor.
319
     *
320
     * @return BindingDescriptor The binding descriptor.
321
     *
322
     * @throws NoSuchBindingException If the UUID was not found.
323
     */
324 1
    public function getBindingDescriptor(Binding $binding)
325
    {
326 1
        foreach ($this->bindingDescriptors as $bindingDescriptor) {
327 1
            if ($binding->equals($bindingDescriptor->getBinding())) {
328 1
                return $bindingDescriptor;
329
            }
330
        }
331
332
        throw NoSuchBindingException::forBinding($binding);
333
    }
334
335
    /**
336
     * Adds a binding descriptor.
337
     *
338
     * @param BindingDescriptor $descriptor The binding descriptor to add.
339
     */
340 51
    public function addBindingDescriptor(BindingDescriptor $descriptor)
341
    {
342
        // Prevent duplicates
343 51
        $this->removeBindingDescriptor($descriptor->getBinding());
344
345 51
        $this->bindingDescriptors[] = $descriptor;
346 51
    }
347
348
    /**
349
     * Removes a binding descriptor.
350
     *
351
     * @param Binding $binding The UUID of the binding descriptor to remove.
352
     */
353 51
    public function removeBindingDescriptor(Binding $binding)
354
    {
355 51
        foreach ($this->bindingDescriptors as $key => $bindingDescriptor) {
356 11
            if ($binding->equals($bindingDescriptor->getBinding())) {
357 11
                unset($this->bindingDescriptors[$key]);
358
            }
359
        }
360 51
    }
361
362
    /**
363
     * Removes all binding descriptors.
364
     */
365
    public function clearBindingDescriptors()
366
    {
367
        $this->bindingDescriptors = array();
368
    }
369
370
    /**
371
     * Returns whether the binding descriptor exists in this file.
372
     *
373
     * @param Binding $binding The UUID of the binding descriptor.
374
     *
375
     * @return bool Whether the file contains the binding descriptor.
376
     */
377 5
    public function hasBindingDescriptor(Binding $binding)
378
    {
379 5
        foreach ($this->bindingDescriptors as $bindingDescriptor) {
380 2
            if ($binding->equals($bindingDescriptor->getBinding())) {
381 2
                return true;
382
            }
383
        }
384
385 4
        return false;
386
    }
387
388
    /**
389
     * Returns whether the file contains any binding descriptors.
390
     *
391
     * @return bool Returns `true` if the file contains binding descriptors and
392
     *              `false` otherwise.
393
     */
394
    public function hasBindingDescriptors()
395
    {
396
        return count($this->bindingDescriptors) > 0;
397
    }
398
399
    /**
400
     * Adds a type descriptor.
401
     *
402
     * @param BindingTypeDescriptor $descriptor The type descriptor.
403
     */
404 71
    public function addTypeDescriptor(BindingTypeDescriptor $descriptor)
405
    {
406 71
        $this->typeDescriptors[$descriptor->getTypeName()] = $descriptor;
407 71
    }
408
409
    /**
410
     * Removes a type descriptor.
411
     *
412
     * @param string $typeName The type name.
413
     */
414 13
    public function removeTypeDescriptor($typeName)
415
    {
416 13
        unset($this->typeDescriptors[$typeName]);
417 13
    }
418
419
    /**
420
     * Removes all type descriptors.
421
     */
422
    public function clearTypeDescriptors()
423
    {
424
        $this->typeDescriptors = array();
425
    }
426
427
    /**
428
     * Returns the type descriptor with the given name.
429
     *
430
     * @param string $typeName The type name.
431
     *
432
     * @return BindingTypeDescriptor The type descriptor.
433
     */
434 13
    public function getTypeDescriptor($typeName)
435
    {
436 13
        return $this->typeDescriptors[$typeName];
437
    }
438
439
    /**
440
     * Returns the type descriptors.
441
     *
442
     * @return BindingTypeDescriptor[] The type descriptors.
443
     */
444 69
    public function getTypeDescriptors()
445
    {
446
        // Names as keys are for internal use only
447 69
        return array_values($this->typeDescriptors);
448
    }
449
450
    /**
451
     * Returns whether a type is defined in this file.
452
     *
453
     * @param string $typeName The type name.
454
     *
455
     * @return bool Whether the type is defined in the file.
456
     */
457 13
    public function hasTypeDescriptor($typeName)
458
    {
459 13
        return isset($this->typeDescriptors[$typeName]);
460
    }
461
462
    /**
463
     * Returns whether the file contains any type descriptors.
464
     *
465
     * @return bool Returns `true` if the file contains type descriptors and
466
     *              `false` otherwise.
467
     */
468
    public function hasTypeDescriptors()
469
    {
470
        return count($this->typeDescriptors) > 0;
471
    }
472
473
    /**
474
     * Sets an extra key in the module file.
475
     *
476
     * Extra keys can be freely set by the user. They are stored in a separate
477
     * area of the module file and not validated in any way.
478
     *
479
     * @param string $key   The name of the key.
480
     * @param mixed  $value The value to store.
481
     */
482 81
    public function setExtraKey($key, $value)
483
    {
484 81
        $this->extra[$key] = $value;
485 81
    }
486
487
    /**
488
     * Sets multiple extra keys at once.
489
     *
490
     * Existing extra keys are overridden.
491
     *
492
     * @param array $values The values indexed by their key names.
493
     *
494
     * @see setExtraKey()
495
     */
496 5
    public function setExtraKeys(array $values)
497
    {
498 5
        $this->extra = array();
499
500 5
        foreach ($values as $key => $value) {
501 5
            $this->setExtraKey($key, $value);
502
        }
503 5
    }
504
505
    /**
506
     * Sets multiple extra keys at once.
507
     *
508
     * Existing extra keys are preserved.
509
     *
510
     * @param array $values The values indexed by their key names.
511
     *
512
     * @see setExtraKey()
513
     */
514 1
    public function addExtraKeys(array $values)
515
    {
516 1
        foreach ($values as $key => $value) {
517 1
            $this->setExtraKey($key, $value);
518
        }
519 1
    }
520
521
    /**
522
     * Removes an extra key.
523
     *
524
     * @param string $key The name of the key.
525
     *
526
     * @see setExtraKey()
527
     */
528 7
    public function removeExtraKey($key)
529
    {
530 7
        unset($this->extra[$key]);
531 7
    }
532
533
    /**
534
     * Removes all extra keys.
535
     *
536
     * @see setExtraKey()
537
     */
538 4
    public function clearExtraKeys()
539
    {
540 4
        $this->extra = array();
541 4
    }
542
543
    /**
544
     * Returns the value of an extra key.
545
     *
546
     * @param string $key     The name of the key.
547
     * @param mixed  $default The value to return if the key was not set.
548
     *
549
     * @return mixed The value stored for the key.
550
     *
551
     * @see setExtraKey()
552
     */
553 96
    public function getExtraKey($key, $default = null)
554
    {
555 96
        return array_key_exists($key, $this->extra) ? $this->extra[$key] : $default;
556
    }
557
558
    /**
559
     * Returns all stored extra keys.
560
     *
561
     * @return array The stored values indexed by their key names.
562
     *
563
     * @see setExtraKey()
564
     */
565 24
    public function getExtraKeys()
566
    {
567 24
        return $this->extra;
568
    }
569
570
    /**
571
     * Returns whether the given extra key exists.
572
     *
573
     * @param string $key The name of the key.
574
     *
575
     * @return bool Returns `true` if the given extra key exists and `false`
576
     *              otherwise.
577
     *
578
     * @see setExtraKey()
579
     */
580 14
    public function hasExtraKey($key)
581
    {
582 14
        return array_key_exists($key, $this->extra);
583
    }
584
585
    /**
586
     * Returns whether the file contains any extra keys.
587
     *
588
     * @return bool Returns `true` if the file contains extra keys and `false`
589
     *              otherwise.
590
     *
591
     * @see setExtraKey()
592
     */
593 2
    public function hasExtraKeys()
594
    {
595 2
        return count($this->extra) > 0;
596
    }
597
}
598