Passed
Push — master ( efc3b3...7d9581 )
by Felipe
05:21
created

RouteMiddleware   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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