Passed
Push — master ( ddbf8b...086098 )
by Robbie
11:17
created

HttpMethodBypass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethods() 0 3 1
A addMethods() 0 22 4
A __construct() 0 3 1
A checkRequestForBypass() 0 3 1
1
<?php
2
3
namespace SilverStripe\Control\Middleware\ConfirmationMiddleware;
4
5
use SilverStripe\Control\HTTPRequest;
6
7
/**
8
 * Allows to bypass requests of a particular HTTP method
9
 */
10
class HttpMethodBypass implements Bypass
11
{
12
    /**
13
     * HTTP Methods to bypass
14
     *
15
     * @var string[]
16
     */
17
    private $methods = [];
18
19
    /**
20
     * Initialize the bypass with HTTP methods
21
     *
22
     * @param string[] ...$method
23
     */
24
    public function __construct(...$methods)
25
    {
26
        $this->addMethods(...$methods);
27
    }
28
29
    /**
30
     * Returns the list of methods
31
     *
32
     * @return string[]
33
     */
34
    public function getMethods()
35
    {
36
        return $this->methods;
37
    }
38
39
    /**
40
     * Add new HTTP methods to the list
41
     *
42
     * @param string[] ...$methods
43
     *
44
     * return $this
45
     */
46
    public function addMethods(...$methods)
47
    {
48
        // uppercase and exclude empties
49
        $methods = array_reduce(
50
            $methods,
51
            static function &(&$result, $method) {
52
                $method = strtoupper(trim($method));
53
                if (strlen($method)) {
54
                    $result[] = $method;
55
                }
56
                return $result;
57
            },
58
            []
59
        );
60
61
        foreach ($methods as $method) {
62
            if (!in_array($method, $this->methods, true)) {
63
                $this->methods[] = $method;
64
            }
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * Returns true if the current process is running in CLI mode
72
     *
73
     * @param HTTPRequest $request
74
     *
75
     * @return bool
76
     */
77
    public function checkRequestForBypass(HTTPRequest $request)
78
    {
79
        return in_array($request->httpMethod(), $this->methods, true);
80
    }
81
}
82