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