Completed
Push — master ( ccb78a...e0e64f )
by Nazar
04:41
created

ExitException::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 3
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	Exception;
11
12
class ExitException extends Exception {
13
	/**
14
	 * @var bool
15
	 */
16
	protected $json = false;
17
	/**
18
	 * ExitException constructor.
19
	 *
20
	 * @param int|string|string[] $message Error message (or code if no message)
21
	 * @param int                 $code    HTTP status code
22
	 * @param Exception|null      $previous
23
	 */
24 4
	function __construct ($message = '', $code = 0, Exception $previous = null) {
25 4
		parent::__construct('', $code, $previous);
26 4
		if (is_numeric($message) && !$code) {
27 4
			$this->code = $message;
28
		} else {
29 2
			$this->message = $message;
30
		}
31
		/**
32
		 * Make sure code is always the same in `cs\ExitException` and `cs\Response` instances
33
		 */
34 4
		$Response = Response::instance();
35 4
		if ($this->code) {
36 4
			$Response->code = $this->code;
37
		} else {
38 2
			$this->code = $Response->code;
39
		}
40 4
	}
41
	/**
42
	 * @return bool
43
	 */
44 4
	function getJson () {
45 4
		return $this->json;
46
	}
47
	/**
48
	 * Specify that error should be in JSON format
49
	 *
50
	 * @return $this
51
	 */
52 2
	function setJson () {
53 2
		$this->json = true;
54 2
		return $this;
55
	}
56
}
57