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 |
||
| 11 | class Security |
||
| 12 | { |
||
| 13 | // sha1('user') |
||
| 14 | const USER_ID_TOKEN = '12dea96fec20593566ab75692c9949596833adc9'; |
||
| 15 | // sha1('admin') |
||
| 16 | const MANAGER_ID_TOKEN = 'd033e22ae348aeb5660fc2140aec35850c4da997'; |
||
| 17 | // sha1('superadmin') |
||
| 18 | const ADMIN_ID_TOKEN = '889a3a791b3875cfae413574b53da4bb8a90d53e'; |
||
| 19 | // sha1('FLASHES') |
||
| 20 | const FLASH_MESSAGE_TOKEN = '4680c68435db1bfbf17c3fcc4f7b39d2c6122504'; |
||
| 21 | |||
| 22 | use SecureTrait; |
||
| 23 | use SingletonTrait; |
||
| 24 | /** |
||
| 25 | * @var array $user |
||
| 26 | */ |
||
| 27 | private $user = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array $admin |
||
| 31 | */ |
||
| 32 | private $admin = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool $authorized |
||
| 36 | */ |
||
| 37 | private $authorized = FALSE; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool $checked |
||
| 41 | */ |
||
| 42 | private $checked = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array $session |
||
| 46 | */ |
||
| 47 | protected $session; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor por defecto |
||
| 51 | */ |
||
| 52 | 2 | public function init() |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Initializator for SESSION |
||
| 70 | 2 | */ |
|
| 71 | 2 | private function initSession() { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Método estático que devuelve los perfiles de la plataforma |
||
| 83 | * @return array |
||
| 84 | 1 | */ |
|
| 85 | public static function getProfiles() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Method that returns all the available profiles |
||
| 96 | * @return array |
||
| 97 | 1 | */ |
|
| 98 | public function getAdminProfiles() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Método estático que devuelve los perfiles disponibles |
||
| 105 | * @return array |
||
| 106 | 2 | */ |
|
| 107 | public static function getCleanProfiles() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Método estático que devuelve los perfiles disponibles |
||
| 118 | * @return array |
||
| 119 | 1 | */ |
|
| 120 | public function getAdminCleanProfiles() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Método que guarda los administradores |
||
| 127 | * |
||
| 128 | * @param array $user |
||
| 129 | * |
||
| 130 | * @return bool |
||
| 131 | 1 | */ |
|
| 132 | public static function save($user) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Method to save a new admin user |
||
| 150 | * @param array $user |
||
| 151 | * @return bool |
||
| 152 | 1 | */ |
|
| 153 | public function saveUser($user) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Servicio que actualiza los datos del usuario |
||
| 164 | * |
||
| 165 | * @param $user |
||
| 166 | 1 | */ |
|
| 167 | public function updateUser($user) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Método que devuelve los administradores de una plataforma |
||
| 174 | * @return array|null |
||
| 175 | 3 | */ |
|
| 176 | public function getAdmins() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 183 | * |
||
| 184 | * @param null $user |
||
| 185 | * @param null $pass |
||
| 186 | * @param boolean $force |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | * @throws \HttpException |
||
| 190 | 3 | */ |
|
| 191 | public function checkAdmin($user = NULL, $pass = NULL, $force = false) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 224 | * @return array |
||
| 225 | 1 | */ |
|
| 226 | protected function getAdminFromCookie() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Método privado para la generación del hash de la cookie de administración |
||
| 239 | * @return string |
||
| 240 | 1 | */ |
|
| 241 | public function getHash() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Método que devuelve el usuario logado |
||
| 248 | * @return array |
||
| 249 | 2 | */ |
|
| 250 | public function getUser() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Método que devuelve el usuario administrador logado |
||
| 257 | * @return array |
||
| 258 | 2 | */ |
|
| 259 | public function getAdmin() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Método que calcula si se está logado o para acceder a administración |
||
| 266 | * @return bool |
||
| 267 | 2 | */ |
|
| 268 | public function canAccessRestrictedAdmin() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 275 | * |
||
| 276 | * @param string|null $route |
||
| 277 | * |
||
| 278 | * @return string|null |
||
| 279 | */ |
||
| 280 | public function notAuthorized($route) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Servicio que chequea si un usuario es super administrador o no |
||
| 289 | * @return bool |
||
| 290 | 1 | */ |
|
| 291 | public function isSuperAdmin() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Servicio que devuelve un dato de sesión |
||
| 305 | * |
||
| 306 | * @param string $key |
||
| 307 | * |
||
| 308 | * @return mixed |
||
| 309 | 6 | */ |
|
| 310 | public function getSessionKey($key) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Servicio que setea una variable de sesión |
||
| 322 | * |
||
| 323 | * @param string $key |
||
| 324 | * @param mixed $data |
||
| 325 | * |
||
| 326 | * @return Security |
||
| 327 | 5 | */ |
|
| 328 | public function setSessionKey($key, $data = NULL) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 337 | * @return mixed |
||
| 338 | 2 | */ |
|
| 339 | public function getFlashes() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Servicio que limpia los mensajes flash |
||
| 348 | * @return $this |
||
| 349 | 3 | */ |
|
| 350 | public function clearFlashes() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Servicio que inserta un flash en sesión |
||
| 359 | * |
||
| 360 | * @param string $key |
||
| 361 | * @param mixed $data |
||
| 362 | 1 | */ |
|
| 363 | public function setFlash($key, $data = NULL) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Servicio que devuelve un flash de sesión |
||
| 375 | * |
||
| 376 | * @param string $key |
||
| 377 | * |
||
| 378 | * @return mixed |
||
| 379 | 2 | */ |
|
| 380 | public function getFlash($key) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Servicio que actualiza |
||
| 389 | * |
||
| 390 | * @param boolean $closeSession |
||
| 391 | * |
||
| 392 | * @return Security |
||
| 393 | 2 | */ |
|
| 394 | public function updateSession($closeSession = FALSE) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Servicio que limpia la sesión |
||
| 411 | 1 | */ |
|
| 412 | public function closeSession() |
||
| 419 | |||
| 420 | } |
||
| 421 |
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: