Completed
Pull Request — master (#1)
by David
05:31
created

IsSafeHttpRoute::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\Middlewares\SafeRequests;
5
6
use Psr\Http\Message\ServerRequestInterface;
7
8
/**
9
 * Used to whitelist a set of routes.
10
 * Those routes will not be checked for CSRF.
11
 *
12
 * Useful for routes reserved for server to server APIs (AND already protected in another way).
13
 */
14
final class IsSafeHttpRoute implements IsSafeHttpRequestInterface
15
{
16
    /**
17
     * @var \string[]
18
     */
19
    private $routes;
20
21
    /**
22
     * @param \string[] ...$routes A list of routes (expressed as regular expressions) that are NOT checked for CSRF.
23
     */
24
    public function __construct(string ...$routes)
25
    {
26
        $this->routes = $routes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $routes of type array<integer,array<integer,object<string>>> is incompatible with the declared type array<integer,object<string>> of property $routes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
    }
28
29
    public function __invoke(ServerRequestInterface $request) : bool
30
    {
31
        $path = $request->getUri()->getPath();
32
33
        foreach ($this->routes as $route) {
34
            if (preg_match($route, $path)) {
35
                return true;
36
            }
37
        }
38
        return false;
39
    }
40
}
41