Completed
Pull Request — master (#65)
by Márk
25:20 queued 15:23
created

RequestConditionalPlugin::handleRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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