Route   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A method() 0 3 1
A setAction() 0 6 2
A __construct() 0 10 1
A setRoute() 0 6 2
A setHttpMethod() 0 6 2
A setHandler() 0 6 2
A action() 0 3 1
A route() 0 3 1
A handler() 0 3 1
1
<?php
2
3
namespace Controlabs\Routify;
4
5
class Route
6
{
7
    /**
8
     * The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
9
     *
10
     * @type string
11
     */
12
    const GET = 'GET';
13
14
    /**
15
     * The HEAD method asks for a response identical to that of a GET request, but without the response body.
16
     *
17
     * @type string
18
     */
19
    const HEAD = 'HEAD';
20
21
    /**
22
     * The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.
23
     *
24
     * @type string
25
     */
26
    const POST = 'POST';
27
28
    /**
29
     * The PUT method replaces all current representations of the target resource with the request payload.
30
     *
31
     * @type string
32
     */
33
    const PUT = 'PUT';
34
35
    /**
36
     * The DELETE method deletes the specified resource.
37
     *
38
     * @type string
39
     */
40
    const DELETE = 'DELETE';
41
42
    /**
43
     * The CONNECT method establishes a tunnel to the server identified by the target resource.
44
     *
45
     * @type string
46
     */
47
    const CONNECT = 'CONNECT';
48
49
    /**
50
     * The OPTIONS method is used to describe the communication options for the target resource.
51
     *
52
     * @type string
53
     */
54
    const OPTIONS = 'OPTIONS';
55
56
    /**
57
     * The TRACE method performs a message loop-back test along the path to the target resource.
58
     *
59
     * @type string
60
     */
61
    const TRACE = 'TRACE';
62
63
    /**
64
     * The PATCH method is used to apply partial modifications to a resource.
65
     *
66
     * @type string
67
     */
68
    const PATCH = 'PATCH';
69
70
71
    private $action;
72
    private $handler;
73
    private $httpMethod;
74
    private $route;
75
76
    public function __construct(
77
        string $httpMethod,
78
        string $route,
79
        string $handler,
80
        string $action
81
    ) {
82
        $this->setHttpMethod($httpMethod);
83
        $this->setRoute($route);
84
        $this->setHandler($handler);
85
        $this->setAction($action);
86
    }
87
88
    public function action()
89
    {
90
        return $this->action;
91
    }
92
93
    public function handler()
94
    {
95
        return $this->handler;
96
    }
97
98
    public function method()
99
    {
100
        return $this->httpMethod;
101
    }
102
103
    public function route()
104
    {
105
        return $this->route;
106
    }
107
108
    protected function setAction(string $action)
109
    {
110
        if (empty($action)) {
111
            throw new RouteException("Invalid action.");
112
        }
113
        $this->action = $action;
114
    }
115
116
    protected function setHandler(string $handler)
117
    {
118
        if (empty($handler)) {
119
            throw new RouteException("Invalid handler.");
120
        }
121
        $this->handler = $handler;
122
    }
123
124
    protected function setHttpMethod(string $httpMethod)
125
    {
126
        if (!in_array($httpMethod, [self::GET, self::HEAD, self::POST, self::PUT, self::DELETE, self::CONNECT, self::OPTIONS, self::TRACE, self::PATCH])) {
127
            throw new RouteException("Invalid HTTP method.");
128
        }
129
        $this->httpMethod = $httpMethod;
130
    }
131
132
    protected function setRoute(string $route)
133
    {
134
        if (empty($route)) {
135
            throw new RouteException("Invalid route.");
136
        }
137
        $this->route = $route;
138
    }
139
}
140