Total Complexity | 41 |
Total Lines | 328 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Init often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Init, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | final class Init extends ModuleBase |
||
58 | { |
||
59 | /** |
||
60 | * List of controllers that don't need to perform fully initialization |
||
61 | * like: install/database checks, session/event handlers initialization |
||
62 | */ |
||
63 | const PARTIAL_INIT = ['resource', 'install', 'bootstrap', 'status', 'upgrade', 'error']; |
||
64 | /** |
||
65 | * List of controllers that don't need to update the user's session activity |
||
66 | */ |
||
67 | const NO_SESSION_ACTIVITY = ['items']; |
||
68 | |||
69 | /** |
||
70 | * @var SessionContext |
||
71 | */ |
||
72 | private $context; |
||
73 | /** |
||
74 | * @var ThemeInterface |
||
75 | */ |
||
76 | private $theme; |
||
77 | /** |
||
78 | * @var Language |
||
79 | */ |
||
80 | private $language; |
||
81 | /** |
||
82 | * @var SecureSessionService |
||
83 | */ |
||
84 | private $secureSessionService; |
||
85 | /** |
||
86 | * @var PluginManager |
||
87 | */ |
||
88 | private $pluginManager; |
||
89 | /** |
||
90 | * @var ItemPresetService |
||
91 | */ |
||
92 | private $itemPresetService; |
||
93 | /** |
||
94 | * @var bool |
||
95 | */ |
||
96 | private $isIndex = false; |
||
97 | |||
98 | /** |
||
99 | * Init constructor. |
||
100 | * |
||
101 | * @param ContainerInterface $container |
||
102 | */ |
||
103 | public function __construct(ContainerInterface $container) |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Initialize Web App |
||
117 | * |
||
118 | * @param string $controller |
||
119 | * |
||
120 | * @throws \DI\DependencyException |
||
121 | * @throws \DI\NotFoundException |
||
122 | * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException |
||
123 | * @throws \SP\Core\Exceptions\ConstraintException |
||
124 | * @throws \SP\Core\Exceptions\QueryException |
||
125 | * @throws \SP\Core\Exceptions\SPException |
||
126 | * @throws \SP\Repositories\NoSuchItemException |
||
127 | * @throws \Exception |
||
128 | */ |
||
129 | public function initialize($controller) |
||
130 | { |
||
131 | logger(__METHOD__); |
||
132 | |||
133 | $this->isIndex = $controller === 'index'; |
||
134 | |||
135 | // Iniciar la sesión de PHP |
||
136 | $this->initSession($this->configData->isEncryptSession()); |
||
137 | |||
138 | // Volver a cargar la configuración si se recarga la página |
||
139 | if ($this->request->checkReload() === false) { |
||
140 | // Cargar la configuración |
||
141 | $this->config->loadConfig($this->context); |
||
142 | |||
143 | // Cargar el lenguaje |
||
144 | $this->language->setLanguage(); |
||
145 | |||
146 | // Initialize theme |
||
147 | $this->theme->initTheme(); |
||
148 | } else { |
||
149 | logger('Browser reload'); |
||
150 | |||
151 | $this->context->setAppStatus(SessionContext::APP_STATUS_RELOADED); |
||
152 | |||
153 | // Cargar la configuración |
||
154 | $this->config->loadConfig($this->context, true); |
||
155 | |||
156 | // Restablecer el idioma |
||
157 | $this->language->setLanguage(true); |
||
158 | |||
159 | // Re-Initialize theme |
||
160 | $this->theme->initTheme(true); |
||
161 | } |
||
162 | |||
163 | // Comprobar si es necesario cambiar a HTTPS |
||
164 | HttpUtil::checkHttps($this->configData, $this->request); |
||
165 | |||
166 | if (in_array($controller, self::PARTIAL_INIT, true) === false) { |
||
167 | $databaseUtil = $this->container->get(DatabaseUtil::class); |
||
168 | |||
169 | // Checks if sysPass is installed |
||
170 | if (!$this->checkInstalled()) { |
||
171 | logger('Not installed', 'ERROR'); |
||
172 | |||
173 | $this->router->response() |
||
174 | ->redirect('index.php?r=install/index') |
||
175 | ->send(); |
||
176 | } |
||
177 | |||
178 | // Checks if the database is set up |
||
179 | if (!$databaseUtil->checkDatabaseConnection()) { |
||
180 | logger('Database connection error', 'ERROR'); |
||
181 | |||
182 | $this->router->response() |
||
183 | ->redirect('index.php?r=error/databaseConnection') |
||
184 | ->send(); |
||
185 | } |
||
186 | |||
187 | // Checks if maintenance mode is turned on |
||
188 | if ($this->checkMaintenanceMode($this->context)) { |
||
189 | logger('Maintenance mode', 'INFO'); |
||
190 | |||
191 | $this->router->response() |
||
192 | ->redirect('index.php?r=error/maintenanceError') |
||
193 | ->send(); |
||
194 | } |
||
195 | |||
196 | // Checks if upgrade is needed |
||
197 | if ($this->checkUpgrade()) { |
||
198 | logger('Upgrade needed', 'ERROR'); |
||
199 | |||
200 | $this->config->generateUpgradeKey(); |
||
201 | |||
202 | $this->router->response() |
||
203 | ->redirect('index.php?r=upgrade/index') |
||
204 | ->send(); |
||
205 | } |
||
206 | |||
207 | // Checks if the database is set up |
||
208 | if (!$databaseUtil->checkDatabaseTables($this->configData->getDbName())) { |
||
209 | logger('Database checking error', 'ERROR'); |
||
210 | |||
211 | $this->router->response() |
||
212 | ->redirect('index.php?r=error/databaseError') |
||
213 | ->send(); |
||
214 | } |
||
215 | |||
216 | // Initialize event handlers |
||
217 | $this->initEventHandlers(); |
||
218 | |||
219 | if (!in_array($controller, self::NO_SESSION_ACTIVITY)) { |
||
220 | // Initialize user session context |
||
221 | $this->initUserSession(); |
||
222 | } |
||
223 | |||
224 | // Load plugins |
||
225 | $this->pluginManager->loadPlugins(); |
||
226 | |||
227 | if ($this->context->isLoggedIn() |
||
228 | && $this->context->getAppStatus() === SessionContext::APP_STATUS_RELOADED |
||
|
|||
229 | ) { |
||
230 | logger('Reload user profile'); |
||
231 | // Recargar los permisos del perfil de usuario |
||
232 | $this->context->setUserProfile( |
||
233 | $this->container->get(UserProfileService::class) |
||
234 | ->getById($this->context->getUserData() |
||
235 | ->getUserProfileId())->getProfile()); |
||
236 | } |
||
237 | |||
238 | return; |
||
239 | } |
||
240 | |||
241 | // Do not keep the PHP's session opened |
||
242 | SessionContext::close(); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Iniciar la sesión PHP |
||
247 | * |
||
248 | * @param bool $encrypt Encriptar la sesión de PHP |
||
249 | * |
||
250 | * @throws \Exception |
||
251 | */ |
||
252 | private function initSession($encrypt = false) |
||
253 | { |
||
254 | if ($encrypt === true |
||
255 | && Bootstrap::$checkPhpVersion |
||
256 | && ($key = $this->secureSessionService->getKey(UUIDCookie::factory($this->request))) !== false) { |
||
257 | session_set_save_handler(new CryptSessionHandler($key), true); |
||
258 | } |
||
259 | |||
260 | |||
261 | try { |
||
262 | $this->context->initialize(); |
||
263 | } catch (\Exception $e) { |
||
264 | $this->router->response()->header('HTTP/1.1', '500 Internal Server Error'); |
||
265 | |||
266 | throw $e; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Comprueba que la aplicación esté instalada |
||
272 | * Esta función comprueba si la aplicación está instalada. Si no lo está, redirige al instalador. |
||
273 | */ |
||
274 | private function checkInstalled() |
||
275 | { |
||
276 | return $this->configData->isInstalled() |
||
277 | && $this->router->request()->param('r') !== 'install/index'; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Comprobar si es necesario actualizar componentes |
||
282 | */ |
||
283 | private function checkUpgrade() |
||
284 | { |
||
285 | return $this->configData->getUpgradeKey() |
||
286 | || (UpgradeDatabaseService::needsUpgrade($this->configData->getDatabaseVersion()) || |
||
287 | UpgradeAppService::needsUpgrade(UpgradeUtil::fixVersionNumber($this->configData->getConfigVersion()))); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Inicializar la sesión de usuario |
||
292 | * |
||
293 | */ |
||
294 | private function initUserSession() |
||
295 | { |
||
296 | $lastActivity = $this->context->getLastActivity(); |
||
297 | $inMaintenance = $this->configData->isMaintenance(); |
||
298 | |||
299 | // Timeout de sesión |
||
300 | if ($lastActivity > 0 |
||
301 | && !$inMaintenance |
||
302 | && time() > ($lastActivity + $this->getSessionLifeTime()) |
||
303 | ) { |
||
304 | if ($this->router->request()->cookies()->get(session_name()) !== null) { |
||
305 | $this->router->response()->cookie(session_name(), '', time() - 42000); |
||
306 | } |
||
307 | |||
308 | SessionContext::restart(); |
||
309 | } else { |
||
310 | $sidStartTime = $this->context->getSidStartTime(); |
||
311 | |||
312 | // Regenerar el Id de sesión periódicamente para evitar fijación |
||
313 | if ($sidStartTime === 0) { |
||
314 | // Intentar establecer el tiempo de vida de la sesión en PHP |
||
315 | @ini_set('session.gc_maxlifetime', $this->getSessionLifeTime()); |
||
316 | |||
317 | $this->context->setSidStartTime(time()); |
||
318 | $this->context->setStartActivity(time()); |
||
319 | } else if (!$inMaintenance |
||
320 | && time() > ($sidStartTime + SessionContext::MAX_SID_TIME) |
||
321 | && $this->context->isLoggedIn() |
||
322 | ) { |
||
323 | try { |
||
324 | CryptSession::reKey($this->context); |
||
325 | } catch (CryptoException $e) { |
||
326 | logger($e->getMessage()); |
||
327 | |||
328 | SessionContext::restart(); |
||
329 | return; |
||
330 | } |
||
331 | } |
||
332 | |||
333 | $this->context->setLastActivity(time()); |
||
334 | } |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Obtener el timeout de sesión desde la configuración. |
||
339 | * |
||
340 | * @return int con el tiempo en segundos |
||
341 | */ |
||
342 | private function getSessionLifeTime() |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param int $default |
||
363 | * |
||
364 | * @return int |
||
365 | * @throws \SP\Core\Exceptions\ConstraintException |
||
366 | * @throws \SP\Core\Exceptions\InvalidArgumentException |
||
367 | * @throws \SP\Core\Exceptions\NoSuchPropertyException |
||
368 | * @throws \SP\Core\Exceptions\QueryException |
||
369 | */ |
||
370 | private function getSessionTimeoutForUser(int $default = null) |
||
385 | } |
||
386 | } |