Matching   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 50
ccs 9
cts 11
cp 0.8182
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

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