Completed
Pull Request — master (#65)
by Márk
07:54
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\Authentication;
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 RequestConditionalPlugin 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
    public function __construct(RequestMatcher $requestMatcher, Plugin $delegatedPlugin)
31
    {
32
        $this->requestMatcher = $requestMatcher;
33
        $this->delegatedPlugin = $delegatedPlugin;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
40
    {
41
        if ($this->requestMatcher->matches($request)) {
42
            return $this->delegatedPlugin->handleRequest($request, $next, $first);
43
        }
44
45
        return $next($request);
46
    }
47
}
48