Passed
Pull Request — master (#32)
by Anatoly
09:33
created

Router::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
use Sunrise\Http\Router\Exception\ExceptionInterface;
22
use Sunrise\Http\Router\Exception\MethodNotAllowedException;
23
use Sunrise\Http\Router\Exception\RouteNotFoundException;
24
use Sunrise\Http\Router\RequestHandler\RoutableRequestHandler;
25
26
/**
27
 * Import functions
28
 */
29
use function array_keys;
30
31
/**
32
 * Router
33
 */
34
class Router extends RouteCollection implements MiddlewareInterface, RequestHandlerInterface
35
{
36
37
    /**
38
     * Server Request attribute name for routing error instance
39
     *
40
     * @var string
41
     */
42
    public const ATTR_NAME_FOR_ROUTING_ERROR = '@routing-error';
43
44
    /**
45
     * Gets a route for the given name
46
     *
47
     * @param string $name
48
     *
49
     * @return RouteInterface
50
     *
51
     * @throws RouteNotFoundException
52
     */
53 2
    public function getRoute(string $name) : RouteInterface
54
    {
55 2
        foreach ($this->getRoutes() as $route) {
56 1
            if ($name === $route->getName()) {
57 1
                return $route;
58
            }
59
        }
60
61 1
        throw new RouteNotFoundException();
62
    }
63
64
    /**
65
     * Looks for a route that matches the given request
66
     *
67
     * @param ServerRequestInterface $request
68
     *
69
     * @return RouteInterface
70
     *
71
     * @throws MethodNotAllowedException
72
     * @throws RouteNotFoundException
73
     *
74
     * @todo Matching by strategy for detecting verbs...
75
     */
76 9
    public function match(ServerRequestInterface $request) : RouteInterface
77
    {
78 9
        $routes = [];
79 9
        foreach ($this->getRoutes() as $route) {
80 9
            foreach ($route->getMethods() as $method) {
81 9
                $routes[$method][] = $route;
82
            }
83
        }
84
85 9
        $method = $request->getMethod();
86 9
        if (!isset($routes[$method])) {
87 3
            throw new MethodNotAllowedException(
88 3
                array_keys($routes)
89
            );
90
        }
91
92 6
        $target = $request->getUri()->getPath();
93 6
        foreach ($routes[$method] as $route) {
94 6
            if (path_match($route->getPath(), $target, $attributes)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $attributes seems to be never defined.
Loading history...
Bug introduced by
The function path_match was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

94
            if (/** @scrutinizer ignore-call */ path_match($route->getPath(), $target, $attributes)) {
Loading history...
95 6
                return $route->withAddedAttributes($attributes);
96
            }
97
        }
98
99 3
        throw new RouteNotFoundException();
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 6
    public function handle(ServerRequestInterface $request) : ResponseInterface
106
    {
107 6
        $route = $this->match($request);
108
109 2
        $requestHandler = new RoutableRequestHandler($route);
110
111 2
        return $requestHandler->handle($request);
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
118
    {
119
        try {
120 3
            return $this->handle($request);
121 2
        } catch (ExceptionInterface $e) {
122 2
            return $handler->handle(
123 2
                $request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e)
124
            );
125
        }
126
    }
127
}
128