Passed
Push — master ( 8aa69a...8e436e )
by Edson
02:15
created

Router::setArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EdsonOnildo\Router;
4
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * Class Router
9
 * @package Router
10
 */
11
class Router
12
{
13
    private $request;
14
15
    /**
16
     * @var array
17
     */
18
    private static $routes = [];
19
20
    /**
21
     * Router constructor.
22
     */
23
    public function __construct()
24
    {
25
        $this->request = Request::createFromGlobals();
26
    }
27
28
    /**
29
     * @param array $route
30
     */
31
    public function add(array $route): void
32
    {
33
        $request = Request::create(
34
            $route['uri'],
35
            strtoupper($route['method'])
36
        );
37
38
        $request->attributes->set('callback', $route['callback']);
39
40
        self::$routes[] = $request;
41
    }
42
43
    /**
44
     * @return Request|null
45
     */
46
    public function handle(): ?Request
47
    {
48
        foreach (self::$routes as $route) {
49
            if ($this->checkVerb($route) && $this->checkPath($route)) {
50
                return $route;
51
            }
52
        }
53
        return null;
54
    }
55
56
    /**
57
     * @param Request $route
58
     * @return bool
59
     */
60
    private function checkVerb(Request $route): bool
61
    {
62
        return $this->request->isMethod('get') == $route->isMethod('get');
63
    }
64
65
    /**
66
     * @param Request $route
67
     * @return bool
68
     */
69
    private function checkPath(Request $route): bool
70
    {
71
        // if (count($route->getUri()) != count($this->server->getUri())) {
72
        //     return false;
73
        // }
74
        return $this->parsePath($route)
75
            && $this->request->getPathInfo() == $route->getPathInfo();
76
    }
77
78
    /**
79
     * @param Route $route
80
     * @return bool
81
     */
82
    private function parsePath(Request $route): bool
0 ignored issues
show
Unused Code introduced by
The parameter $route is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
    private function parsePath(/** @scrutinizer ignore-unused */ Request $route): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    {
84
        // for ($i = 0; $i < count($routeUri); $i++) {
85
        //     $serverUri[$i] = preg_replace('/\@.*/', $this->server->getUri()[$i], $routeUri[$i]);
86
        //     if ($serverUri[$i] != $routeUri[$i]) {
87
        //         // Regular expression named parameter matching
88
        //         if (preg_match('/\@([\w]+):(.*)/', $routeUri[$i], $match)) {
89
        //             if (preg_match('/'.$match[2].'/', $serverUri[$i], $match[2])) {
90
        //                 if ($serverUri[$i] != $match[2][0]) {
91
        //                     return false;
92
        //                 }
93
        //                 $route->setArg($match[1], $match[2][0]);
94
        //             } else {
95
        //                 return false;
96
        //             }
97
        //         } else {
98
        //             $routeUri[$i] = str_replace('@', '', $routeUri[$i]);
99
        //             $route->setArg(str_replace(':', '', $routeUri[$i]), $serverUri[$i]);
100
        //         }
101
        //     }
102
        // }
103
            
104
        // $serverUri = implode('/', $serverUri);
105
        // $route->setUri($serverUri);
106
107
        return true;
108
    }
109
}
110