Completed
Push — feature/code-analysis ( f93e28...a26fec )
by Jonathan
04:01
created

MvcException::setHttpCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * webtrees-lib: MyArtJaub library for webtrees
4
*
5
* @package MyArtJaub\Webtrees
6
* @subpackage Mvc
7
* @author Jonathan Jaubart <[email protected]>
8
* @copyright Copyright (c) 2016, Jonathan Jaubart
9
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
10
*/
11
namespace MyArtJaub\Webtrees\Mvc;
12
13
/**
14
 * Specific Exception for MyArtJaub Exception
15
 * @see \Exception
16
 */
17
class MvcException extends \Exception {
18
19
    /** @var int[] $VALID_HTTP List of valid HTTP codes */
20
    protected static $VALID_HTTP =  array(
21
        100, 101,
22
        200, 201, 202, 203, 204, 205, 206,
23
        300, 301, 302, 303, 304, 305, 306, 307,
24
        400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417,
25
        500, 501, 502, 503, 504, 505
26
    );
27
    
28
    /** @var int $http_code */
29
    protected $http_code;
30
    
31
    /**
32
     * Constructor for MvcException
33
     * 
34
     * @param int $http_code
35
     * @param string $message
36
     * @param int $code
37
     * @param \Throwable $previous
38
     */
39
    public function __construct($http_code = 500, $message = "", $code = 0, \Throwable $previous = null) {
40
        parent::__construct($message, $code, $previous);   
41
                
42
        $this->http_code = in_array($http_code, self::$VALID_HTTP) ? $http_code : 500;
43
    }
44
    
45
    /**
46
     * Get the HTTP code
47
     * 
48
     * @return int
49
     */
50
    public function getHttpCode() {
51
        return $this->http_code;
52
    }
53
    
54
    /**
55
     * Set the HTTP code
56
     * 
57
     * @param int $http_code
58
     * @throws InvalidArgumentException Thrown if not valid Http code
59
     */
60
    public function setHttpCode($http_code) {
61
        if(!in_array($http_code, self::$VALID_HTTP))
62
            throw new \InvalidArgumentException('Invalid HTTP code');
63
        $this->http_code= $http_code;
64
    }   
65
66
}
67
68