Passed
Push — master ( 87ba60...d80a49 )
by Alain
02:43
created

Locations   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 48
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 8 2
A hasLocation() 0 15 2
1
<?php
2
/**
3
 * Bright Nucleus View Component.
4
 *
5
 * @package   BrightNucleus\View
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\View\Location;
13
14
use BrightNucleus\View\Exception\InvalidLocation;
15
use Doctrine\Common\Collections\ArrayCollection;
16
17
/**
18
 * Class Locations.
19
 *
20
 * @since   0.1.1
21
 *
22
 * @package BrightNucleus\View\Location
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
class Locations extends ArrayCollection
26
{
27
28
    /**
29
     * Adds a location at the end of the collection if it does not already exist.
30
     *
31
     * @param mixed $location The location to add.
32
     *
33
     * @return boolean Whether the location was added or not.
34
     */
35 31
    public function add($location)
36
    {
37 31
        if ($this->hasLocation($location)) {
38 13
            return false;
39
        }
40
41 19
        return parent::add($location);
42
    }
43
44
    /**
45
     * Check whether a given location is already registered.
46
     *
47
     * For two locations to be equal, both their path and their extensions must be the same.
48
     *
49
     * @since 0.1.1
50
     *
51
     * @param Location $location Location to check the existence of.
52
     *
53
     * @return bool Whether the location is already registered or not.
54
     *
55
     * @throws InvalidLocation If the location is not valid.
56
     */
57 31
    public function hasLocation($location)
58
    {
59 31
        if ( ! $location instanceof Location) {
60
            throw new InvalidLocation(
61
                sprintf(
62
                    _('Invalid location to check existence for: "%s".'),
63
                    serialize($location)
64
                )
65
            );
66
        }
67
68 31
        return $this->exists(function ($key, $element) use ($location) {
69 15
            return $location == $element;
70 31
        });
71
    }
72
}
73