Passed
Pull Request — master (#1)
by David
02:31
created

IsSafeHttpMethod::fromDefaultSafeMethods()   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 0
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
    /**
14
     * @var \string[]
15
     */
16
    private $safeMethods;
17
18
    public function __construct(string ...$safeMethods)
19
    {
20
        $this->safeMethods = array_map('strtoupper', $safeMethods);
21
    }
22
23
    public static function fromDefaultSafeMethods() : self
24
    {
25
        return new self('GET', 'HEAD', 'OPTIONS');
26
    }
27
28
    public function __invoke(ServerRequestInterface $request) : bool
29
    {
30
        return in_array(strtoupper($request->getMethod()), $this->safeMethods, true);
31
    }
32
}
33