Completed
Push — master ( 50d640...921733 )
by Iqbal
04:07
created

HttpStatusTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 4 Features 0
Metric Value
wmc 19
c 4
b 4
f 0
lcom 1
cbo 2
dl 0
loc 161
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setStatusCode() 0 13 4
A isValid() 0 4 1
A isInformational() 0 4 1
A isSuccessful() 0 4 1
A isRedirection() 0 4 1
A isClientError() 0 4 1
A isServerError() 0 4 1
A isOk() 0 4 1
A isForbidden() 0 4 1
A isNotFound() 0 4 1
A isEmpty() 0 4 1
A isNotModified() 0 4 1
A getStatusCode() 0 8 2
A getStatusText() 0 8 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http;
12
13
use Borobudur\Http\Exception\InvalidArgumentException;
14
15
/**
16
 * @author      Iqbal Maulana <[email protected]>
17
 * @created     8/5/15
18
 */
19
trait HttpStatusTrait
20
{
21
    /**
22
     * Get status code.
23
     *
24
     * @return int
25
     */
26
    public function getStatusCode()
27
    {
28
        if (property_exists($this, 'statusCode')) {
29
            return $this->{'statusCode'};
30
        }
31
32
        return null;
33
    }
34
35
    /**
36
     * Set status code.
37
     *
38
     * @param int $statusCode
39
     *
40
     * @return $this
41
     */
42
    public function setStatusCode($statusCode)
43
    {
44
        if (false === HttpStatus::isValid($statusCode)) {
45
            throw new InvalidArgumentException(sprintf('Invalid status code "%s".', $statusCode));
46
        }
47
48
        if (property_exists($this, 'statusCode') && property_exists($this, 'statusText')) {
49
            $this->{'statusCode'} = $statusCode = (int) $statusCode;
50
            $this->{'statusText'} = HttpStatus::getStatusText($statusCode, '');
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * Get status text.
58
     *
59
     * @return string
60
     */
61
    public function getStatusText()
62
    {
63
        if (property_exists($this, 'statusText')) {
64
            return $this->{'statusText'};
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * Check if current response is valid status code.
72
     *
73
     * @return bool
74
     */
75
    public function isValid()
76
    {
77
        return HttpStatus::isValid($this->getStatusCode());
78
    }
79
80
    /**
81
     * Check if current response is informational.
82
     *
83
     * @return bool
84
     */
85
    public function isInformational()
86
    {
87
        return HttpStatus::isInformational($this->getStatusCode());
88
    }
89
90
    /**
91
     * Check if current response is successful.
92
     *
93
     * @return bool
94
     */
95
    public function isSuccessful()
96
    {
97
        return HttpStatus::isSuccessful($this->getStatusCode());
98
    }
99
100
    /**
101
     * Check if current response is redirection.
102
     *
103
     * @return bool
104
     */
105
    public function isRedirection()
106
    {
107
        return HttpStatus::isRedirection($this->getStatusCode());
108
    }
109
110
    /**
111
     * Check if current response is client error.
112
     *
113
     * @return bool
114
     */
115
    public function isClientError()
116
    {
117
        return HttpStatus::isClientError($this->getStatusCode());
118
    }
119
120
    /**
121
     * Check if current response is server error.
122
     *
123
     * @return bool
124
     */
125
    public function isServerError()
126
    {
127
        return HttpStatus::isServerError($this->getStatusCode());
128
    }
129
130
    /**
131
     * Check if current response is ok.
132
     *
133
     * @return bool
134
     */
135
    public function isOk()
136
    {
137
        return HttpStatus::isOk($this->getStatusCode());
138
    }
139
140
    /**
141
     * Check if current response is forbidden.
142
     *
143
     * @return bool
144
     */
145
    public function isForbidden()
146
    {
147
        return HttpStatus::isForbidden($this->getStatusCode());
148
    }
149
150
    /**
151
     * Check if current response is not found.
152
     *
153
     * @return bool
154
     */
155
    public function isNotFound()
156
    {
157
        return HttpStatus::isNotFound($this->getStatusCode());
158
    }
159
160
    /**
161
     * Check if current response is empty.
162
     *
163
     * @return bool
164
     */
165
    public function isEmpty()
166
    {
167
        return HttpStatus::isEmpty($this->getStatusCode());
168
    }
169
170
    /**
171
     * Check if response is not modified.
172
     *
173
     * @return bool
174
     */
175
    public function isNotModified()
176
    {
177
        return HttpStatus::HTTP_NOT_MODIFIED === $this->getStatusCode();
178
    }
179
}
180