Passed
Push — master ( 1cb3cc...989b89 )
by Wanderson
02:14 queued 57s
created

Controller::setView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Mvc;
4
5
use Win\Request\Url;
6
7
/**
8
 * Controllers
9
 *
10
 * São responsáveis por processar as requisições e definir as Views
11
 */
12
abstract class Controller {
13
14
	public static $dir = '/app/controller';
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
		$this->app = Application::app();
40
		$this->action = $action;
41
	}
42
43
	/** @param string $title */
44
	public function setTitle($title) {
45
		$this->addData('title', $title);
46
	}
47
48
	/**
49
	 * Adiciona uma variável para usar na View
50
	 * @param string $name
51
	 * @param mixed $value
52
	 */
53
	public function addData($name, $value) {
54
		$this->data[$name] = $value;
55
	}
56
57
	/**
58
	 * Retorna uma variável do Controller
59
	 * @param string $name
60
	 * @return mixed|null
61
	 */
62
	public function getData($name) {
63
		if (key_exists($name, $this->data)) {
64
			return $this->data[$name];
65
		}
66
		return null;
67
	}
68
69
	/** @param View|mixed $view */
70
	protected function setView($view) {
71
		if ($view instanceof View) {
72
			$this->app->view = $view;
73
		}
74
	}
75
76
	/** @return string */
77
	public function getAction() {
78
		return $this->action;
79
	}
80
81
	/**
82
	 * Carrega o Controller,
83
	 * executando o Action atual
84
	 */
85
	public function load() {
86
		$this->init();
87
		$action = $this->action;
88
		$view = $this->$action();
89
		$this->setView($view);
90
		$this->app->view->validate();
91
		$this->app->view->mergeData($this->data);
92
	}
93
94
	/**
95
	 * Volta para o método index da pagina atual
96
	 * @codeCoverageIgnore
97
	 */
98
	protected function backToIndex() {
99
		Url::instance()->redirect($this->app->getPage());
100
	}
101
102
	/**
103
	 * Action Index
104
	 */
105
	abstract public function index();
106
107
	/**
108
	 * Este método é chamado sempre que o Controller é carregado
109
	 */
110
	protected function init() {
111
		
112
	}
113
114
	/**
115
	 * Redireciona para a URL
116
	 * @param string $url
117
	 */
118
	public function redirect($url) {
119
		Url::instance()->redirect($url);
120
	}
121
122
	/**
123
	 * Atualiza a mesma página
124
	 * @param string $url
125
	 */
126
	public function refresh() {
127
		Url::instance()->redirect(Url::instance()->getUrl());
128
	}
129
130
	/**
131
	 * Ao chamar um método inexistente retorna um 404
132
	 * @param string $name
133
	 * @param mixed[] $arguments
134
	 * @return boolean
135
	 */
136
	public function __call($name, $arguments) {
137
		if ($this->app->getPage() !== '404') {
138
			$this->app->pageNotFound();
139
		}
140
	}
141
142
}
143