Complex classes like Security 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Security, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Security |
||
| 14 | { |
||
| 15 | // sha1('user') |
||
| 16 | const USER_ID_TOKEN = '12dea96fec20593566ab75692c9949596833adc9'; |
||
| 17 | // sha1('admin') |
||
| 18 | const MANAGER_ID_TOKEN = 'd033e22ae348aeb5660fc2140aec35850c4da997'; |
||
| 19 | // sha1('superadmin') |
||
| 20 | const ADMIN_ID_TOKEN = '889a3a791b3875cfae413574b53da4bb8a90d53e'; |
||
| 21 | // sha1('FLASHES') |
||
| 22 | const FLASH_MESSAGE_TOKEN = '4680c68435db1bfbf17c3fcc4f7b39d2c6122504'; |
||
| 23 | const LOGGED_USER_TOKEN = '__U_T_L__'; |
||
| 24 | |||
| 25 | use SecureTrait; |
||
| 26 | use SingletonTrait; |
||
| 27 | /** |
||
| 28 | * @var array $user |
||
| 29 | */ |
||
| 30 | private $user = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array $admin |
||
| 34 | */ |
||
| 35 | private $admin = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool $authorized |
||
| 39 | */ |
||
| 40 | private $authorized = FALSE; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool $checked |
||
| 44 | */ |
||
| 45 | private $checked = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array $session |
||
| 49 | */ |
||
| 50 | protected $session; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor por defecto |
||
| 54 | */ |
||
| 55 | 2 | public function init() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Initializator for SESSION |
||
| 73 | */ |
||
| 74 | 2 | private function initSession() { |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Método estático que devuelve los perfiles de la plataforma |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | 2 | public static function getProfiles() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Method that returns all the available profiles |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | 1 | public function getAdminProfiles() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Método estático que devuelve los perfiles disponibles |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | 2 | public static function getCleanProfiles() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Método estático que devuelve los perfiles disponibles |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | 1 | public function getAdminCleanProfiles() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Método que guarda los administradores |
||
| 130 | * |
||
| 131 | * @param array $user |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | 1 | public static function save($user) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Method to save a new admin user |
||
| 153 | * @param array $user |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | 1 | public function saveUser($user) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Servicio que actualiza los datos del usuario |
||
| 167 | * |
||
| 168 | * @param $user |
||
| 169 | */ |
||
| 170 | 1 | public function updateUser($user) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param $alias |
||
| 177 | * @param $profile |
||
| 178 | */ |
||
| 179 | 1 | public function updateAdmin($alias, $profile) { |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Método que devuelve los administradores de una plataforma |
||
| 189 | * @return array|null |
||
| 190 | */ |
||
| 191 | 4 | public function getAdmins() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 198 | * |
||
| 199 | * @param null $user |
||
| 200 | * @param null $pass |
||
| 201 | * @param boolean $force |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | * @throws \HttpException |
||
| 205 | */ |
||
| 206 | 3 | public function checkAdmin($user = NULL, $pass = NULL, $force = false) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | 1 | protected function getAdminFromCookie() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Método privado para la generación del hash de la cookie de administración |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 1 | public function getHash() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Método que devuelve el usuario logado |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | 2 | public function getUser() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Método que devuelve el usuario administrador logado |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | 2 | public function getAdmin() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Método que calcula si se está logado o para acceder a administración |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 3 | public function canAccessRestrictedAdmin() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 298 | * |
||
| 299 | * @param string|null $route |
||
| 300 | * |
||
| 301 | * @return string|null |
||
| 302 | */ |
||
| 303 | public function notAuthorized($route) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Servicio que chequea si un usuario es super administrador o no |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 1 | public function isSuperAdmin() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Servicio que devuelve un dato de sesión |
||
| 328 | * |
||
| 329 | * @param string $key |
||
| 330 | * |
||
| 331 | * @return mixed |
||
| 332 | */ |
||
| 333 | 6 | public function getSessionKey($key) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Servicio que setea una variable de sesión |
||
| 345 | * |
||
| 346 | * @param string $key |
||
| 347 | * @param mixed $data |
||
| 348 | * |
||
| 349 | * @return Security |
||
| 350 | */ |
||
| 351 | 5 | public function setSessionKey($key, $data = NULL) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | 2 | public function getFlashes() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Servicio que limpia los mensajes flash |
||
| 371 | * @return $this |
||
| 372 | */ |
||
| 373 | 3 | public function clearFlashes() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Servicio que inserta un flash en sesión |
||
| 382 | * |
||
| 383 | * @param string $key |
||
| 384 | * @param mixed $data |
||
| 385 | */ |
||
| 386 | 1 | public function setFlash($key, $data = NULL) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Servicio que devuelve un flash de sesión |
||
| 398 | * |
||
| 399 | * @param string $key |
||
| 400 | * |
||
| 401 | * @return mixed |
||
| 402 | */ |
||
| 403 | 2 | public function getFlash($key) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Servicio que actualiza |
||
| 412 | * |
||
| 413 | * @param boolean $closeSession |
||
| 414 | * |
||
| 415 | * @return Security |
||
| 416 | */ |
||
| 417 | 2 | public function updateSession($closeSession = FALSE) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Servicio que limpia la sesión |
||
| 434 | */ |
||
| 435 | 1 | public function closeSession() |
|
| 442 | |||
| 443 | } |
||
| 444 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: