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

PregMatchRequestTargetMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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