PregMatchRequestTargetMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 43
ccs 12
cts 12
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 13 2
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 Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
20
21
/**
22
 * Class PregMatchRequestTargetMiddleware
23
 *
24
 * @package CoiSA\Http\Middleware
25
 */
26
final class PregMatchRequestTargetMiddleware implements MiddlewareInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    private $pattern;
32
33
    /**
34
     * @var RequestHandlerInterface
35
     */
36
    private $handler;
37
38
    /**
39
     * PregMatchRequestTargetMiddleware constructor.
40
     *
41
     * @param string                  $pattern
42
     * @param RequestHandlerInterface $handler
43
     */
44 4
    public function __construct(string $pattern, RequestHandlerInterface $handler)
45
    {
46 4
        $this->pattern = '(' . $pattern . ')i';
47 4
        $this->handler = $handler;
48 4
    }
49
50
    /**
51
     * @param ServerRequestInterface  $request
52
     * @param RequestHandlerInterface $handler
53
     *
54
     * @return ResponseInterface
55
     */
56 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
57
    {
58 4
        if (!\preg_match($this->pattern, $request->getRequestTarget(), $matches)) {
59 1
            return $handler->handle($request);
60
        }
61
62 3
        $pattern = $this->pattern;
63 3
        $request = $request->withAttribute(
64 3
            self::class,
65 3
            \compact('pattern', 'matches')
66
        );
67
68 3
        return $this->handler->handle($request);
69
    }
70
}
71