Route   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 5
dl 0
loc 215
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A extractParameters() 0 11 2
A attributesMatch() 0 6 2
A getUrl() 0 4 1
A getName() 0 4 1
A getCallable() 0 4 1
A getParameters() 0 4 1
A getRoute() 0 9 1
A getRequest() 0 4 1
A getResponse() 0 4 1
A requestUrlCall() 0 9 2
A with() 0 5 1
A run() 0 7 2
A handleRequestCallable() 0 6 1
A handleRequestController() 0 7 1
1
<?php
2
3
namespace Preetender\Routing;
4
5
use Illuminate\Http\Response;
6
use Illuminate\Http\Request;
7
use Preetender\Routing\Response\ExecuteResponse;
8
9
/**
10
 * Class Route
11
 * @package Preetender\Routing
12
 */
13
class Route
14
{
15
    use RouteReflection;
16
17
    /** @var string */
18
    protected $url;
19
20
    /** @var  */
21
    protected $callable;
22
23
    /** @var   */
24
    protected $name;
25
26
    /** @var array  */
27
    private static $parameters = [];
28
29
    /** @var array  */
30
    private static $attributes = [];
31
32
    /** @var Request  */
33
    protected $request;
34
35
    /** @var  Response */
36
    protected $response;
37
38
    /** @var  */
39
    protected $running;
40
41
    /**
42
     * Route constructor.
43
     * @param $url
44
     * @param $callable
45
     * @param null $name
46
     */
47
    public function __construct($url, $callable, $name = null)
48
    {
49
        $this->url = trim($url, '/');
50
        $this->callable = $callable;
51
        $this->request = Kernel::getContainer()->get(Request::class);
52
        $this->response = Kernel::getContainer()->get(Response::class);
53
        $this->name = $name;
54
    }
55
56
    /**
57
     * Check the path, if there are parameters to create attributes for the scope of the request
58
     *
59
     * @param $url
60
     * @return bool
61
     */
62
    public function extractParameters($url)
63
    {
64
        $url = trim($url, '/');
65
        $path = preg_replace_callback('#:([\w]+)#', [$this, 'attributesMatch'], $this->getUrl());
66
        if(!preg_match("#^$path$#i", $url, $matches)) {
67
            return false;
68
        }
69
        array_shift($matches);
70
        self::$parameters = $matches;
71
        return true;
72
    }
73
74
    /**
75
     * ...
76
     *
77
     * @param $match
78
     * @return string
79
     */
80
    private function attributesMatch($match) {
81
        if(isset(self::$attributes[$match[1]])) {
82
            return '(' . self::$attributes[$match[1]] . ')';
83
        }
84
        return '([^/]+)';
85
    }
86
87
    /**
88
     * Get url.
89
     *
90
     * @return string
91
     */
92
    public function getUrl(): string
93
    {
94
        return $this->url;
95
    }
96
97
    /**
98
     * Get name route
99
     *
100
     * @return null|string
101
     */
102
    public function getName()
103
    {
104
        return $this->name;
105
    }
106
107
    /**
108
     * Get callback.
109
     *
110
     * @return mixed
111
     */
112
    public function getCallable()
113
    {
114
        return $this->callable;
115
    }
116
117
    /**
118
     * Get params.
119
     *
120
     * @return array
121
     */
122
    public static function getParameters(): array
123
    {
124
        return self::$parameters;
125
    }
126
127
    /**
128
     * Get properties on route
129
     *
130
     * @return array
131
     */
132
    public function getRoute()
133
    {
134
        return [
135
           'name' => $this->getName(),
136
           'path' => $this->getUrl(),
137
           'callable' => $this->getCallable(),
138
           'parameters' => self::resolveParameters()
139
        ];
140
    }
141
142
    /**
143
     * Get request
144
     *
145
     * @return Request
146
     */
147
    public function getRequest(): Request
148
    {
149
       return $this->request;
150
    }
151
152
    /**
153
     * Get reponse
154
     *
155
     * @return Response
156
     */
157
    public function getResponse(): Response
158
    {
159
       return $this->response;
160
    }
161
162
    /**
163
     * Executing route
164
     *
165
     * @param array $parameters
166
     * @return mixed
167
     */
168
    public function requestUrlCall($parameters = [])
169
    {
170
        $path = $this->getUrl();
171
        foreach ($parameters as $key => $value) {
172
            $path = str_replace(":$key", $value, $path);
173
        }
174
        $this->extractParameters($path);
175
        return $this->run();
176
    }
177
178
    /**
179
     * Exporta para o controlador uma coleção de dados;
180
     *
181
     * @param string $attribute
182
     * @param string $regex
183
     * @return $this
184
     */
185
    public function with(string $attribute, string $regex)
186
    {
187
        self::$attributes[$attribute] = str_replace('(', '(?:', $regex);
188
        return $this;
189
    }
190
191
    /**
192
     * Check the type of request and define which treatment it should receive
193
     *
194
     * @return mixed
195
     */
196
    public function run()
197
    {
198
        if(is_string($this->getCallable())) {
199
            return  $this->handleRequestController();
200
        }
201
        return $this->handleRequestCallable();
202
    }
203
    /**
204
     * ...
205
     *
206
     * @return mixed
207
     */
208
    protected function handleRequestCallable()
209
    {
210
        self::createReflection(new \ReflectionFunction($this->getCallable()));
211
        $call = call_user_func_array($this->getCallable(), self::resolveParameters());
212
        return ExecuteResponse::factory($call, $this->getRequest(), $this->getResponse());
213
    }
214
215
    /**
216
     * ...
217
     *
218
     * @return mixed
219
     */
220
    protected function handleRequestController()
221
    {
222
        $attributes = RouteController::prepare($this->getCallable());
223
        self::createReflection(new \ReflectionClass($attributes['class']), $attributes['method']);
224
        $call = call_user_func_array([$attributes['class'], $attributes['method']], self::resolveParameters());
225
        return ExecuteResponse::factory($call, $this->getRequest(), $this->getResponse());
226
    }
227
}