Completed
Push — master ( 3206cc...899138 )
by David
24:10 queued 23:01
created

Matching::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 7
cp 0.7143
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0932
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The class Http\Message\Authentication\Matching has been deprecated with message: since since version 1.2, and will be removed in 2.0. Use {@link RequestConditional} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
73
    }
74
}
75