Passed
Branch tests1.5 (af713c)
by Wanderson
01:19
created

HttpException::retry()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Mvc;
4
5
use Exception;
6
7
/**
8
 * Resposta HTTP de Erro
9
 * 403, 404, 500, etc
10
 */
11
class HttpException extends Exception {
12
13
	private static $controller = 'errors';
14
15
	/**
16
	 * @param int $code
17
	 * @param string $message
18
	 */
19
	public function __construct($code, $message = '') {
20
		parent::__construct($message, $code);
21
	}
22
23
	/** Executa uma resposta HTTP de erro */
24
	public function run() {
25
		$app = Application::app();
26
		$app->setPage($this->code);
27
		$app->view = new View($this->code, ['exception' => $this]);
28
		$controller = ControllerFactory::create(static::$controller, '_' . $this->code);
0 ignored issues
show
Bug introduced by
Since $controller is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $controller to at least protected.
Loading history...
29
30
		if (get_class($controller) !== get_class($app->controller)) {
31
			$app->controller = $controller;
32
		}
33
		//var_dump('response error: ' . $this->code);
34
		http_response_code($this->code);
35
		$this->retry();
36
	}
37
38
	/** Define 404 caso definiu um erro inválido */
39
	protected function retry() {
40
		try {
41
			Application::app()->run();
42
		} catch (HttpException $e) {
43
			if (!$this->is404()) {
44
				$e->run();
45
			}
46
		}
47
	}
48
49
	private function is404() {
50
		return (Application::app()->getPage() == '404');
51
	}
52
53
	/** @return boolean */
54
	public static function isErrorCode($code) {
55
		return ((int) $code > 0);
56
	}
57
58
}
59