Test Failed
Push — 1.0.0-dev ( c7a39c...6c2ad2 )
by nguereza
03:29
created

Controller::loadRequiredResources()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 14
rs 9.9332
1
<?php
2
	defined('ROOT_PATH') || exit('Access denied');
3
	/**
4
	 * TNH Framework
5
	 *
6
	 * A simple PHP framework using HMVC architecture
7
	 *
8
	 * This content is released under the GNU GPL License (GPL)
9
	 *
10
	 * Copyright (C) 2017 Tony NGUEREZA
11
	 *
12
	 * This program is free software; you can redistribute it and/or
13
	 * modify it under the terms of the GNU General Public License
14
	 * as published by the Free Software Foundation; either version 3
15
	 * of the License, or (at your option) any later version.
16
	 *
17
	 * This program is distributed in the hope that it will be useful,
18
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
	 * GNU General Public License for more details.
21
	 *
22
	 * You should have received a copy of the GNU General Public License
23
	 * along with this program; if not, write to the Free Software
24
	 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
	*/
26
27
	class Controller{
28
		
29
		/**
30
		 * The name of the module if this controller belong to an module
31
		 * @var string
32
		 */
33
		public $moduleName = null;
34
35
		/**
36
		 * The singleton of the super object
37
		 * @var Controller
38
		 */
39
		private static $instance;
40
41
		/**
42
		 * The logger instance
43
		 * @var Log
44
		 */
45
		protected $logger;
46
47
		/**
48
		 * Class constructor
49
		 * @param object $logger the Log instance to use if is null will create one
50
		 */
51
		public function __construct(Log $logger = null){
0 ignored issues
show
Unused Code introduced by
The parameter $logger is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
		public function __construct(/** @scrutinizer ignore-unused */ Log $logger = null){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
			//setting the Log instance
53
			$this->setLoggerFromParamOrCreateNewInstance(null);
54
			
55
			//instance of the super object
56
			self::$instance = & $this;
57
			
58
			//load the required resources
59
			$this->loadRequiredResources();
60
			
61
			//set the cache using the configuration
62
			$this->setCacheFromParamOrConfig(null);
63
			
64
			//set application session configuration
65
			$this->logger->debug('Setting PHP application session handler');
66
			set_session_config();
67
			
68
			//set module using the router
69
			$this->setModuleNameFromRouter();
70
71
			//dispatch the loaded instance of super controller event
72
			$this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED');
73
		}
74
75
76
		/**
77
		 * This is a very useful method it's used to get the super object instance
78
		 * @return Controller the super object instance
79
		 */
80
		public static function &get_instance(){
81
			return self::$instance;
82
		}
83
84
		/**
85
		 * This method is used to set the module name
86
		 */
87
		protected function setModuleNameFromRouter(){
88
			//determine the current module
89
			if(isset($this->router) && $this->router->getModule()){
90
				$this->moduleName = $this->router->getModule();
91
			}
92
		}
93
94
		/**
95
		 * Set the cache using the argument otherwise will use the configuration
96
		 * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured
97
		 */
98
		protected function setCacheFromParamOrConfig(CacheInterface $cache = null){
99
			$this->logger->debug('Setting the cache handler instance');
100
			//set cache handler instance
101
			if(get_config('cache_enable', false)){
102
				if ($cache !== null){
103
					$this->cache = $cache;
0 ignored issues
show
Bug Best Practice introduced by
The property cache does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
104
				} else if (isset($this->{strtolower(get_config('cache_handler'))})){
105
					$this->cache = $this->{strtolower(get_config('cache_handler'))};
106
					unset($this->{strtolower(get_config('cache_handler'))});
107
				} 
108
			}
109
		}
110
111
		/**
112
		 * Set the Log instance using argument or create new instance
113
		 * @param object $logger the Log instance if not null
114
		 */
115
		protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){
116
			if($logger !== null){
117
	          $this->logger = $logger;
118
	        }
119
	        else{
120
	            $this->logger =& class_loader('Log', 'classes');
121
				$this->logger->setLogger('MainController');
122
	        }
123
		}
124
125
		/**
126
		 * This method is used to load the required resources for framework to work
127
		 * @return void 
128
		 */
129
		private function loadRequiredResources(){
130
			$this->logger->debug('Adding the loaded classes to the super instance');
131
			foreach (class_loaded() as $var => $class){
132
				$this->$var =& class_loader($class);
133
			}
134
135
			$this->logger->debug('Loading the required classes into super instance');
136
			$this->eventdispatcher =& class_loader('EventDispatcher', 'classes');
0 ignored issues
show
Bug Best Practice introduced by
The property eventdispatcher does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
137
			$this->loader =& class_loader('Loader', 'classes');
138
			$this->lang =& class_loader('Lang', 'classes');
139
			$this->request =& class_loader('Request', 'classes');
140
			//dispatch the request instance created event
141
			$this->eventdispatcher->dispatch('REQUEST_CREATED');
142
			$this->response =& class_loader('Response', 'classes', 'classes');
143
		}
144
145
	}
146