HttpStatus   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 125
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getStatus() 0 5 1
A parseStatus() 0 5 1
A toStatusLine() 0 4 1
A __toString() 0 4 1
A getCode() 0 4 1
A getProtocol() 0 4 1
1
<?php
2
/**
3
 * This file is part of the PHP Generics package.
4
 *
5
 * @package Generics
6
 */
7
namespace Generics\Client;
8
9
/**
10
 * This class provides helper for status code to status message conversion
11
 *
12
 * @author Maik
13
 */
14
class HttpStatus
15
{
16
17
    const STATUS_200 = 'OK';
18
19
    const STATUS_301 = 'Moved Permanently';
20
21
    const STATUS_302 = 'Found';
22
23
    const STATUS_400 = 'Bad Request';
24
25
    const STATUS_401 = 'Unauthorized';
26
27
    const STATUS_403 = 'Forbidden';
28
29
    const STATUS_404 = 'Not Found';
30
31
    const STATUS_500 = 'Internal Server Error';
32
33
    const STATUS_501 = 'Not Implemented';
34
35
    const STATUS_502 = 'Bad Gateway';
36
37
    const STATUS_503 = 'Service Unavailable';
38
39
    const STATUS_505 = 'HTTP Version Not Supported';
40
41
    /**
42
     * The status code
43
     *
44
     * @var int
45
     */
46
    private $code;
47
48
    /**
49
     * The protocol version
50
     *
51
     * @var string
52
     */
53
    private $proto;
54
55
    /**
56
     * Create a new HttpStatus instance
57
     *
58
     * @param int $code
59
     *            The status code
60
     * @param string $protocolVersion
61
     *            The version of Http protocol (e.g. 1.0)
62
     */
63 19
    public function __construct($code, $protocolVersion = '1.1')
64
    {
65 19
        $this->code = $code;
66 19
        $this->proto = $protocolVersion;
67 19
    }
68
69
    /**
70
     * Retrieve the status message for a particular code
71
     *
72
     * @param int $code
73
     *
74
     * @return string The status message
75
     */
76 2
    public static function getStatus($code): string
77
    {
78 2
        $prop = sprintf("Generics\\Client\\HttpStatus::STATUS_%d", $code);
79 2
        return constant($prop);
80
    }
81
82
    /**
83
     * Parse the status line into its parts
84
     *
85
     * @param string $statusLine
86
     *
87
     * @return HttpStatus
88
     */
89 19
    public static function parseStatus($statusLine): HttpStatus
90
    {
91 19
        list ($proto, $code) = sscanf($statusLine, "%s %d %s");
92 19
        return new HttpStatus($code, $proto);
93
    }
94
95
    /**
96
     * Retrieve the status line
97
     *
98
     * @return string The status line according RFC
99
     * @see http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p1-messaging-latest.html#rfc.section.3.1.2
100
     */
101 1
    public function toStatusLine(): string
102
    {
103 1
        return sprintf("%s %d %s", $this->proto, $this->code, self::getStatus($this->code));
104
    }
105
106
    /**
107
     * Retrieve the status as string
108
     * It is a wrapper for
109
     *
110
     * @see \Generics\Client\HttpStatus::toStatusLine()
111
     *
112
     * @return string
113
     */
114 1
    public function __toString(): string
115
    {
116 1
        return $this->toStatusLine();
117
    }
118
119
    /**
120
     * Get the status code
121
     *
122
     * @return int
123
     */
124 19
    public function getCode(): int
125
    {
126 19
        return $this->code;
127
    }
128
129
    /**
130
     * Get the protocol including version
131
     *
132
     * @return string
133
     */
134 1
    public function getProtocol(): string
135
    {
136 1
        return $this->proto;
137
    }
138
}
139