Passed
Push — master ( c44d00...13b3c2 )
by David
53s
created

IsSafeHttpMethod::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
 * Shamelessly borrowed from Ocramius/PSR7Csrf package.
10
 */
11
final class IsSafeHttpMethod implements IsSafeHttpRequestInterface
12
{
13
    const STRICT_COMPARE = true; // Used a constant to avoid a Humbug mutation.
14
15
    /**
16
     * @var \string[]
17
     */
18
    private $safeMethods;
19
20
    public function __construct(string ...$safeMethods)
21
    {
22
        $this->safeMethods = array_map('strtoupper', $safeMethods);
23
    }
24
25
    public static function fromDefaultSafeMethods() : self
26
    {
27
        return new self('GET', 'HEAD', 'OPTIONS');
28
    }
29
30
    public function __invoke(ServerRequestInterface $request) : bool
31
    {
32
        return in_array(strtoupper($request->getMethod()), $this->safeMethods, self::STRICT_COMPARE);
33
    }
34
}
35