Passed
Branch master (099444)
by Wanderson
01:24
created

ErrorPage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setError() 0 10 2
A isErrorPage() 0 2 1
A validate() 0 3 2
A stopControllerIf403() 0 3 3
A app() 0 2 1
1
<?php
2
3
namespace Win\Mvc;
4
5
use Win\Html\Seo\Title;
6
7
/**
8
 * Paginas de Erro
9
 */
10
class ErrorPage {
11
12
	public static $pages = [
13
		404 => 'Página não encontrada',
14
		401 => 'Não autorizado',
15
		403 => 'Acesso negado',
16
		500 => 'Erro no Servidor',
17
		503 => 'Problemas de Conexão'
18
	];
19
20
	/**
21
	 * Chama pageNotFound se o usuário acessar /404
22
	 *
23
	 * Isso garante que todas as funcionalidades de pageNotFound serão executadas
24
	 * mesmo se a página existente 404 for acessada
25
	 */
26
	public static function validate() {
27
		if (key_exists((int) static::app()->getParam(0), static::$pages)):
28
			Application::app()->pageNotFound();
29
		endif;
30
	}
31
32
	/**
33
	 * Define a página como Página de Erro
34
	 * @param int $errorCode
35
	 */
36
	public static function setError($errorCode) {
37
		if (key_exists($errorCode, static::$pages)):
38
			static::stopControllerIf403($errorCode);
39
			static::app()->setPage((string) $errorCode);
40
			static::app()->view = new View($errorCode);
41
42
			static::app()->controller = ControllerFactory::create('Error' . $errorCode);
43
			static::app()->view->addData('title', Title::otimize(static::$pages[$errorCode]));
44
			http_response_code($errorCode);
45
			static::app()->controller->reload();
46
		endif;
47
	}
48
49
	/**
50
	 * Trava o carregamento do Controller, se ele definir um erro 403
51
	 * Isso evita que códigos sem permissões de acesso nunca sejam executados
52
	 * @param int $errorCode
53
	 */
54
	private static function stopControllerIf403($errorCode) {
55
		if ($errorCode == 403 && static::app()->getParam(0) !== (string) $errorCode):
56
			$this->redirect(403 . '/index/' . static::app()->getUrl());
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
Bug introduced by
The method redirect() does not exist on Win\Mvc\ErrorPage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
			$this->/** @scrutinizer ignore-call */ 
57
          redirect(403 . '/index/' . static::app()->getUrl());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
		endif;
58
	}
59
60
	/** @return Application */
61
	protected static function app() {
62
		return Application::app();
63
	}
64
65
	/** @return boolean */
66
	public static function isErrorPage() {
67
		return (boolean) (key_exists((int) static::app()->getPage(), static::$pages));
68
	}
69
70
}
71