RouteMiddleware   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 34
ccs 8
cts 8
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 6 1
1
<?php
2
3
/**
4
 * This file is part of coisa/http.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\Http\Middleware;
15
16
use CoiSA\Http\Handler\MiddlewareHandler;
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
22
/**
23
 * Class RouteMiddleware
24
 *
25
 * @package CoiSA\Http\Middleware
26
 */
27
final class RouteMiddleware implements MiddlewareInterface
28
{
29
    /**
30
     * @var string
31
     */
32
    private $pattern;
33
34
    /**
35
     * @var MiddlewareInterface
36
     */
37
    private $middleware;
38
39
    /**
40
     * RouteMiddleware constructor.
41
     *
42
     * @param string                  $method
43
     * @param string                  $pattern
44
     * @param RequestHandlerInterface $handler
45
     */
46 5
    public function __construct(string $method, string $pattern, RequestHandlerInterface $handler)
47
    {
48 5
        $this->pattern    = $pattern;
49 5
        $this->middleware = new RequestMethodMiddleware($method, $handler);
50 5
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
56
    {
57 4
        $requestHandler = new MiddlewareHandler($this->middleware, $handler);
58 4
        $middleware     = new PregMatchRequestTargetMiddleware($this->pattern, $requestHandler);
59
60 4
        return $middleware->process($request, $handler);
61
    }
62
}
63