Completed
Push — master ( 91edc0...9fe488 )
by Edson
11s
created

Router::checkVerb()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Router;
4
5
/**
6
 * Class Router
7
 * @package Router
8
 */
9
class Router
10
{
11
    /**
12
     * @var array
13
     */
14
    private $server;
15
16
    /**
17
     * @var array
18
     */
19
    private static $routes = [];
20
21
    /**
22
     * Router constructor.
23
     */
24
    public function __construct()
25
    {
26
        $this->server = new Server();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Router\Server() of type Router\Server is incompatible with the declared type array of property $server.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
    }
28
29
    /**
30
     * @param array $route
31
     */
32
    public function add(array $route): void
33
    {
34
        self::$routes[] = new Client($route);
35
    }
36
37
    /**
38
     * @return null|Client
39
     */
40
    public function handle(): ?Client
41
    {
42
        foreach (self::$routes as $route) {
43
            if ($this->checkVerb($route) && $this->checkPath($route)) {
44
                $this->setArgs($route);
45
                return $route;
46
            }
47
        }
48
        return null;
49
    }
50
51
    /**
52
     * @param Client $route
53
     * @return bool
54
     */
55
    private function checkVerb(Client $route): bool
56
    {
57
        if ($this->server->getMethod() != $route->getMethod()) {
58
            return false;
59
        }
60
61
        return true;
62
    }
63
64
    /**
65
     * @param Client $route
66
     * @return bool
67
     */
68
    private function checkPath(Client $route): bool
69
    {
70
        if (count($route->getUri()) != count($this->server->getUri())) {
71
            return false;
72
        }
73
74
        if (!$this->parsePath($route)) {
75
            return false;
76
        }
77
78
        for ($i = 0; $i < count($route->getUri()) && count($this->server->getUri()); $i++) {
79
            if ($route->getUri()[$i] != $this->server->getUri()[$i]) {
80
                return false;
81
            }
82
        }
83
84
        return true;
85
    }
86
87
    /**
88
     * @param Client $route
89
     * @return bool
90
     */
91
    private function parsePath(Client $route): bool
92
    {
93
        $serverUri = $this->server->getUri();
94
        $routeUri = $route->getUri();
95
96
        for ($i = 0; $i < count($routeUri); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
97
            $serverUri[$i] = preg_replace('/\@.*/', $this->server->getUri()[$i], $routeUri[$i]);
98
            if ($serverUri[$i] != $routeUri[$i]) {
99
                // Regular expression named parameter matching
100
                if (preg_match('/\@([\w]+):(.*)/', $routeUri[$i], $match)) {
101
                    if (preg_match('/'.$match[2].'/', $serverUri[$i], $match[2])) {
102
                        if ($serverUri[$i] != $match[2][0]) {
103
                            return false;
104
                        }
105
                        $route->setArg($match[1], $match[2][0]);
106
                    } else {
107
                        return false;
108
                    }
109
                } else {
110
                    $route->setArg(str_replace(':', '', $routeUri[$i]), $serverUri[$i]);
111
                }
112
            }
113
        }
114
            
115
        $serverUri = implode('/', $serverUri);
116
        $route->setUri($serverUri);
117
118
        return true;
119
    }
120
121
    /**
122
     * @param Client $route
123
     */
124
    private function setArgs(Client $route): void
125
    {
126
        foreach ($_POST as $key => $value) {
127
            $route->setArg($key, $value);
128
        }
129
    }
130
}
131