Completed
Pull Request — master (#14)
by Márk
03:05
created

RequestMatcherPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Http\Client\Common\Plugin;
4
5
use Http\Client\Common\Plugin;
6
use Http\Message\RequestMatcher;
7
use Psr\Http\Message\RequestInterface;
8
9
/**
10
 * Apply a delegated plugin based on a request match.
11
 *
12
 * @author Márk Sági-Kazár <[email protected]>
13
 */
14
final class RequestMatcherPlugin implements Plugin
15
{
16
    /**
17
     * @var RequestMatcher
18
     */
19
    private $requestMatcher;
20
21
    /**
22
     * @var Plugin
23
     */
24
    private $delegatedPlugin;
25
26
    /**
27
     * @param RequestMatcher $requestMatcher
28
     * @param Plugin         $delegatedPlugin
29
     */
30 4
    public function __construct(RequestMatcher $requestMatcher, Plugin $delegatedPlugin)
31
    {
32 4
        $this->requestMatcher = $requestMatcher;
33 4
        $this->delegatedPlugin = $delegatedPlugin;
34 4
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
40
    {
41 2
        if ($this->requestMatcher->matches($request)) {
42 1
            return $this->delegatedPlugin->handleRequest($request, $next, $first);
43
        }
44
45 1
        return $next($request);
46
    }
47
}
48