Test Failed
Push — develop ( acdfb2...2de2bf )
by nguereza
02:53
created

RouteMatchMiddleware::process()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 27
rs 9.4888
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant PHP
7
 * Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file RouteMatchMiddleware.php
34
 *
35
 *  The Route match middleware class is used to match the request
36
 * based on the routes definition
37
 *
38
 *  @package    Platine\Framework\Http\Middleware
39
 *  @author Platine Developers Team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   http://www.iacademy.cf
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Framework\Http\Middleware;
50
51
use Platine\Framework\Http\Exception\HttpMethodNotAllowedException;
52
use Platine\Http\Handler\MiddlewareInterface;
53
use Platine\Http\Handler\RequestHandlerInterface;
54
use Platine\Http\Response;
55
use Platine\Http\ResponseInterface;
56
use Platine\Http\ServerRequestInterface;
57
use Platine\Route\Route;
58
use Platine\Route\Router;
59
60
/**
61
 * class RouteMatchMiddleware
62
 * @package Platine\Framework\Http\Middleware
63
 */
64
class RouteMatchMiddleware implements MiddlewareInterface
65
{
66
67
    /**
68
     * The Router instance
69
     * @var Router
70
     */
71
    protected Router $router;
72
73
    /**
74
     * The list of allowed methods
75
     * @var array<string>
76
     */
77
    protected array $allowedMethods = [];
78
79
    /**
80
     * Create new instance
81
     * @param Router $router
82
     * @param array<string>  $allowedMethods the default allowed methods
83
     */
84
    public function __construct(Router $router, array $allowedMethods = ['HEAD'])
85
    {
86
        $this->router = $router;
87
88
        foreach ($allowedMethods as $method) {
89
            if (is_string($method)) {
90
                $this->allowedMethods[] = strtoupper($method);
91
            }
92
        }
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function process(
99
        ServerRequestInterface $request,
100
        RequestHandlerInterface $handler
101
    ): ResponseInterface {
102
        if (!$route = $this->router->match($request, false)) {
103
            return $handler->handle($request);
104
        }
105
106
        if (
107
                !$this->isAllowedMethod($request->getMethod())
108
                && !$route->isAllowedMethod($request->getMethod())
109
        ) {
110
            $exception = new HttpMethodNotAllowedException($request);
111
            $exception->setAllowedMethods($route->getMethods());
112
113
            throw $exception;
114
        }
115
116
        foreach ($route->getParameters()->all() as $parameter) {
117
            $request = $request->withAttribute(
118
                $parameter->getName(),
119
                $parameter->getValue()
120
            );
121
        }
122
123
        $request = $request->withAttribute(Route::class, $route);
124
        return $handler->handle($request);
125
    }
126
127
    /**
128
     * Check whether the given method is allowed
129
     * @param  string  $method
130
     * @return bool
131
     */
132
    protected function isAllowedMethod(string $method): bool
133
    {
134
        return (empty($this->allowedMethods)
135
                || in_array(strtoupper($method), $this->allowedMethods, true));
136
    }
137
}
138