Status::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php namespace Comodojo\Dispatcher\Response;
2
3
use \Comodojo\Dispatcher\Traits\ToStringTrait;
4
use \Comodojo\Dispatcher\Components\HttpStatusCodes;
5
use \Exception;
6
7
/**
8
 * @package     Comodojo Dispatcher
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @author      Marco Castiello <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
class Status {
25
26
    use ToStringTrait;
27
28
    private $status_code = 200;
29
30
    private $codes;
31
32 7
    public function __construct() {
33
34 7
        $this->codes = new HttpStatusCodes;
35
36 7
    }
37
38 8
    public function get() {
39
40 8
        return $this->status_code;
41
42
    }
43
44 5
    public function set($code) {
45
46 5
        if (!$this->codes->exists($code)) {
47
48
            throw new Exception("Invalid HTTP Status Code $code");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $code instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
49
50
        }
51
52 5
        $this->status_code = $code;
53
54 5
        return $this;
55
56
    }
57
58 1
    public function description($code=null) {
59
60 1
        if ( is_null($code) ) return $this->codes->getMessage($this->status_code);
61
62 1
        return $this->codes->getMessage($code);
63
64
    }
65
66
}
67