Completed
Pull Request — master (#123)
by David
01:53
created

Matching::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 2.1481
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
    /**
31
     * @param Authentication $authentication
32
     * @param callable|null  $matcher
33
     */
34 5
    public function __construct(Authentication $authentication, callable $matcher = null)
35
    {
36 5
        if (is_null($matcher)) {
37
            $matcher = function () {
38
                return true;
39
            };
40
        }
41
42 5
        $this->authentication = $authentication;
43 5
        $this->matcher = new CallbackRequestMatcher($matcher);
44 5
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function authenticate(RequestInterface $request)
50
    {
51 2
        if ($this->matcher->matches($request)) {
52 1
            return $this->authentication->authenticate($request);
53
        }
54
55 1
        return $request;
56
    }
57
58
    /**
59
     * Creates a matching authentication for an URL.
60
     *
61
     * @param Authentication $authentication
62
     * @param string         $url
63
     *
64
     * @return self
65
     */
66
    public static function createUrlMatcher(Authentication $authentication, $url)
67
    {
68 1
        $matcher = function (RequestInterface $request) use ($url) {
69
            return preg_match($url, $request->getRequestTarget());
70 1
        };
71
72 1
        return new static($authentication, $matcher);
73
    }
74
}
75