Reject::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
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\Helpers\StreamFactory;
8
9
/**
10
 * A 403 Access Forbidden middleware.
11
 */
12
class Reject
13
{
14
    /**
15
     * Used to create streams.
16
     *
17
     * @var callable
18
     */
19
    protected $streamFactory;
20
21
    /**
22
     * Create new Reject middleware.
23
     *
24
     * @param callable $streamFactory Defaults to `new StreamFactory`
25
     */
26
    public function __construct(callable $streamFactory = null)
27
    {
28
        $this->streamFactory = $streamFactory ?: new StreamFactory();
29
    }
30
31
    /**
32
     * Invoke middleware.
33
     *
34
     * @param ServerRequestInterface $request  request object
35
     * @param ResponseInterface      $response response object
36
     * @param callable               $next     next middleware
37
     *
38
     * @return ResponseInterface response object
39
     *
40
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
41
     */
42
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
43
    {
44
        $body = call_user_func($this->streamFactory);
45
        $body->write('403 Access Forbidden, bad CSRF token');
46
47
        return $response->withStatus(403)->withHeader('Content-type', 'text/plain')->withBody($body);
48
    }
49
}
50