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 | |||
| 24 | use SecureTrait; |
||
| 25 | use SingletonTrait; |
||
| 26 | /** |
||
| 27 | * @var array $user |
||
| 28 | */ |
||
| 29 | private $user = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array $admin |
||
| 33 | */ |
||
| 34 | private $admin = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool $authorized |
||
| 38 | */ |
||
| 39 | private $authorized = FALSE; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool $checked |
||
| 43 | */ |
||
| 44 | private $checked = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array $session |
||
| 48 | */ |
||
| 49 | protected $session; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor por defecto |
||
| 53 | */ |
||
| 54 | 2 | public function init() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Initializator for SESSION |
||
| 72 | */ |
||
| 73 | 2 | private function initSession() { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Método estático que devuelve los perfiles de la plataforma |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | 2 | public static function getProfiles() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Method that returns all the available profiles |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | 1 | public function getAdminProfiles() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Método estático que devuelve los perfiles disponibles |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | 2 | public static function getCleanProfiles() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Método estático que devuelve los perfiles disponibles |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | 1 | public function getAdminCleanProfiles() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Método que guarda los administradores |
||
| 129 | * |
||
| 130 | * @param array $user |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | 1 | public static function save($user) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Method to save a new admin user |
||
| 152 | * @param array $user |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | 1 | public function saveUser($user) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Servicio que actualiza los datos del usuario |
||
| 166 | * |
||
| 167 | * @param $user |
||
| 168 | */ |
||
| 169 | 1 | public function updateUser($user) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Método que devuelve los administradores de una plataforma |
||
| 176 | * @return array|null |
||
| 177 | */ |
||
| 178 | 4 | public function getAdmins() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 185 | * |
||
| 186 | * @param null $user |
||
| 187 | * @param null $pass |
||
| 188 | * @param boolean $force |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | * @throws \HttpException |
||
| 192 | */ |
||
| 193 | 3 | public function checkAdmin($user = NULL, $pass = NULL, $force = false) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | 1 | protected function getAdminFromCookie() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Método privado para la generación del hash de la cookie de administración |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 1 | public function getHash() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Método que devuelve el usuario logado |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | 2 | public function getUser() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Método que devuelve el usuario administrador logado |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | 2 | public function getAdmin() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Método que calcula si se está logado o para acceder a administración |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 3 | public function canAccessRestrictedAdmin() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 288 | * |
||
| 289 | * @param string|null $route |
||
| 290 | * |
||
| 291 | * @return string|null |
||
| 292 | */ |
||
| 293 | public function notAuthorized($route) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Servicio que chequea si un usuario es super administrador o no |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | 1 | public function isSuperAdmin() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Servicio que devuelve un dato de sesión |
||
| 318 | * |
||
| 319 | * @param string $key |
||
| 320 | * |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | 6 | public function getSessionKey($key) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Servicio que setea una variable de sesión |
||
| 335 | * |
||
| 336 | * @param string $key |
||
| 337 | * @param mixed $data |
||
| 338 | * |
||
| 339 | * @return Security |
||
| 340 | */ |
||
| 341 | 5 | public function setSessionKey($key, $data = NULL) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 350 | * @return mixed |
||
| 351 | */ |
||
| 352 | 2 | public function getFlashes() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Servicio que limpia los mensajes flash |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | 3 | public function clearFlashes() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Servicio que inserta un flash en sesión |
||
| 372 | * |
||
| 373 | * @param string $key |
||
| 374 | * @param mixed $data |
||
| 375 | */ |
||
| 376 | 1 | public function setFlash($key, $data = NULL) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Servicio que devuelve un flash de sesión |
||
| 388 | * |
||
| 389 | * @param string $key |
||
| 390 | * |
||
| 391 | * @return mixed |
||
| 392 | */ |
||
| 393 | 2 | public function getFlash($key) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Servicio que actualiza |
||
| 402 | * |
||
| 403 | * @param boolean $closeSession |
||
| 404 | * |
||
| 405 | * @return Security |
||
| 406 | */ |
||
| 407 | 2 | public function updateSession($closeSession = FALSE) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Servicio que limpia la sesión |
||
| 424 | */ |
||
| 425 | 1 | public function closeSession() |
|
| 432 | |||
| 433 | } |
||
| 434 |
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: