Headers   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 40
c 1
b 0
f 0
dl 0
loc 61
ccs 6
cts 9
cp 0.6667
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A codeToHeader() 0 7 3
A setCustomCode() 0 3 1
1
<?php
2
3
namespace kalanis\kw_address_handler;
4
5
6
/**
7
 * Class Headers
8
 * @package kalanis\kw_address_handler
9
 * Working with headers
10
 */
11
class Headers
12
{
13
    /** @var array<int, string> */
14
    protected static array $headerCodes = [
15
        200 => '200 OK',
16
        201 => '201 Created',
17
        202 => '202 Accepted',
18
        203 => '203 Non-Authoritative Information',
19
        204 => '204 No Content',
20
        205 => '205 Reset Content',
21
        206 => '206 Partial Content',
22
        400 => '400 Bad Request',
23
        401 => '401 Unauthorized',
24
        403 => '403 Forbidden',
25
        404 => '404 Not Found',
26
        405 => '405 Method Not Allowed',
27
        406 => '406 Not Acceptable',
28
        407 => '407 Proxy Authentication Required',
29
        408 => '408 Request Timeout',
30
        409 => '409 Conflict',
31
        410 => '410 Gone',
32
        411 => '411 Length Required',
33
        413 => '413 Payload Too Large',
34
        414 => '414 URI Too Long',
35
        415 => '415 Unsupported Media Type',
36
        416 => '416 Range Not Satisfiable',
37
        417 => '417 Expectation Failed',
38
        418 => '418 I\'m a teapot',
39
        429 => '429 Too Many Requests',
40
        451 => '451 Unavailable For Legal Reasons',
41
        500 => '500 Internal Server Error',
42
        501 => '501 Not Implemented',
43
        502 => '502 Bad Gateway',
44
        503 => '503 Service Unavailable',
45
        504 => '504 Gateway Timeout',
46
        505 => '505 HTTP Version Not Supported',
47
    ];
48
49
    /**
50
     * @param string $protocol
51
     * @param int $code
52
     * @codeCoverageIgnore access external
53
     */
54
    public static function setCustomCode(string $protocol, int $code): void
55
    {
56
        header($protocol . ' ' . static::codeToHeader($code));
57
    }
58
59
    /**
60
     * @param int $code
61
     * @param int $default
62
     * @return string
63
     * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
64
     */
65 4
    public static function codeToHeader(int $code, int $default = 404): string
66
    {
67 4
        return isset(static::$headerCodes[$code])
68 2
            ? static::$headerCodes[$code]
69 2
            : (isset(static::$headerCodes[$default])
70 1
                ? static::$headerCodes[$default]
71 4
                : static::$headerCodes[500]
72
            );
73
    }
74
}
75