Completed
Push — master ( 55d4ba...3ea7f0 )
by Guido
03:29
created

RouteManager::convertUriParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php namespace Gvera\Helpers\routes;
2
3
use Gvera\Helpers\http\HttpRequest;
4
5
/**
6
 * Class RouteManager
7
 * @package Gvera\Helpers\routes
8
 * The routing is managed through convention over configuration by default, but a custom route could be added in the
9
 * routes.yml file, that rule will override and take precedence. This class has the algorithm that decides if the route
10
 * that is being input match any of the ones that are noted in routes.yml.
11
 */
12
class RouteManager
13
{
14
    private array $routes;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
15
    const ROUTE_NEEDLE = ':';
16
    const ROUTE_CACHE_KEY = 'gv_routes';
17
    private HttpRequest $httpRequest;
18
    private array $excludeDirectories = [".", ".."];
19
20
    /**
21
     * @Cached
22
     * RouteManager constructor.
23
     * @param $httpRequest
24
     */
25
    public function __construct(HttpRequest $httpRequest)
26
    {
27
        $this->httpRequest = $httpRequest;
28
    }
29
30
    public function setRoutes(array $routes)
31
    {
32
        $this->routes = $routes;
33
    }
34
35
36
    /**
37
     * @param $pathLike
38
     * @return mixed
39
     */
40
    public function getRoute($pathLike)
41
    {
42
        $pathLikeArray = explode("/", $pathLike);
43
        if (!$this->isPathLikeArrayValid($pathLikeArray)) {
44
            return false;
45
        }
46
47
        $filteredRoutes = $this->stripRoutesByHttpMethod($this->httpRequest->getRequestType());
48
49
        foreach ($filteredRoutes as $route) {
50
            if ($routeFound = $this->defineRoute($route, $pathLikeArray)) {
51
                return $routeFound;
52
            }
53
        }
54
        return false;
55
    }
56
57
    public function addRoute($method, $route, $action)
58
    {
59
        $this->routes[$method][] = array('route' => $route, 'action' => $action);
60
    }
61
62
    public function getExcludeDirectories(): array
63
    {
64
        return $this->excludeDirectories;
65
    }
66
67
    /**
68
     * @return string|bool
69
     */
70
    private function defineRoute($route, $pathLikeArray)
71
    {
72
        if (!(strpos($route['uri'], $pathLikeArray[1]) !== false) ||
73
            !(strpos($route['uri'], $pathLikeArray[2]) !== false)) {
74
                return false;
75
        }
76
            $totalRoute = $route['uri'] ;
77
            $totalRouteArray = explode("/", $totalRoute);
78
            $routeController = $totalRouteArray[1];
79
            $routeMethod = $totalRouteArray[2];
80
            
81
            return $this->isUrlAndUriValid($pathLikeArray, $routeController, $routeMethod, $totalRoute, $route);
82
    }
83
84
    /**
85
     * @param array $pathLikeArray
86
     * @param string $routeController
87
     * @param string $routeMethod
88
     * @param string $totalRoute
89
     * @param array $route
90
     * @return false|string
91
     */
92
    private function isUrlAndUriValid(
93
        array $pathLikeArray,
94
        string $routeController,
95
        string $routeMethod,
96
        string $totalRoute,
97
        array $route
98
    ) {
99
        $urlCheck = ($pathLikeArray[1] == $routeController && $pathLikeArray[2] == $routeMethod);
100
        $checkUri = $this->convertUriParams($pathLikeArray, explode('/', $totalRoute));
101
        return $urlCheck && $checkUri ? $route['action'] : false;
102
    }
103
104
    /**
105
     * @param $pathLikeArray
106
     * @return bool
107
     */
108
    private function isPathLikeArrayValid($pathLikeArray): bool
109
    {
110
        return isset($pathLikeArray[2]) && !empty($pathLikeArray[2]);
111
    }
112
113
    /**
114
     * @param $totalRoute
115
     * @param $pathLikeArray
116
     * @return bool
117
     */
118
    private function convertUriParams($totalRoute, $pathLikeArray)
119
    {
120
        $count = count($pathLikeArray);
121
        for ($i = 0; $i < $count; $i++) {
122
            if (substr_count($pathLikeArray[$i], self::ROUTE_NEEDLE) == 2) {
123
                $value = $totalRoute[$i] ?? null;
124
125
                $this->httpRequest->setParameter(
126
                    str_replace(self::ROUTE_NEEDLE, '', $pathLikeArray[$i]),
127
                    $value
128
                );
129
            }
130
        }
131
132
        return true;
133
    }
134
135
    /**
136
     * @param $method
137
     * @return array
138
     */
139
    private function stripRoutesByHttpMethod($method): array
140
    {
141
        $filteredRoutes = array();
142
        foreach ($this->routes as $route) {
143
            if ($route['method'] == $method) {
144
                $filteredRoutes[] = $route;
145
            }
146
        }
147
148
        return $filteredRoutes;
149
    }
150
}
151