Passed
Push — master ( 4cfdc5...37c7d0 )
by Alex
07:00
created

SimpleRouter::getRequestMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Mezon\Router;
3
4
/**
5
 * Class SimpleRouter
6
 *
7
 * @package Mezon
8
 * @subpackage Router
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2021/09/27)
11
 * @copyright Copyright (c) 2021, aeon.org
12
 */
13
14
/**
15
 * Simple router class
16
 */
17
class SimpleRouter implements RouterInterface
18
{
19
20
    use SimpleRoutesSet, SimpleUrlParser, RouteTypes;
21
22
    /**
23
     * Method wich handles invalid route error
24
     *
25
     * @var callable
26
     */
27
    private $invalidRouteErrorHandler;
28
29
    /**
30
     * Method returns request method
31
     *
32
     * @return string Request method
33
     */
34
    private function getRequestMethod(): string
35
    {
36
        return $_SERVER['REQUEST_METHOD'] ?? 'GET';
37
    }
38
39
    /**
40
     * Constructor
41
     */
42
    public function __construct()
43
    {
44
        $_SERVER['REQUEST_METHOD'] = $this->getRequestMethod();
45
46
        $this->invalidRouteErrorHandler = [
47
            $this,
48
            'noProcessorFoundErrorHandler'
49
        ];
50
51
        $this->initDefaultTypes();
52
    }
53
54
    /**
55
     * Method fetches actions from the objects and creates GetRoutes for them
56
     *
57
     * @param object $object
58
     *            Object to be processed
59
     * @param array $map
60
     *            map
61
     */
62
    public function fetchActions(object $object, array $map = []): void
63
    {
64
        $methods = get_class_methods($object);
65
66
        foreach ($methods as $method) {
67
            if (strpos($method, 'action') === 0) {
68
                $route = Utils::convertMethodNameToRoute($method);
69
70
                $key = str_replace('action', '', $method);
71
                $requestMethods = array_key_exists($key, $map) ? $map[$key] : [
72
                    'GET',
73
                    'POST'
74
                ];
75
76
                $this->addRoute($route, [
77
                    $object,
78
                    $method
79
                ], $requestMethods);
80
            }
81
        }
82
    }
83
84
    /**
85
     * Method processes no processor found error
86
     *
87
     * @param string $route
88
     *            Route
89
     */
90
    public function noProcessorFoundErrorHandler(string $route): void
91
    {
92
        throw (new \Exception(
93
            'The processor was not found for the route ' . $route . ' in ' . $this->getAllRoutesTrace()));
94
    }
95
96
    /**
97
     * Method sets InvalidRouteErrorHandler function
98
     *
99
     * @param callable $function
100
     *            Error handler
101
     *            
102
     * @return callable old error handler
103
     */
104
    public function setNoProcessorFoundErrorHandler(callable $function): callable
105
    {
106
        $oldErrorHandler = $this->invalidRouteErrorHandler;
107
108
        $this->invalidRouteErrorHandler = $function;
109
110
        return $oldErrorHandler;
111
    }
112
113
    /**
114
     *
115
     * {@inheritdoc}
116
     * @see \Mezon\Router\RouterInterface::callRoute()
117
     */
118
    public function callRoute($route)
119
    {
120
        $route = Utils::prepareRoute($route);
121
        $requestMethod = $this->getRequestMethod();
122
        $this->validateRequestMethod($requestMethod);
123
124
        if (($result = $this->findStaticRouteProcessor($route)) !== false) {
125
            return $result;
126
        }
127
        if (($result = $this->findDynamicRouteProcessor($route)) !== false) {
128
            return $result;
129
        }
130
        if (($result = $this->findUniversalRouteProcessor($route)) !== false) {
131
            return $result;
132
        }
133
        call_user_func($this->invalidRouteErrorHandler, $route);
134
    }
135
136
    /**
137
     * Method returns call back by it's router
138
     *
139
     * @param array|string $route
140
     *            route
141
     * @return array|callable|bool route callback
142
     */
143
    public function getCallback($route)
144
    {
145
        $route = Utils::prepareRoute($route);
146
147
        if (($result = $this->getStaticRouteProcessor($route)) !== false) {
148
            return $result;
149
        }
150
151
        if (($result = $this->getDynamicRouteProcessor($route)) !== false) {
152
            return $result;
153
        }
154
155
        if (($result = $this->getUniversalRouteProcessor()) !== false) {
156
            return $result;
157
        }
158
159
        call_user_func($this->invalidRouteErrorHandler, $route); // @codeCoverageIgnoreStart
160
        return false;
161
    } // @codeCoverageIgnoreEnd
162
}
163