Test Failed
Push — master ( c0baca...92e477 )
by Bohuslav
10:33
created

Collection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Kambo\Router\Route;
4
5
// spl
6
use IteratorAggregate;
7
use ArrayIterator;
8
9
// Kambo\Router
10
use Kambo\Router\Enum\Method;
11
use Kambo\Router\Route\Route;
12
13
/**
14
 * Collection of all defined routes.
15
 *
16
 * @author  Bohuslav Simek <[email protected]>
17
 * @license Apache-2.0
18
 * @package Kambo\Router\Route
19
 */
20
class Collection implements IteratorAggregate
21
{
22
    /**
23
     * Contains all routes
24
     *
25
     * @var array
26
     */
27
    private $routes = [];
28
29
    /**
30
     * Contains all routes
31
     *
32
     * @var array
33
     */
34 18
    private $routeBuilder;
35
36 18
    /**
37
     * Route constructor
38 18
     *
39
     * @param String $method
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     * @param String $url
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
41
     * @param Mixed  $handler
0 ignored issues
show
Bug introduced by
There is no parameter named $handler. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
42
     */
43
    public function __construct($routeBuilder)
44
    {
45
        $this->routeBuilder = $routeBuilder;
46
    }
47
48
    /**
49
     *
50
     * IteratorAggregate: returns the iterator object.
51 2
     *
52
     * @return ArrayIterator
53 2
     *
54
     */
55 2
    public function getIterator()
56
    {
57
        return new ArrayIterator($this->routes);
58
    }
59
60
    /**
61
     * Add route matched with GET method.
62
     * Shortcut for createRoute function with preset GET method.
63
     *
64
     * @param mixed $route   route definition
65
     * @param mixed $handler handler which will be executed if the url match
66
     *                       the route
67
     *
68 1
     * @return Kambo\Router\Route\Route Created route
69
     */
70 1
    public function get($route, $handler)
71
    {
72 1
        return $this->createRoute(Method::GET, $route, $handler);
73
    }
74
75
    /**
76
     * Add route matched with POST method.
77
     * Shortcut for createRoute function with preset POST method.
78
     *
79
     * @param mixed $route   route definition
80
     * @param mixed $handler handler which will be executed if the url match
81
     *                       the route
82
     *
83
     * @return Kambo\Router\Route\Route Created route
84
     */
85 1
    public function post($route, $handler)
86
    {
87 1
        return $this->createRoute(Method::POST, $route, $handler);
88
    }
89 1
90
    /**
91
     * Add route matched with DELETE method.
92
     * Shortcut for createRoute function with preset DELETE method.
93
     *
94
     * @param mixed $route   route definition
95
     * @param mixed $handler handler which will be executed if the url match
96
     *                       the route
97
     *
98
     * @return Kambo\Router\Route\Route Created route
99
     */
100
    public function delete($route, $handler)
101
    {
102 2
        return $this->createRoute(Method::DELETE, $route, $handler);
103
    }
104 2
105
    /**
106 2
     * Add route matched with PUT method.
107
     * Shortcut for createRoute function with preset PUT method.
108
     *
109
     * @param mixed $route   route definition
110
     * @param mixed $handler handler which will be executed if the url match
111
     *                       the route
112
     *
113
     * @return Kambo\Router\Route\Route Created route
114
     */
115
    public function put($route, $handler)
116
    {
117
        return $this->createRoute(Method::PUT, $route, $handler);
118
    }
119
120 24
    /**
121
     * Add route which will be matched to any method.
122 24
     * Shortcut for createRoute function with preset ANY method.
123
     *
124 24
     * @param mixed $route   route definition
125
     * @param mixed $handler handler which will be executed if the url match
126
     *                       the route
127
     *
128
     * @return Kambo\Router\Route\Route Created route
129
     */
130
    public function any($route, $handler)
131
    {
132
        return $this->createRoute(Method::ANY, $route, $handler);
133
    }
134
135 1
    /**
136
     * Create a route in the collection.
137 1
     * The data structure used in the $handler depends on the used dispatcher.
138
     *
139 1
     * @param mixed $method  HTTP method which will be used for binding
140
     * @param mixed $route   route definition
141
     * @param mixed $handler handler which will be executed if the
142
     *                       url matchs the route
143
     *
144
     * @return Kambo\Router\Route\Route Created route
145
     */
146
    public function createRoute($method, $route, $handler)
147 27
    {
148
        $createdRoute   = $this->routeBuilder->build($method, $route, $handler);
0 ignored issues
show
Bug introduced by
The method build cannot be called on $this->routeBuilder (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
149 27
        $this->routes[] = $createdRoute;
150
151
        return $createdRoute;
152
    }
153
154
    /**
155
     * Add a route to the collection.
156
     *
157
     * @param Kambo\Router\Route\Route $route route which will be added into
158
     *                                        collection
159
     *
160
     * @return self for fluent interface
161
     */
162
    public function addRoute(Route $route)
163
    {
164
        $this->routes[] = $route;
165
166
        return $this;
167
    }
168
}
169