Passed
Pull Request — master (#9)
by Pavel
11:27
created

MethodCollection::addResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bankiru\Api\Rpc\Routing;
4
5
use Symfony\Component\Config\Resource\ResourceInterface;
6
7
class MethodCollection implements \IteratorAggregate, \Countable
8
{
9
    /** @var  Route[] */
10
    private $routes = [];
11
    /**
12
     * @var ResourceInterface[]
13
     */
14
    private $resources = [];
15
16
    /**
17
     * @param $method
18
     *
19
     * @return Route
20
     * @throws \OutOfBoundsException
21
     */
22 1
    public function get($method)
23
    {
24 1
        if (!$this->has($method)) {
25
            throw new \OutOfBoundsException();
26
        }
27
28 1
        return $this->routes[$method];
29
    }
30
31 2
    public function has($method)
32
    {
33 2
        return array_key_exists($method, $this->routes);
34
    }
35
36 2
    public function add($method, Route $route)
37
    {
38 2
        if ($this->has($method)) {
39
            throw new \LogicException(sprintf('Trying to replace method %s', $method));
40
        }
41
42 2
        $this->replace($method, $route);
43 2
    }
44
45 2
    public function replace($method, Route $route)
46
    {
47 2
        unset($this->routes[$method]);
48
49 2
        $this->routes[$method] = $route;
50 2
    }
51
52 2
    public function addPrefix($prefix)
53
    {
54 2
        if ('' === $prefix) {
55 2
            return;
56
        }
57
58 2
        foreach ($this->routes as $name => $route) {
59 2
            unset($this->routes[$name]);
60 2
            $method = $prefix . $route->getMethod();
61 2
            $route->setMethod($method);
62 2
            $this->routes[$method] = $route;
63 2
        }
64 2
    }
65
66 2
    public function addContext($context)
67
    {
68 2
        foreach ($this->routes as $route) {
69 2
            if ($route->inheritContext()) {
70 2
                $route->addContext($context);
71 2
            }
72 2
        }
73 2
    }
74
75 2
    public function addCollection(MethodCollection $collection)
76
    {
77
        // we need to remove all routes with the same names first because just replacing them
78
        // would not place the new route at the end of the merged array
79 2
        foreach ($collection->all() as $name => $route) {
80 2
            unset($this->routes[$name]);
81 2
            $this->routes[$name] = $route;
82 2
        }
83
84 2
        $this->resources = array_merge($this->resources, $collection->getResources());
85 2
    }
86
87 2
    public function all()
88
    {
89 2
        return $this->routes;
90
    }
91
92
    /**
93
     * Returns an array of resources loaded to build this collection.
94
     *
95
     * @return ResourceInterface[] An array of resources
96
     */
97 2
    public function getResources()
98
    {
99 2
        return array_unique($this->resources);
100
    }
101
102
    /** {@inheritdoc} */
103
    public function getIterator()
104
    {
105
        return new \ArrayIterator($this->routes);
106
    }
107
108
    /** {@inheritdoc} */
109
    public function count()
110
    {
111
        return count($this->routes);
112
    }
113
114
    /**
115
     * @param ResourceInterface $resource
116
     */
117 2
    public function addResource(ResourceInterface $resource)
118
    {
119 2
        $this->resources[] = $resource;
120 2
    }
121
}
122