Completed
Push — master ( cba7ca...11b479 )
by Mikael
04:30
created

Route::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Anax\Route;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Route\Exception\ConfigurationException;
7
8
/**
9
 * Route to match a $path, mounted on $mount having a $handler to call.
10
 */
11
class Route
12
{
13
    /**
14
     * @var string       $name          a name for this route.
15
     * @var string       $info          description of route.
16
     * @var array        $method        the method(s) to support
17
     * @var string       $methodMatched the matched method.
18
     * @var string       $mount         where to mount the path
19
     * @var string       $path          the path rule for this route
20
     * @var callable     $handler       the callback to handle this route
21
     * @var null|array   $arguments     arguments for the callback, extracted
22
     *                                  from path
23
     */
24
    private $name;
25
    private $info;
26
    private $method;
27
    private $methodMatched;
28
    private $mount;
29
    private $path;
30
    private $handler;
31
    private $arguments = [];
32
33
34
35
    /**
36
     * Set values for route.
37
     *
38
     * @param string|array           $method  as request method to support
39
     * @param string                 $mount   where to mount the path
40
     * @param string                 $path    for this route
41
     * @param string|array|callable  $handler for this path, callable or equal
42
     * @param string                 $info    description of the route
43
     *
44
     * @return $this
45
     */
46 136
    public function set(
47
        $method = null,
48
        $mount = null,
49
        $path = null,
50
        $handler = null,
51
        string $info = null
52
    ) : object {
53 136
        $this->mount = rtrim($mount, "/");
54 136
        $this->path = $path;
55 136
        $this->handler = $handler;
56 136
        $this->info = $info;
57
58 136
        $this->method = $method;
59 136
        if (is_string($method)) {
60 22
            $this->method = array_map("trim", explode("|", $method));
61
        }
62 136
        if (is_array($this->method)) {
63 64
            $this->method = array_map("strtoupper", $this->method);
64
        }
65
66 136
        return $this;
67
    }
68
69
70
71
    /**
72
     * Check if the route matches a query and request method.
73
     *
74
     * @param string $query  to match against
75
     * @param string $method as request method
76
     *
77
     * @return boolean true if query matches the route
78
     */
79 122
    public function match(string $query, string $method = null)
80
    {
81 122
        $this->arguments = [];
82 122
        $this->methodMatched = null;
83
84 122
        $matcher = new RouteMatcher();
85 122
        $res = $matcher->match(
86 122
            $this->getAbsolutePath(),
87 122
            $query,
88 122
            $this->method,
0 ignored issues
show
Bug introduced by
It seems like $this->method can also be of type string; however, Anax\Route\RouteMatcher::match() does only seem to accept null|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
89 122
            $method
90
        );
91 122
        $this->arguments = $matcher->arguments;
92 122
        $this->methodMatched = $matcher->methodMatched;
93
94 122
        return $res;
95
    }
96
97
98
99
    /**
100
     * Handle the action for the route.
101
     *
102
     * @param string                       $path the matched path
103
     * @param ContainerInjectableInterface $di   container with services
104
     *
105
     * @return mixed
106
     */
107 110
    public function handle(
108
        string $path = null,
109
        ContainerInjectableInterface $di = null
110
    ) {
111 110
        if ($this->mount) {
112
            // Remove the mount path to get base for controller
113 8
            $len = strlen($this->mount);
114 8
            if (substr($path, 0, $len) == $this->mount) {
115 8
                $path = ltrim(substr($path, $len), "/");
116
            }
117
        }
118
119 110
        $handler = new RouteHandler();
120 110
        return $handler->handle($this->methodMatched, $path, $this->handler, $this->arguments, $di);
0 ignored issues
show
Documentation introduced by
$this->handler is of type callable|null, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
    }
122
123
124
125
    /**
126
     * Set the name of the route.
127
     *
128
     * @param string $name set a name for the route
129
     *
130
     * @return $this
131
     */
132 1
    public function setName($name)
133
    {
134 1
        $this->name = $name;
135 1
        return $this;
136
    }
137
138
139
140
    /**
141
     * Get information of the route.
142
     *
143
     * @return null|string as route information.
144
     */
145 2
    public function getInfo()
146
    {
147 2
        return $this->info;
148
    }
149
150
151
152
    /**
153
     * Get the path for the route.
154
     *
155
     * @return string
156
     */
157 1
    public function getPath()
158
    {
159 1
        return $this->path;
160
    }
161
162
163
164
    /**
165
     * Get the absolute $path by adding $mount.
166
     *
167
     * @return string|null as absolute path for this route.
168
     */
169 122
    public function getAbsolutePath()
170
    {
171 122
        if (is_null($this->path)) {
172 64
            return null;
173
        }
174
175 93
        if (empty($this->mount)) {
176 92
            return $this->path;
177
        }
178
179 1
        return $this->mount . "/" . $this->path;
180
    }
181
182
183
184
    /**
185
     * Get the request method for the route.
186
     *
187
     * @return string representing the request method supported
188
     */
189 1
    public function getRequestMethod()
190
    {
191 1
        return implode("|", $this->method);
192
    }
193
}
194