Completed
Push — master ( 1798cf...75b2e6 )
by Bohuslav
01:48
created

Collection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 137
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 1
A post() 0 6 1
A delete() 0 6 1
A put() 0 6 1
A any() 0 6 1
A createRoute() 0 6 1
A addRoute() 0 6 1
A getRoutes() 0 4 1
1
<?php
2
3
namespace Kambo\Router\Route;
4
5
use Kambo\Router\Enum\Method;
6
use Kambo\Router\Route\Route;
7
8
/**
9
 * A container for all defined routes.
10
 *
11
 * @author  Bohuslav Simek <[email protected]>
12
 * @license Apache-2.0
13
 * @package Kambo\Router\Route
14
 */
15
class Collection
16
{
17
    /**
18
     * Contains all routes
19
     *
20
     * @var array
21
     */
22
    private $routes = [];
23
24
    /**
25
     * Add route matched with GET method.
26
     * Shortcut for createRoute function with preset GET method.
27
     *
28
     * @param mixed $route   route definition
29
     * @param mixed $handler handler that will be executed if the url match
30
     *                       the route
31
     *
32
     * @return self for fluent interface
33
     */
34
    public function get($route, $handler)
35
    {
36
        $this->createRoute(Method::GET, $route, $handler);
37
38
        return $this;
39
    }
40
41
    /**
42
     * Add route matched with POST method.
43
     * Shortcut for createRoute function with preset POST method.
44
     *
45
     * @param mixed $route   route definition
46
     * @param mixed $handler handler that will be executed if the url match
47
     *                       the route
48
     *
49
     * @return self for fluent interface
50
     */
51
    public function post($route, $handler)
52
    {
53
        $this->createRoute(Method::POST, $route, $handler);
54
55
        return $this;
56
    }
57
58
    /**
59
     * Add route matched with DELETE method.
60
     * Shortcut for createRoute function with preset DELETE method.
61
     *
62
     * @param mixed $route   route definition
63
     * @param mixed $handler handler that will be executed if the url match
64
     *                       the route
65
     *
66
     * @return self for fluent interface
67
     */
68
    public function delete($route, $handler)
69
    {
70
        $this->createRoute(Method::DELETE, $route, $handler);
71
72
        return $this;
73
    }
74
75
    /**
76
     * Add route matched with PUT method.
77
     * Shortcut for createRoute function with preset PUT method.
78
     *
79
     * @param mixed $route   route definition
80
     * @param mixed $handler handler that will be executed if the url match
81
     *                       the route
82
     *
83
     * @return self for fluent interface
84
     */
85
    public function put($route, $handler)
86
    {
87
        $this->createRoute(Method::PUT, $route, $handler);
88
89
        return $this;
90
    }
91
92
    /**
93
     * Add route that will be matched to any method.
94
     * Shortcut for createRoute function with preset ANY method.
95
     *
96
     * @param mixed $route   route definition
97
     * @param mixed $handler handler that will be executed if the url match
98
     *                       the route
99
     *
100
     * @return self for fluent interface
101
     */
102
    public function any($route, $handler)
103
    {
104
        $this->createRoute(Method::ANY, $route, $handler);
105
106
        return $this;
107
    }
108
109
    /**
110
     * Create a route to the collection.
111
     * The data structure used in the $handler depends on the used dispatcher.
112
     *
113
     * @param mixed $method  HTTP method that will be used for binding
114
     * @param mixed $route   route definition
115
     * @param mixed $handler handler that will be executed if the
116
     *                       url matchs the route
117
     *
118
     * @return self for fluent interface
119
     */
120
    public function createRoute($method, $route, $handler)
121
    {
122
        $this->routes[] = new Route($method, $route, $handler);
123
124
        return $this;
125
    }
126
127
    /**
128
     * Add a route to the collection.
129
     *
130
     * @param Kambo\Router\Route\Route $route route that will be added into
131
     *                                        collection
132
     *
133
     * @return self for fluent interface
134
     */
135
    public function addRoute(Route $route)
136
    {
137
        $this->routes[] = $route;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get all defines routes in collection.
144
     *
145
     * @return Route[]
146
     */
147
    public function getRoutes()
148
    {
149
        return $this->routes;
150
    }
151
}
152