1 | <?php |
||
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 | 5 | * @param callable|null $matcher |
|
33 | */ |
||
34 | 5 | public function __construct(Authentication $authentication, callable $matcher = null) |
|
35 | { |
||
36 | if (is_null($matcher)) { |
||
37 | $matcher = function () { |
||
38 | return true; |
||
39 | }; |
||
40 | 5 | } |
|
41 | 5 | ||
42 | 5 | $this->authentication = $authentication; |
|
43 | $this->matcher = new CallbackRequestMatcher($matcher); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | 2 | * {@inheritdoc} |
|
48 | */ |
||
49 | 2 | public function authenticate(RequestInterface $request) |
|
50 | 1 | { |
|
51 | if ($this->matcher->matches($request)) { |
||
52 | return $this->authentication->authenticate($request); |
||
53 | 1 | } |
|
54 | |||
55 | 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 | 1 | public static function createUrlMatcher(Authentication $authentication, $url) |
|
74 | } |
||
75 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.