Completed
Pull Request — master (#5)
by Márk
02:37
created

Matching   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
lcom 1
cbo 1
dl 0
loc 57
ccs 12
cts 15
cp 0.8
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A authenticate() 0 8 2
A createUrlMatcher() 0 8 1
1
<?php
2
3
namespace Http\Message\Authentication;
4
5
use Http\Message\Authentication;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Authenticate a PSR-7 Request if the reuqest is matching.
10
 *
11
 * @author Márk Sági-Kazár <[email protected]>
12
 */
13
final class Matching implements Authentication
14
{
15
    /**
16
     * @var Authentication
17
     */
18
    private $authentication;
19
20
    /**
21
     * @var callable
22
     */
23
    private $matcher;
24
25
    /**
26
     * @param Authentication $authentication
27
     * @param callable|null  $matcher
28
     */
29 9
    public function __construct(Authentication $authentication, callable $matcher = null)
30
    {
31 9
        if (is_null($matcher)) {
32
            $matcher = function () {
33
                return true;
34
            };
35
        }
36
37 9
        $this->authentication = $authentication;
38 9
        $this->matcher = $matcher;
39 9
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function authenticate(RequestInterface $request)
45
    {
46 2
        if (!call_user_func($this->matcher, $request)) {
47
            return $request;
48 2
        }
49
50
        return $this->authentication->authenticate($request);
51
    }
52
53
    /**
54
     * Creates a matching authentication for an URL.
55
     *
56 1
     * @param Authentication $authentication
57
     * @param string         $url
58 1
     *
59 1
     * @return self
60
     */
61
    public static function createUrlMatcher(Authentication $authentication, $url)
62
    {
63
        $matcher = function ($request) use ($url) {
64
            return preg_match($url, $request->getRequestTarget());
65
        };
66 2
67
        return new static($authentication, $matcher);
68 2
    }
69
}
70