RoutingMiddleware   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 6
c 2
b 0
f 0
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Infrastructure\Http;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\MiddlewareInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
19
20
/**
21
 * RoutingMiddleware
22
 *
23
 * @package Slick\WebStack\Infrastructure\Http
24
 */
25
final class RoutingMiddleware implements MiddlewareInterface
26
{
27
28
    /**
29
     * Creates a RoutingMiddleware
30
     *
31
     * @param UrlMatcherInterface $matcher
32
     */
33
    public function __construct(
34
        private UrlMatcherInterface $matcher,
35
        private string $routingBasePath = ''
36
    ) {
37
    }
38
39
    /**
40
     * @inheritDoc
41
     * @SuppressWarnings
42
     */
43
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
44
    {
45
        $pathinfo = $request->getUri()->getPath();
46
        $basePath = $this->routingBasePath;
47
        $parameters = $this->matcher->match(str_replace('//', '/', '/'.str_replace($basePath, '', $pathinfo)));
48
        $request = $request->withAttribute('route', $parameters);
49
        return $handler->handle($request);
50
    }
51
}
52