Passed
Push — master ( a4ddd3...82bd79 )
by Alex
06:44
created

StaticRoutes   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
eloc 32
c 1
b 1
f 0
dl 0
loc 108
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findUniversalRouteProcessor() 0 9 2
A getUniversalRouteProcessor() 0 10 2
A getStaticRouteProcessor() 0 10 2
A findStaticRouteProcessor() 0 9 2
A getStaticRoutes() 0 9 2
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router;
4
5
/**
6
 * Trait StaticRoutes
7
 *
8
 * @package Router
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2021/09/27)
11
 * @copyright Copyright (c) 2021, aeon.org
12
 */
13
trait StaticRoutes
14
{
15
16
    /**
17
     * List of static routes for all supported request methods
18
     *
19
     * @var array<string, array<string, array{0: string, 1:string}|callable|string>>
20
     */
21
    protected $staticRoutes = [
22
        'GET' => [],
23
        'POST' => [],
24
        'PUT' => [],
25
        'DELETE' => [],
26
        'OPTION' => [],
27
        'PATCH' => []
28
    ];
29
30
    /**
31
     * Method returns static routes handlers for the specified request methods
32
     *
33
     * @param string $requestMethod
34
     *            request method
35
     * @return array<string, array{0: string, 1:string}|callable|string> handlers
36
     */
37
    protected function getStaticRoutes(string $requestMethod): array
0 ignored issues
show
Unused Code introduced by
The parameter $requestMethod 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

37
    protected function getStaticRoutes(/** @scrutinizer ignore-unused */ string $requestMethod): array

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...
38
    {
39
        $requestMethod = (string) $_SERVER['REQUEST_METHOD'];
40
41
        if (! isset($this->staticRoutes[$requestMethod])) {
42
            throw (new \Exception('Unsupported request method : ' . $requestMethod, - 1));
43
        }
44
45
        return $this->staticRoutes[$requestMethod];
46
    }
47
48
    /**
49
     * Method returns route handler
50
     *
51
     * @param string $route
52
     *            routes
53
     * @return array{0: string, 1: string}|callable|false|string route handler
54
     */
55
    protected function getStaticRouteProcessor(string $route)
56
    {
57
        $processors = $this->getStaticRoutes((string) $_SERVER['REQUEST_METHOD']);
58
59
        if (isset($processors[$route])) {
60
            $this->calledRoute = $route;
0 ignored issues
show
Bug Best Practice introduced by
The property calledRoute does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
61
62
            return $processors[$route];
63
        } else {
64
            return false;
65
        }
66
    }
67
68
    /**
69
     * Method returns route handler
70
     *
71
     * @return array{0: string, 1: string}|callable|false|string route handler
72
     */
73
    protected function getUniversalRouteProcessor()
74
    {
75
        $processors = $this->getStaticRoutes((string) $_SERVER['REQUEST_METHOD']);
76
77
        if (isset($processors['*'])) {
78
            $this->calledRoute = '*';
0 ignored issues
show
Bug Best Practice introduced by
The property calledRoute does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
80
            return $processors['*'];
81
        } else {
82
            return false;
83
        }
84
    }
85
    
86
    
87
    /**
88
     * Method searches route processor
89
     *
90
     * @param string $route
91
     *            route
92
     * @return mixed|false result of the router processor
93
     */
94
    public function findStaticRouteProcessor(string $route)
95
    {
96
        $processor = $this->getStaticRouteProcessor($route);
97
        
98
        if ($processor === false) {
99
            return false;
100
        }
101
        
102
        return $this->executeHandler($processor, $route);
0 ignored issues
show
Bug introduced by
It seems like executeHandler() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

102
        return $this->/** @scrutinizer ignore-call */ executeHandler($processor, $route);
Loading history...
103
    }
104
    
105
    /**
106
     * Method searches universal route processor
107
     *
108
     * @param string $route
109
     *            route
110
     * @return mixed|false result of the router processor
111
     */
112
    public function findUniversalRouteProcessor(string $route)
113
    {
114
        $processor = $this->getUniversalRouteProcessor();
115
        
116
        if ($processor === false) {
117
            return false;
118
        }
119
        
120
        return $this->executeHandler($processor, $route);
121
    }
122
}
123