AcceptMethods::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
namespace Schnittstabil\Psr7\Csrf\Middlewares;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Schnittstabil\Psr7\Csrf\RequestAttributesTrait;
8
9
/**
10
 * Middleware for whitelisting certain HTTP methods.
11
 */
12
class AcceptMethods
13
{
14
    use RequestAttributesTrait;
15
16
    /**
17
     * HTTP methods allowed to bypass CSRF protection.
18
     *
19
     * @var string[]
20
     */
21
    protected $methods;
22
23
    /**
24
     * Create new AcceptMethods middleware.
25
     *
26
     * @param string[] $methods HTTP methods allowed to bypass CSRF protection
27
     */
28
    public function __construct(array $methods = array('GET', 'OPTIONS'))
29
    {
30
        $this->methods = array_map('strtoupper', $methods);
31
    }
32
33
    /**
34
     * Invoke middleware.
35
     *
36
     * @param ServerRequestInterface $request  request object
37
     * @param ResponseInterface      $response response object
38
     * @param callable               $next     next middleware
39
     *
40
     * @return ResponseInterface response object
41
     */
42
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
43
    {
44
        if (in_array($request->getMethod(), $this->methods)) {
45
            $request = $request->withAttribute(self::$isValidAttribute, true);
46
        }
47
48
        return $next($request, $response);
49
    }
50
}
51