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 |
||
| 10 | class Security |
||
| 11 | { |
||
| 12 | // sha1('user') |
||
| 13 | const USER_ID_TOKEN = '12dea96fec20593566ab75692c9949596833adc9'; |
||
| 14 | // sha1('admin') |
||
| 15 | const MANAGER_ID_TOKEN = 'd033e22ae348aeb5660fc2140aec35850c4da997'; |
||
| 16 | // sha1('superadmin') |
||
| 17 | const ADMIN_ID_TOKEN = '889a3a791b3875cfae413574b53da4bb8a90d53e'; |
||
| 18 | // sha1('FLASHES') |
||
| 19 | const FLASH_MESSAGE_TOKEN = '4680c68435db1bfbf17c3fcc4f7b39d2c6122504'; |
||
| 20 | |||
| 21 | use SingletonTrait; |
||
| 22 | /** |
||
| 23 | * @var array $user |
||
| 24 | */ |
||
| 25 | private $user = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array $admin |
||
| 29 | */ |
||
| 30 | private $admin = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool $authorized |
||
| 34 | */ |
||
| 35 | private $authorized = FALSE; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool $checked |
||
| 39 | */ |
||
| 40 | private $checked = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array $session |
||
| 44 | */ |
||
| 45 | protected $session; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor por defecto |
||
| 49 | */ |
||
| 50 | 3 | public function __construct() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Método estático que devuelve los perfiles de la plataforma |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | 2 | public static function getProfiles() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Method that returns all the available profiles |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | 1 | public function getAdminProfiles() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Método estático que devuelve los perfiles disponibles |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | 1 | public static function getCleanProfiles() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Método estático que devuelve los perfiles disponibles |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | 1 | public function getAdminCleanProfiles() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Método que guarda los administradores |
||
| 113 | * |
||
| 114 | * @param array $user |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | 1 | public static function save($user) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Method to save a new admin user |
||
| 136 | * @param array $user |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | 1 | public function saveUser($user) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Servicio que actualiza los datos del usuario |
||
| 150 | * |
||
| 151 | * @param $user |
||
| 152 | */ |
||
| 153 | 1 | public function updateUser($user) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Método que devuelve los administradores de una plataforma |
||
| 160 | * @return array|null |
||
| 161 | */ |
||
| 162 | 4 | public function getAdmins() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 169 | * |
||
| 170 | * @param null $user |
||
| 171 | * @param null $pass |
||
| 172 | * @param boolean $force |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | * @throws \HttpException |
||
| 176 | */ |
||
| 177 | 3 | public function checkAdmin($user = NULL, $pass = NULL, $force = false) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | 1 | protected function getAdminFromCookie() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Método privado para la generación del hash de la cookie de administración |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 1 | public function getHash() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Método que devuelve el usuario logado |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | 1 | public function getUser() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Método que devuelve el usuario administrador logado |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | 1 | public function getAdmin() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Método que calcula si se está logado o para acceder a administración |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | 2 | public function canAccessRestrictedAdmin() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 261 | * |
||
| 262 | * @param string|null $route |
||
| 263 | * |
||
| 264 | * @return string|null |
||
| 265 | */ |
||
| 266 | public function notAuthorized($route) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Servicio que chequea si un usuario es super administrador o no |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 1 | public function isSuperAdmin() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Servicio que devuelve un dato de sesión |
||
| 292 | * |
||
| 293 | * @param string $key |
||
| 294 | * |
||
| 295 | * @return mixed |
||
| 296 | */ |
||
| 297 | 5 | public function getSessionKey($key) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Servicio que setea una variable de sesión |
||
| 309 | * |
||
| 310 | * @param string $key |
||
| 311 | * @param mixed $data |
||
| 312 | * |
||
| 313 | * @return Security |
||
| 314 | */ |
||
| 315 | 5 | public function setSessionKey($key, $data = NULL) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 324 | * @return mixed |
||
| 325 | */ |
||
| 326 | 1 | public function getFlashes() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Servicio que limpia los mensajes flash |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | 3 | public function clearFlashes() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Servicio que inserta un flash en sesión |
||
| 346 | * |
||
| 347 | * @param string $key |
||
| 348 | * @param mixed $data |
||
| 349 | */ |
||
| 350 | 1 | public function setFlash($key, $data = NULL) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Servicio que devuelve un flash de sesión |
||
| 362 | * |
||
| 363 | * @param string $key |
||
| 364 | * |
||
| 365 | * @return mixed |
||
| 366 | */ |
||
| 367 | 1 | public function getFlash($key) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Servicio que actualiza |
||
| 376 | * |
||
| 377 | * @param boolean $closeSession |
||
| 378 | * |
||
| 379 | * @return Security |
||
| 380 | */ |
||
| 381 | 2 | public function updateSession($closeSession = FALSE) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Servicio que limpia la sesión |
||
| 398 | */ |
||
| 399 | 1 | public function closeSession() |
|
| 405 | |||
| 406 | } |
||
| 407 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.