Completed
Push — master ( 7656a5...de6dfd )
by David
06:46
created

RequestMatcherPlugin::handleRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

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