Completed
Pull Request — master (#65)
by Márk
17:06 queued 07:07
created

RequestConditionalPlugin   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleRequest() 0 8 2
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