Test Failed
Push — master ( b22efa...070fdf )
by Felipe
04:16 queued 02:04
created

StatusCodeMiddleware::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of coisa/http.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\Http\Middleware;
15
16
use Fig\Http\Message\StatusCodeInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
22
/**
23
 * Class StatusCodeMiddleware
24
 *
25
 * @package CoiSA\Http\Middleware
26
 */
27
final class StatusCodeMiddleware implements MiddlewareInterface
28
{
29
    /**
30
     * @var int
31
     */
32
    private $statusCode;
33
34
    /**
35
     * StatusCodeMiddleware constructor.
36
     *
37
     * @param int $statusCode
38
     *
39
     * @throws \ReflectionException
40
     */
41
    public function __construct(int $statusCode)
42
    {
43
        $reflection    = new \ReflectionClass(StatusCodeInterface::class);
44
        $allowedStatus = $reflection->getConstants();
45
46
        if (!\in_array($statusCode, $allowedStatus)) {
47
            throw new \UnexpectedValueException('Invalid HTTP Status Code');
48
        }
49
50
        $this->statusCode = $statusCode;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
57
    {
58
        return $handler->handle($request)->withStatus($this->statusCode);
59
    }
60
}
61