Matching::authenticate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 2
rs 10
c 1
b 0
f 0
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