Passed
Push — master ( 7344eb...099444 )
by Wanderson
01:24
created

Router   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createCustomUrl() 0 11 3
A createController() 0 2 1
A load() 0 3 2
A hasCustomUrl() 0 2 1
A run() 0 7 2
A getCustomUrl() 0 2 1
1
<?php
2
3
namespace Win\Mvc;
4
5
use Win\DesignPattern\Singleton;
6
use Win\Helper\Url;
7
8
/**
9
 * Rota de URL
10
 * 
11
 * Redireciona a requisição para um outro Controller.
12
 * Veja as rotas em "config/routes.php"
13
 */
14
class Router {
15
16
	use Singleton;
17
18
	/** @var string[] */
19
	private $routes = [];
20
	public static $file = '/app/config/routes.php';
21
22
	/**
23
	 * Se for atribuído algum valor indica que é uma rota personalizada
24
	 * @var mixed[]
25
	 */
26
	protected static $customUrl = [null, null];
27
28
	/**
29
	 * Retorna a nova URL
30
	 * @return mixed[]
31
	 */
32
	public function getCustomUrl() {
33
		return static::$customUrl;
34
	}
35
36
	/**
37
	 * Retorna TRUE se a URL foi personalizada
38
	 * @return boolean
39
	 */
40
	public function hasCustomUrl() {
41
		return (!is_null(static::$customUrl[0]));
42
	}
43
44
	/**
45
	 * Inicia o processo de URL personalizada
46
	 * retornando TRUE se alguma rota foi encontrada
47
	 * @return boolean
48
	 */
49
	public function run() {
50
		$this->load();
51
		if (count($this->routes)) {
52
			static::$customUrl = static::createCustomUrl();
0 ignored issues
show
Bug Best Practice introduced by
The method Win\Mvc\Router::createCustomUrl() is not static, but was called statically. ( Ignorable by Annotation )

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

52
			/** @scrutinizer ignore-call */ 
53
   static::$customUrl = static::createCustomUrl();
Loading history...
53
			return $this->hasCustomUrl();
54
		}
55
		return false;
56
	}
57
58
	/** Carrega o arquivo que contem as rotas */
59
	private function load() {
60
		if (file_exists(BASE_PATH . static::$file)) {
61
			$this->routes = include BASE_PATH . static::$file;
62
		}
63
	}
64
65
	/**
66
	 * Percorre todas as rotas e retorna a nova URL
67
	 * @return string[] Nova URL [0 => controller, 1 => action]
68
	 */
69
	protected function createCustomUrl() {
70
		$search = ['', '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$10'];
71
		$matches = [];
72
		foreach ($this->routes as $url => $route):
73
			$exists = preg_match('@' . Url::instance()->format($url) . '$@', Url::instance()->getUrl(), $matches) == 1;
74
			if ($exists):
75
				$route = str_replace($search, $matches, $route) . '/';
76
				return explode('/', $route);
77
			endif;
78
		endforeach;
79
		return [null, null];
80
	}
81
82
	/**
83
	 * Cria um Controller de acordo com a nova URL
84
	 * @return Controller|DefaultController
85
	 */
86
	public function createController() {
87
		return ControllerFactory::create(static::$customUrl[0], static::$customUrl[1]);
88
	}
89
90
}
91