Completed
Push — master ( 0513f3...46cdfb )
by Daniel
03:05
created

MethodCollection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 70
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 4 1
A count() 0 4 1
A add() 0 6 1
A all() 0 4 1
A get() 0 8 2
A remove() 0 6 2
A addCollection() 0 11 2
A getResources() 0 4 1
A addResource() 0 4 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Routing;
4
5
use IteratorAggregate;
6
use Symfony\Component\Config\Resource\ResourceInterface;
7
8
class MethodCollection implements IteratorAggregate, \Countable
9
{
10
    private $methods = [];
11
    private  $resources = [];
12
13
    public function getIterator()
14
    {
15
        return new \ArrayIterator($this->methods);
16
    }
17
18
    public function count()
19
    {
20
        return count($this->methods);
21
    }
22
23
    public function add($name, Method $method)
24
    {
25
        unset($this->methods[$name]);
26
27
        $this->methods[$name] = $method;
28
    }
29
30
    public function all()
31
    {
32
        return $this->methods;
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
}