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

IsSafeHttpMethod   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 24
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromDefaultSafeMethods() 0 4 1
A __invoke() 0 4 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