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

IsSafeHttpMethod   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 22
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
    /**
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