StatusCodeMiddleware::process()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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 94
    public function __construct(int $statusCode)
42
    {
43 94
        $reflection    = new \ReflectionClass(StatusCodeInterface::class);
44 94
        $allowedStatus = $reflection->getConstants();
45
46 94
        if (!\in_array($statusCode, $allowedStatus)) {
47 1
            throw new \UnexpectedValueException('Invalid HTTP Status Code');
48
        }
49
50 94
        $this->statusCode = $statusCode;
51 94
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 75
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
57
    {
58 75
        return $handler->handle($request)->withStatus($this->statusCode);
59
    }
60
}
61