HttpStatusCode   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 1
b 0
f 0
dl 0
loc 59
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A code() 0 3 1
A description() 0 3 1
A search() 0 8 3
A __construct() 0 18 3
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Domain\Security\Http;
13
14
/**
15
 * HttpStatusCode
16
 *
17
 * @package Slick\WebStack\Domain\Security\Http
18
 */
19
final class HttpStatusCode
20
{
21
22
    /**
23
     * @var array<array{code: int, description: string}>
24
     */
25
    private static array $codes = [];
26
27
    private int $code;
28
    private string $description;
29
30
    public function __construct(int $statusCode)
31
    {
32
        $statusCodesJson = file_get_contents(__DIR__ . "/http_codes.json");
33
        $code = null;
34
35
        if ($statusCodesJson) {
36
            self::$codes = json_decode($statusCodesJson, true);
37
            $code = $this->search($statusCode);
38
        }
39
40
41
        if (!$code) {
42
            $this->code = $statusCode;
43
            $this->description = "Unknown HTTP status code: $statusCode";
44
            return;
45
        }
46
        $this->code = $code['code'];
47
        $this->description = $code['description'];
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function code(): int
54
    {
55
        return $this->code;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function description(): string
62
    {
63
        return $this->description;
64
    }
65
66
    /**
67
     * @param int $statusCode
68
     * @return array{code: int, description: string}|null
69
     */
70
    private function search(int $statusCode): ?array
71
    {
72
        foreach (self::$codes as $code) {
73
            if ($code['code'] === $statusCode) {
74
                return $code;
75
            }
76
        }
77
        return null;
78
    }
79
}
80