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 | * Método que devuelve los administradores de una plataforma |
||
| 177 | * @return array|null |
||
| 178 | */ |
||
| 179 | 4 | public function getAdmins() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 186 | * |
||
| 187 | * @param null $user |
||
| 188 | * @param null $pass |
||
| 189 | * @param boolean $force |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | * @throws \HttpException |
||
| 193 | */ |
||
| 194 | 3 | public function checkAdmin($user = NULL, $pass = NULL, $force = false) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 1 | protected function getAdminFromCookie() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Método privado para la generación del hash de la cookie de administración |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 1 | public function getHash() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Método que devuelve el usuario logado |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | 2 | public function getUser() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Método que devuelve el usuario administrador logado |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | 2 | public function getAdmin() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Método que calcula si se está logado o para acceder a administración |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 3 | public function canAccessRestrictedAdmin() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 290 | * |
||
| 291 | * @param string|null $route |
||
| 292 | * |
||
| 293 | * @return string|null |
||
| 294 | */ |
||
| 295 | public function notAuthorized($route) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Servicio que chequea si un usuario es super administrador o no |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 1 | public function isSuperAdmin() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Servicio que devuelve un dato de sesión |
||
| 320 | * |
||
| 321 | * @param string $key |
||
| 322 | * |
||
| 323 | * @return mixed |
||
| 324 | */ |
||
| 325 | 6 | public function getSessionKey($key) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Servicio que setea una variable de sesión |
||
| 337 | * |
||
| 338 | * @param string $key |
||
| 339 | * @param mixed $data |
||
| 340 | * |
||
| 341 | * @return Security |
||
| 342 | */ |
||
| 343 | 5 | public function setSessionKey($key, $data = NULL) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 352 | * @return mixed |
||
| 353 | */ |
||
| 354 | 2 | public function getFlashes() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Servicio que limpia los mensajes flash |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | 3 | public function clearFlashes() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Servicio que inserta un flash en sesión |
||
| 374 | * |
||
| 375 | * @param string $key |
||
| 376 | * @param mixed $data |
||
| 377 | */ |
||
| 378 | 1 | public function setFlash($key, $data = NULL) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Servicio que devuelve un flash de sesión |
||
| 390 | * |
||
| 391 | * @param string $key |
||
| 392 | * |
||
| 393 | * @return mixed |
||
| 394 | */ |
||
| 395 | 2 | public function getFlash($key) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Servicio que actualiza |
||
| 404 | * |
||
| 405 | * @param boolean $closeSession |
||
| 406 | * |
||
| 407 | * @return Security |
||
| 408 | */ |
||
| 409 | 2 | public function updateSession($closeSession = FALSE) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Servicio que limpia la sesión |
||
| 426 | */ |
||
| 427 | 1 | public function closeSession() |
|
| 434 | |||
| 435 | } |
||
| 436 |
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: