Passed
Push — master ( 8ceacb...8c98e8 )
by Jean-Christophe
15:18
created

Controller::getDefaultViewName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Ubiquity\controllers;
4
5
use Ubiquity\views\View;
6
use Ubiquity\exceptions\RouterException;
7
use Ubiquity\themes\ThemesManager;
8
use Ubiquity\domains\DDDManager;
9
10
/**
11
 * Base class for controllers.
12
 * Ubiquity\controllers$Controller
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.7
17
 *
18
 */
19
abstract class Controller {
20
21
	/**
22
	 * The view
23
	 *
24
	 * @var View
25
	 */
26
	protected $view;
27
28
	/**
29
	 * Default action
30
	 */
31
	abstract public function index();
32
33
	/**
34
	 * Constructor
35
	 * initialize $view variable
36
	 */
37 50
	public function __construct() {
38 50
		$this->view = new View ();
39 50
	}
40
41
	/**
42
	 * Method called before each action
43
	 * Can be override in derived class
44
	 */
45 23
	public function initialize() {
46 23
	}
47
48
	/**
49
	 * Method called after each action
50
	 * Can be override in derived class
51
	 */
52 39
	public function finalize() {
53 39
	}
54
55
	/**
56
	 * Loads the view $viewName possibly passing the variables $pdata
57
	 *
58
	 * @param string $viewName The name of the view to load
59
	 * @param mixed $pData Variable or associative array to pass to the view
60
	 *        If a variable is passed, it will have the name **$data** in the view,
61
	 *        If an associative array is passed, the view retrieves variables from the table's key names
62
	 * @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable)
63
	 * @throws \Exception
64
	 * @return string null or the view content if **$asString** parameter is true
65
	 */
66 30
	public function loadView($viewName, $pData = NULL, $asString = false) {
67 30
		if (isset ( $pData )) {
68 23
			$this->view->setVars ( $pData );
69
		}
70 30
		return $this->view->render ( $viewName, $asString );
71
	}
72
73
	/**
74
	 * Loads the default view (controllerName/actionName) possibly passing the variables $pdata.
75
	 * (@activeTheme/controllerName/actionName if themes are activated)
76
	 *
77
	 * @param mixed $pData Variable or associative array to pass to the view
78
	 *        If a variable is passed, it will have the name **$data** in the view,
79
	 *        If an associative array is passed, the view retrieves variables from the table's key names
80
	 * @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable)
81
	 * @throws \Exception
82
	 * @return string null or the view content if **$asString** parameter is true
83
	 */
84 4
	public function loadDefaultView($pData = NULL, $asString = false) {
85 4
		return $this->loadView ( $this->getDefaultViewName (), $pData, $asString );
86
	}
87
88
	/**
89
	 * Returns the default view name for this controller/action i.e ControllerName/actionName.html for the action actionName in ControllerName
90
	 * If there is an activeTheme **@activeTheme/ControllerName/actionName.html**
91
	 *
92
	 * @return string the default view name
93
	 */
94 4
	public function getDefaultViewName() {
95 4
		if (ThemesManager::getActiveTheme () !== '') {
96 4
			return '@activeTheme/' . Startup::getControllerSimpleName () . "/" . Startup::getAction () . "." . Startup::getViewNameFileExtension ();
97
		}
98
		return DDDManager::getViewNamespace().Startup::getControllerSimpleName () . "/" . Startup::getAction () . "." . Startup::getViewNameFileExtension ();
99
	}
100
101
	/**
102
	 * Returns True if access to the controller is allowed
103
	 * To be override in sub classes
104
	 *
105
	 * @param string $action
106
	 * @return boolean
107
	 */
108 52
	public function isValid($action) {
109 52
		return true;
110
	}
111
112
	/**
113
	 * Called if isValid () returns false
114
	 * To be override in sub classes
115
	 */
116 5
	public function onInvalidControl() {
117 5
		if (! \headers_sent ()) {
118 5
			\header ( 'HTTP/1.1 401 Unauthorized', true, 401 );
119
		}
120 5
	}
121
122
	/**
123
	 * Loads the controller $controller and calls its $action method by passing the parameters $params
124
	 *
125
	 * @param string $controller The Controller
126
	 * @param string $action The action to call
127
	 * @param mixed $params Parameters passed to the **$action** method
128
	 * @param boolean $initialize If true, the controller's initialize method is called before $action
129
	 * @param boolean $finalize If true, the controller's finalize method is called after $action
130
	 * @throws \Exception
131
	 */
132 2
	public function forward($controller, $action = 'index', $params = [], $initialize = false, $finalize = false) {
133 2
		$u = ['controller'=>$controller,'action'=>$action ];
134 2
		if (\is_array ( $params )) {
135 2
			$u['params']= $params;
136
		} else {
137
			$u['params'] =[ $params ];
138
		}
139 2
		Startup::runAction ( $u, $initialize, $finalize );
140 2
	}
141
142
	/**
143
	 * Redirect to a route by its name
144
	 *
145
	 * @param string $routeName The route name
146
	 * @param array $parameters The parameters to pass to the route action
147
	 * @param boolean $initialize Call the **initialize** method if true
148
	 * @param boolean $finalize Call the **finalize** method if true
149
	 * @throws RouterException
150
	 */
151 1
	public function redirectToRoute($routeName, $parameters = [ ], $initialize = false, $finalize = false) {
152 1
		$infos = Router::getRouteInfoByName ( $routeName );
153 1
		if ($infos !== false) {
154 1
			if (isset ( $infos ['controller'] )) {
155 1
				$this->forward ( $infos ['controller'], $infos ['action'] ?? 'index', $parameters, $initialize, $finalize );
156
			} else {
157
				$method = \strtolower ( $_SERVER ['REQUEST_METHOD'] );
158
				if (isset ( $infos [$method] )) {
159
					$infos = $infos [$method];
160
					$this->forward ( $infos ['controller'], $infos ['action'] ?? 'index', $parameters, $initialize, $finalize );
161
				} else {
162 1
					throw new RouterException ( "Route {$routeName} not found for method {$method}", 404 );
163
				}
164
			}
165
		} else {
166
			throw new RouterException ( "Route {$routeName} not found", 404 );
167
		}
168 1
	}
169
170
	/**
171
	 * Returns the associated view instance
172
	 *
173
	 * @return \Ubiquity\views\View
174
	 */
175 21
	public function getView() {
176 21
		return $this->view;
177
	}
178
}
179