Completed
Push — master ( 8a29f8...30084f )
by Márk
07:40
created

Matching   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 1
dl 0
loc 77
ccs 16
cts 20
cp 0.8
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getAuthentication() 0 4 1
A getMatcher() 0 4 1
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 7
    public function __construct(Authentication $authentication, callable $matcher = null)
30
    {
31 7
        if (is_null($matcher)) {
32
            $matcher = function () {
33
                return true;
34
            };
35
        }
36
37 7
        $this->authentication = $authentication;
38 7
        $this->matcher = $matcher;
39 7
    }
40
41
    /**
42
     * Returns the authentication.
43
     *
44
     * @return string
45
     */
46 1
    public function getAuthentication()
47
    {
48 1
        return $this->authentication;
49
    }
50
51
    /**
52
     * Returns the matcher.
53
     *
54
     * @return callable
55
     */
56 1
    public function getMatcher()
57
    {
58 1
        return $this->matcher;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 2
    public function authenticate(RequestInterface $request)
65
    {
66 2
        if (!call_user_func($this->matcher, $request)) {
67 1
            return $request;
68
        }
69
70 1
        return $this->authentication->authenticate($request);
71
    }
72
73
    /**
74
     * Creates a matching authentication for an URL.
75
     *
76
     * @param Authentication $authentication
77
     * @param string         $url
78
     *
79
     * @return self
80
     */
81
    public static function createUrlMatcher(Authentication $authentication, $url)
82
    {
83 1
        $matcher = function ($request) use ($url) {
84
            return preg_match($url, $request->getRequestTarget());
85 1
        };
86
87 1
        return new static($authentication, $matcher);
88
    }
89
}
90