Completed
Push — master ( 86682d...7d26a6 )
by Jean-Christophe
01:50
created

Controller   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 110
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
index() 0 1 ?
A __construct() 0 5 2
A initialize() 0 2 1
A finalize() 0 2 1
A loadView() 0 5 2
A isValid() 0 3 1
A onInvalidControl() 0 3 1
A forward() 0 9 2
A redirectToRoute() 0 13 3
1
<?php
2
3
/**
4
 * This file is part of Ubiquity framework
5
 *
6
 */
7
namespace Ubiquity\controllers;
8
9
use Ubiquity\views\View;
10
use Ubiquity\exceptions\RouterException;
11
12
/**
13
 * Base class for controllers
14
 *
15
 * @author jcheron
16
 * @version 1.0.3
17
 */
18
abstract class Controller {
19
	/**
20
	 * The view
21
	 *
22
	 * @var View
23
	 */
24
	protected $view;
25
26
	/**
27
	 * Default action
28
	 */
29
	abstract public function index();
30
31
	/**
32
	 * Constructor<br>
33
	 * Automatically invokes the isValid () method to check if access is allowed
34
	 */
35
	public function __construct() {
36
		if (!$this->isValid())
37
			$this->onInvalidControl();
38
		$this->view=new View();
39
	}
40
41
	/**
42
	 * Method called before each action
43
	 * Can be override in derived class
44
	 */
45
	public function initialize() {
46
	}
47
48
	/**
49
	 * Method called after each action
50
	 * Can be override in derived class
51
	 */
52
	public function finalize() {
53
	}
54
55
	/**
56
	 * Loads the view $viewName possibly passing the variables $pdata
57
	 * @param string $viewName view name to load
58
	 * @param mixed $pData Variable or associative array to pass to the view <br> If a variable is passed, it will have the name <b> $ data </ b> in the view, <br>
59
	 * If an associative array is passed, the view retrieves variables from the table's key names
60
	 * @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable)
61
	 * @throws \Exception
62
	 * @return string
63
	 */
64
	public function loadView($viewName, $pData=NULL, $asString=false) {
65
		if (isset($pData))
66
			$this->view->setVars($pData);
67
		return $this->view->render($viewName, $asString);
68
	}
69
70
	/**
71
	 * Returns True if access to the controller is allowed
72
	 * To be override in sub classes
73
	 * @return boolean
74
	 */
75
	public function isValid() {
76
		return true;
77
	}
78
79
	/**
80
	 * Called if isValid () returns false <br>
81
	 * To be override in sub classes
82
	 */
83
	public function onInvalidControl() {
84
		\header('HTTP/1.1 401 Unauthorized', true, 401);
85
	}
86
87
	/**
88
	 * Loads the controller $controller and calls its $action method by passing the parameters $params
89
	 * @param string $controller The Controller
90
	 * @param string $action The action to call
91
	 * @param mixed $params Parameters passed to the $action method
92
	 * @param boolean $initialize If true, the controller's initialize method is called before $action
93
	 * @param boolean $finalize If true, the controller's finalize method is called after $action
94
	 * @throws \Exception
95
	 */
96
	public function forward($controller, $action="index", $params=array(), $initialize=false, $finalize=false) {
97
		$u=array ($controller,$action );
98
		if (\is_array($params)) {
99
			$u=\array_merge($u, $params);
100
		} else {
101
			$u=\array_merge($u, [ $params ]);
102
		}
103
		return Startup::runAction($u, $initialize, $finalize);
104
	}
105
106
	/**
107
	 * Redirect to a route by its name
108
	 * @param string $routeName
109
	 * @param array $parameters
110
	 * @param boolean $initialize
111
	 * @param boolean $finalize
112
	 * @throws RouterException
113
	 */
114
	public function redirectToRoute($routeName,$parameters=[],$initialize=false,$finalize=false){
115
		$path=Router::getRouteByName($routeName,$parameters);
116
		if($path!==false){
117
			$route=Router::getRoute($path,false);
118
			if($route!==false){
119
				return $this->forward($route[0],$route[1],\array_slice($route, 2),$initialize,$finalize);
120
			}else{
121
				throw new RouterException("Route {$routeName} not found",404);
122
			}
123
		}else{
124
			throw new RouterException("Route {$routeName} not found",404);
125
		}
126
	}
127
}
128