Router::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Bonfim\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 Bonfim\Router\Server() of type Bonfim\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
                return $route;
45
            }
46
        }
47
        return null;
48
    }
49
50
    /**
51
     * @param Client $route
52
     * @return bool
53
     */
54
    private function checkVerb(Client $route): bool
55
    {
56
        if ($this->server->getMethod() != $route->getMethod()) {
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * @param Client $route
65
     * @return bool
66
     */
67
    private function checkPath(Client $route): bool
68
    {
69
        $serverUri = $this->server->getUri();
70
        $routeUri = $route->getUri();
71
72
        if (count($serverUri) != count($routeUri)) {
73
            return false;
74
        }
75
76
        foreach ($routeUri as $i => $k) {
77
            // Verifica se o parametro e' nomeado
78
            if (preg_match('/@(.*)/', $k, $match)) {
79
                $name = $match[1];
80
81
                // Verifica se o parametro tem alguma regex
82
                if (preg_match('/([\w]+):(.*)/', $name, $match)) {
83
                    $regex = $match[2];
84
85
                    // Aplica a regex
86
                    if (!preg_match("/^$regex$/", $serverUri[$i])) {
87
                        return false;
88
                    }
89
90
                    // Atualiza o nome tirando a parte da regex
91
                    $name = $match[1];
92
                }
93
94
                // Adiciona o parametro na rota
95
                $route->setArg($name, $serverUri[$i]);
96
            } else {
97
                // Se o parametro nao for nomeado, verifica se eh igual a uri
98
                if ($k != $serverUri[$i]) {
99
                    return false;
100
                }
101
            }
102
        }
103
104
        return true;
105
    }
106
}
107