Passed
Branch master (f3f2b9)
by Jean-Christophe
17:39
created

Controller   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 42.22%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
eloc 34
c 2
b 0
f 0
dl 0
loc 157
ccs 19
cts 45
cp 0.4222
rs 10

11 Methods

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