Passed
Push — master ( 5fb311...42b16f )
by Zlatin
02:02
created

Response   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 128
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getReasonPhrase() 0 3 1
A withStatus() 0 16 4
A __construct() 0 5 1
A getStatusCode() 0 3 1
1
<?php
2
namespace DevOp\Core\Http;
3
4
use Psr\Http\Message\ResponseInterface;
5
6
class Response extends Message implements ResponseInterface
7
{
8
9
    /**
10
     * @link https://gist.github.com/Stoffo/53e093450aed067a8fa8
11
     * */
12
    private static $phrases = array(
13
        100 => 'Continue',
14
        101 => 'Switching Protocols',
15
        102 => 'Processing',
16
        200 => 'OK',
17
        201 => 'Created',
18
        202 => 'Accepted',
19
        203 => 'Non-Authoritative Information',
20
        204 => 'No Content',
21
        205 => 'Reset Content',
22
        206 => 'Partial Content',
23
        207 => 'Multi-Status',
24
        300 => 'Multiple Choices',
25
        301 => 'Moved Permanently',
26
        302 => 'Found',
27
        303 => 'See Other',
28
        304 => 'Not Modified',
29
        305 => 'Use Proxy',
30
        306 => 'Switch Proxy',
31
        307 => 'Temporary Redirect',
32
        400 => 'Bad Request',
33
        401 => 'Unauthorized',
34
        402 => 'Payment Required',
35
        403 => 'Forbidden',
36
        404 => 'Not Found',
37
        405 => 'Method Not Allowed',
38
        406 => 'Not Acceptable',
39
        407 => 'Proxy Authentication Required',
40
        408 => 'Request Timeout',
41
        409 => 'Conflict',
42
        410 => 'Gone',
43
        411 => 'Length Required',
44
        412 => 'Precondition Failed',
45
        413 => 'Request Entity Too Large',
46
        414 => 'Request-URI Too Long',
47
        415 => 'Unsupported Media Type',
48
        416 => 'Requested Range Not Satisfiable',
49
        417 => 'Expectation Failed',
50
        418 => 'I\'m a teapot',
51
        422 => 'Unprocessable Entity',
52
        423 => 'Locked',
53
        424 => 'Failed Dependency',
54
        425 => 'Unordered Collection',
55
        426 => 'Upgrade Required',
56
        449 => 'Retry With',
57
        450 => 'Blocked by Windows Parental Controls',
58
        500 => 'Internal Server Error',
59
        501 => 'Not Implemented',
60
        502 => 'Bad Gateway',
61
        503 => 'Service Unavailable',
62
        504 => 'Gateway Timeout',
63
        505 => 'HTTP Version Not Supported',
64
        506 => 'Variant Also Negotiates',
65
        507 => 'Insufficient Storage',
66
        509 => 'Bandwidth Limit Exceeded',
67
        510 => 'Not Extended'
68
    );
69
70
    /**
71
     *
72
     * @var int
73
     */
74
    private $statusCode;
75
76
    /**
77
     *
78
     * @var string
79
     */
80
    private $reasonPhrase;
81
82
    /**
83
     * @param string $body
84
     * @param int $status
85
     * @param array $headers
86
     */
87 2
    public function __construct($body = 'php://memory', $status = 200, array $headers = [])
88
    {
89 2
        $this->setHeaders($headers);
90 2
        $this->statusCode = (int) $status;
91 2
        $this->body = $this->getStream($body, "wb+");
0 ignored issues
show
Bug introduced by
The property body is declared private in DevOp\Core\Http\Message and cannot be accessed from this context.
Loading history...
92
        
93 2
    }
94
    
95
    /**
96
     *
97
     * @var int
98
     */
99 2
    public function getStatusCode()
100
    {
101 2
        return $this->statusCode;
102
    }
103
104
    /**
105
     * @param int $code
106
     * @param string $reasonPhrase
107
     * @return self
108
     */
109 2
    public function withStatus($code, $reasonPhrase = '')
110
    {
111 2
        if ($code === $this->statusCode) {
112
            return $this;
113
        }
114
115 2
        $clone = clone $this;
116 2
        $clone->statusCode = $code;
117
118 2
        if (empty($reasonPhrase) && isset(self::$phrases[$code])) {
119 2
            $reasonPhrase = self::$phrases[$code];
120
        }
121
122 2
        $clone->reasonPhrase = $reasonPhrase;
123
124 2
        return $clone;
125
    }
126
127
    /**
128
     *
129
     * @var string
130
     */
131
    public function getReasonPhrase()
132
    {
133
        return $this->reasonPhrase;
134
    }
135
}
136