Statusable::isSuccessful()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Muzzle\Messages;
4
5
use Muzzle\HttpStatus;
6
7
trait Statusable
8
{
9
10
    /**
11
     * Gets the response status code.
12
     *
13
     * @return int Status code.
14
     */
15
    abstract public function getStatusCode();
16
17
    /**
18
     * Retrieves a message header value by the given case-insensitive name.
19
     *
20
     * @param string $name Case-insensitive header field name.
21
     * @return string[] An array of string values as provided for the given header.
22
     */
23
    abstract public function getHeader($name);
24
25
    /**
26
     * Is response invalid?
27
     *
28
     * @return bool
29
     *
30
     * @see   http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
31
     *
32
     * @final since version 3.2
33
     */
34
    public function isInvalid()
35
    {
36
37
        return $this->getStatusCode() < HttpStatus::CONTINUE || $this->getStatusCode() >= 600;
38
    }
39
40
    /**
41
     * Is response informative?
42
     *
43
     * @return bool
44
     *
45
     * @final since version 3.3
46
     */
47
    public function isInformational()
48
    {
49
50
        return $this->getStatusCode() >= HttpStatus::CONTINUE && $this->getStatusCode() < HttpStatus::OK;
51
    }
52
53
    /**
54
     * Is response successful?
55
     *
56
     * @return bool
57
     *
58
     * @final since version 3.2
59
     */
60
    public function isSuccessful()
61
    {
62
63
        return $this->getStatusCode() >= HttpStatus::OK && $this->getStatusCode() < HttpStatus::MULTIPLE_CHOICES;
64
    }
65
66
    /**
67
     * Is the response a redirect?
68
     *
69
     * @return bool
70
     *
71
     * @final since version 3.2
72
     */
73
    public function isRedirection()
74
    {
75
76
        return
77
            $this->getStatusCode() >= HttpStatus::MULTIPLE_CHOICES
78
            && $this->getStatusCode() < HttpStatus::BAD_REQUEST;
79
    }
80
81
    /**
82
     * Is there a client error?
83
     *
84
     * @return bool
85
     *
86
     * @final since version 3.2
87
     */
88
    public function isClientError()
89
    {
90
91
        return
92
            $this->getStatusCode() >= HttpStatus::BAD_REQUEST
93
            && $this->getStatusCode() < HttpStatus::INTERNAL_SERVER_ERROR;
94
    }
95
96
    /**
97
     * Was there a server side error?
98
     *
99
     * @return bool
100
     *
101
     * @final since version 3.3
102
     */
103
    public function isServerError()
104
    {
105
106
        return $this->getStatusCode() >= HttpStatus::INTERNAL_SERVER_ERROR && $this->getStatusCode() < 600;
107
    }
108
109
    /**
110
     * Is the response OK?
111
     *
112
     * @return bool
113
     *
114
     * @final since version 3.2
115
     */
116
    public function isOk()
117
    {
118
119
        return HttpStatus::OK === $this->getStatusCode();
120
    }
121
122
    /**
123
     * Is the response forbidden?
124
     *
125
     * @return bool
126
     *
127
     * @final since version 3.2
128
     */
129
    public function isForbidden()
130
    {
131
132
        return HttpStatus::FORBIDDEN === $this->getStatusCode();
133
    }
134
135
    /**
136
     * Is the response a not found error?
137
     *
138
     * @return bool
139
     *
140
     * @final since version 3.2
141
     */
142
    public function isNotFound()
143
    {
144
145
        return HttpStatus::NOT_FOUND === $this->getStatusCode();
146
    }
147
148
    /**
149
     * Is the response a redirect of some form?
150
     *
151
     * @param string $location
152
     *
153
     * @return bool
154
     *
155
     * @final since version 3.2
156
     */
157
    public function isRedirect($location = null)
158
    {
159
160
        $redirectStatuses = [
161
            HttpStatus::CREATED,
162
            HttpStatus::MOVED_PERMANENTLY,
163
            HttpStatus::FOUND,
164
            HttpStatus::SEE_OTHER,
165
            HttpStatus::TEMPORARY_REDIRECT,
166
            HttpStatus::PERMANENTLY_REDIRECT,
167
        ];
168
        return
169
            in_array($this->getStatusCode(), $redirectStatuses)
170
            && (null === $location ?: in_array($location, $this->getHeader('Location')));
171
    }
172
173
    /**
174
     * Is the response empty?
175
     *
176
     * @return bool
177
     *
178
     * @final since version 3.2
179
     */
180
    public function isEmpty()
181
    {
182
183
        return in_array($this->getStatusCode(), [HttpStatus::NO_CONTENT, HttpStatus::NOT_MODIFIED]);
184
    }
185
}
186