Completed
Push — master ( 6b8416...c520c0 )
by Filipe
02:12
created

Response   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 88
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getStatusCode() 0 4 1
A withStatus() 0 6 1
A getReasonPhrase() 0 4 1
A setStatus() 0 7 2
1
<?php
2
3
/**
4
 * This file is part of slick/http
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
namespace Slick\Http\Message;
11
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\StreamInterface;
14
15
/**
16
 * Response
17
 *
18
 * @package Slick\Http\Message
19
*/
20
class Response extends Message implements ResponseInterface
21
{
22
    /**
23
     * @var int
24
     */
25
    private $status;
26
27
    /**
28
     * @var string
29
     */
30
    private $reasonPhrase;
31
32
    /**
33
     * Creates an HTTP Response message
34
     *
35
     * @param int                    $status
36
     * @param string|StreamInterface $body
37
     * @param array                  $headers
38
     */
39
    public function __construct($status, $body = '', array $headers = [])
40
    {
41
        parent::__construct($body);
0 ignored issues
show
Bug introduced by
It seems like $body defined by parameter $body on line 39 can also be of type object<Psr\Http\Message\StreamInterface>; however, Slick\Http\Message\Message::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
43
        $this->setStatus($status);
44
45
        foreach ($headers as $name => $header) {
46
            $this->headers[$name] = [$header];
47
        }
48
    }
49
50
    /**
51
     * Gets the response status code.
52
     *
53
     * The status code is a 3-digit integer result code of the server's attempt
54
     * to understand and satisfy the request.
55
     *
56
     * @return int Status code.
57
     */
58
    public function getStatusCode()
59
    {
60
        return $this->status;
61
    }
62
63
    /**
64
     * Return an instance with the specified status code and, optionally, reason phrase.
65
     *
66
     * @link http://tools.ietf.org/html/rfc7231#section-6
67
     * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
68
     * @param int $code The 3-digit integer result code to set.
69
     * @param string $reasonPhrase The reason phrase to use with the
70
     *     provided status code; if none is provided, implementations MAY
71
     *     use the defaults as suggested in the HTTP specification.
72
     * @return static
73
     * @throws \InvalidArgumentException For invalid status code arguments.
74
     */
75
    public function withStatus($code, $reasonPhrase = '')
76
    {
77
        $response = clone $this;
78
        $response->setStatus($code, $reasonPhrase);
79
        return $response;
80
    }
81
82
    /**
83
     * Gets the response reason phrase associated with the status code.
84
     *
85
     * @link http://tools.ietf.org/html/rfc7231#section-6
86
     * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
87
     * @return string Reason phrase; must return an empty string if none present.
88
     */
89
    public function getReasonPhrase()
90
    {
91
        return $this->reasonPhrase;
92
    }
93
94
    /**
95
     * Sets the response status code
96
     *
97
     * @param int    $status
98
     * @param string $reasonPhrase
99
     */
100
    private function setStatus($status, $reasonPhrase = '')
101
    {
102
        HttpCodes::check($status);
103
        $this->status = intval($status, 10);
104
        if ($reasonPhrase === '')
105
            $this->reasonPhrase = HttpCodes::reasonPhraseFor($this->status);
106
    }
107
}