Completed
Push — master ( 05aecd...a61eba )
by Daniel
03:35
created

MethodCollection::addDefaults()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Routing;
4
5
use Symfony\Component\Config\Resource\ResourceInterface;
6
7
class MethodCollection implements \IteratorAggregate, \Countable
8
{
9
    private $methods = [];
10
    private  $resources = [];
11
12
    public function getIterator()
13
    {
14
        return new \ArrayIterator($this->methods);
15
    }
16
17
    public function count()
18
    {
19
        return count($this->methods);
20
    }
21
22
    public function add($name, Method $method)
23
    {
24
        unset($this->methods[$name]);
25
26
        $this->methods[$name] = $method;
27
    }
28
29
    public function all()
30
    {
31
        return $this->methods;
32
    }
33
34
35
    public function get($name)
36
    {
37
        if (isset($this->methods[$name])) {
38
            return $this->methods[$name];
39
        }
40
41
        return null;
42
    }
43
44
    /**
45
     * Removes a route or an array of routes by name from the collection.
46
     *
47
     * @param string|array $name The route name or an array of route names
48
     */
49
    public function remove($name)
50
    {
51
        foreach ((array) $name as $n) {
52
            unset($this->methods[$n]);
53
        }
54
    }
55
56
    public function addCollection(MethodCollection $collection)
57
    {
58
        // we need to remove all routes with the same names first because just replacing them
59
        // would not place the new route at the end of the merged array
60
        foreach ($collection->all() as $name => $route) {
61
            unset($this->methods[$name]);
62
            $this->methods[$name] = $route;
63
        }
64
65
        $this->resources = array_merge($this->resources, $collection->getResources());
66
    }
67
68
    public function getResources()
69
    {
70
        return array_unique($this->resources);
71
    }
72
73
    public function addResource(ResourceInterface $resource)
74
    {
75
        $this->resources[] = $resource;
76
    }
77
78
    public function addDefaults(array $defaults)
79
    {
80
        if ($defaults) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $defaults of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
            /**
82
             * @var Method $method
83
             */
84
            foreach ($this->methods as $method) {
85
                $method->addDefaults($defaults);
86
            }
87
        }
88
    }
89
}