Passed
Branch v1.4.0 (c6dc92)
by Wanderson
01:17
created

Controller::actionNotFound()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Mvc;
4
5
use Win\Request\Url;
6
7
/**
8
 * Controller
9
 *
10
 * Responsável por processar as requisições e definir a View
11
 */
12
abstract class Controller
13
{
14
	public static $dir = '/app/controllers';
15
16
	/**
17
	 * Ponteiro para Aplicação Principal
18
	 * @var Application
19
	 */
20
	public $app;
21
22
	/** @var string */
23
	private $action;
24
25
	/** @var string */
26
	public $layout = 'main';
27
28
	/**
29
	 * Variáveis para serem usadas no arquivo da View
30
	 * @var mixed[]
31
	 */
32
	private $data = [];
33
34
	/**
35
	 * Cria o Controller, definindo o Action
36
	 * @param string $action
37
	 */
38
	public function __construct($action = 'index')
39
	{
40
		$this->app = Application::app();
41
		$this->action = $action;
42
	}
43
44
	/** @param string $title */
45
	public function setTitle($title)
46
	{
47
		$this->addData('title', $title);
48
	}
49
50
	/**
51
	 * Adiciona uma variável para usar na View
52
	 * @param string $name
53
	 * @param mixed $value
54
	 */
55
	public function addData($name, $value)
56
	{
57
		$this->data[$name] = $value;
58
	}
59
60
	/**
61
	 * Retorna uma variável do Controller
62
	 * @param string $name
63
	 * @return mixed|null
64
	 */
65
	public function getData($name)
66
	{
67
		if (key_exists($name, $this->data)) {
68
			return $this->data[$name];
69
		}
70
71
		return null;
72
	}
73
74
	/** @param View|mixed $view */
75
	protected function setView($view)
76
	{
77
		if ($view instanceof View) {
78
			$this->app->view = $view;
79
		}
80
	}
81
82
	/** @return string */
83
	public function getAction()
84
	{
85
		return $this->action;
86
	}
87
88
	/**
89
	 * Carrega o Controller,
90
	 * executando o Action atual
91
	 */
92
	public function load()
93
	{
94
		$this->init();
95
		$action = $this->action;
96
		if (method_exists($this, $action)) {
97
			$view = $this->$action();
98
			$this->setView($view);
99
			$this->app->view->validate();
100
			$this->app->view->mergeData($this->data);
101
		} else {
102
			$this->actionNotFound();
103
		}
104
	}
105
106
	/**
107
	 * Define Página como 404
108
	 */
109
	protected function actionNotFound()
110
	{
111
		if ('404' !== $this->app->getPage()) {
112
			$this->app->pageNotFound();
113
		}
114
	}
115
116
	/**
117
	 * Volta para o método index da pagina atual
118
	 * @codeCoverageIgnore
119
	 */
120
	protected function backToIndex()
121
	{
122
		Url::instance()->redirect($this->app->getPage());
123
	}
124
125
	/**
126
	 * Action Index
127
	 */
128
	abstract public function index();
129
130
	/**
131
	 * Este método é chamado sempre que o Controller é carregado
132
	 */
133
	protected function init()
134
	{
135
	}
136
137
	/**
138
	 * Redireciona para a URL
139
	 * @param string $url
140
	 */
141
	public function redirect($url)
142
	{
143
		Url::instance()->redirect($url);
144
	}
145
146
	/**
147
	 * Atualiza a mesma página
148
	 * @param string $url
149
	 */
150
	public function refresh()
151
	{
152
		Url::instance()->redirect(Url::instance()->getUrl());
153
	}
154
}
155