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 SingletonTrait; |
||
| 25 | /** |
||
| 26 | * @var array $user |
||
| 27 | */ |
||
| 28 | private $user = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array $admin |
||
| 32 | */ |
||
| 33 | private $admin = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool $authorized |
||
| 37 | */ |
||
| 38 | private $authorized = FALSE; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var bool $checked |
||
| 42 | */ |
||
| 43 | private $checked = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array $session |
||
| 47 | */ |
||
| 48 | protected $session; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor por defecto |
||
| 52 | 3 | */ |
|
| 53 | public function __construct() |
||
| 54 | 3 | { |
|
| 55 | 1 | if (PHP_SESSION_NONE === session_status()) { |
|
| 56 | session_start(); |
||
| 57 | 2 | } |
|
| 58 | 2 | $this->session = (is_null($_SESSION)) ? array() : $_SESSION; |
|
| 59 | 2 | if (NULL === $this->getSessionKey('__FLASH_CLEAR__')) { |
|
| 60 | 2 | $this->clearFlashes(); |
|
| 61 | 2 | $this->setSessionKey('__FLASH_CLEAR__', microtime(TRUE)); |
|
| 62 | 2 | } |
|
| 63 | 2 | $this->user = (array_key_exists(self::USER_ID_TOKEN, $this->session)) ? unserialize($this->session[self::USER_ID_TOKEN]) : NULL; |
|
|
|
|||
| 64 | 2 | $this->admin = (array_key_exists(self::ADMIN_ID_TOKEN, $this->session)) ? unserialize($this->session[self::ADMIN_ID_TOKEN]) : NULL; |
|
| 65 | 2 | if (null === $this->admin) { |
|
| 66 | 2 | $this->checkAdmin(); |
|
| 67 | 2 | } |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Método estático que devuelve los perfiles de la plataforma |
||
| 72 | * @return array |
||
| 73 | 1 | */ |
|
| 74 | public static function getProfiles() |
||
| 75 | { |
||
| 76 | 1 | return array( |
|
| 77 | 1 | self::ADMIN_ID_TOKEN => _('Administrador'), |
|
| 78 | 1 | self::MANAGER_ID_TOKEN => _('Gestor'), |
|
| 79 | 1 | self::USER_ID_TOKEN => _('Usuario'), |
|
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Method that returns all the available profiles |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function getAdminProfiles() |
||
| 88 | { |
||
| 89 | return static::getProfiles(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Método estático que devuelve los perfiles disponibles |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public static function getCleanProfiles() |
||
| 97 | { |
||
| 98 | return array( |
||
| 99 | '__SUPER_ADMIN__' => self::ADMIN_ID_TOKEN, |
||
| 100 | '__ADMIN__' => self::MANAGER_ID_TOKEN, |
||
| 101 | '__USER__' => self::USER_ID_TOKEN, |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Método estático que devuelve los perfiles disponibles |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | public function getAdminCleanProfiles() |
||
| 110 | { |
||
| 111 | return static::getCleanProfiles(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Método que guarda los administradores |
||
| 116 | * |
||
| 117 | * @param array $user |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public static function save($user) |
||
| 122 | { |
||
| 123 | $saved = true; |
||
| 124 | try { |
||
| 125 | $admins = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', Cache::JSONGZ, true) ?: []; |
||
| 126 | $admins[$user['username']]['hash'] = sha1($user['username'] . $user['password']); |
||
| 127 | $admins[$user['username']]['profile'] = $user['profile']; |
||
| 128 | |||
| 129 | Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', $admins, Cache::JSONGZ, true); |
||
| 130 | } catch(\Exception $e) { |
||
| 131 | Logger::log($e->getMessage(), LOG_ERR); |
||
| 132 | $saved = false; |
||
| 133 | } |
||
| 134 | return $saved; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Method to save a new admin user |
||
| 139 | * @param array $user |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function saveUser($user) |
||
| 143 | { |
||
| 144 | $saved = false; |
||
| 145 | if (!empty($user)) { |
||
| 146 | $saved = static::save($user); |
||
| 147 | } |
||
| 148 | return $saved; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Servicio que actualiza los datos del usuario |
||
| 153 | * |
||
| 154 | * @param $user |
||
| 155 | */ |
||
| 156 | public function updateUser($user) |
||
| 157 | { |
||
| 158 | $this->user = $user; |
||
| 159 | } |
||
| 160 | 1 | ||
| 161 | /** |
||
| 162 | 1 | * Método que devuelve los administradores de una plataforma |
|
| 163 | 1 | * @return array|null |
|
| 164 | */ |
||
| 165 | public function getAdmins() |
||
| 166 | { |
||
| 167 | 1 | return Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', Cache::JSONGZ, true); |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Método que devuelve si un usuario tiene privilegios para acceder a la zona de administración |
||
| 172 | * |
||
| 173 | * @param null $user |
||
| 174 | * @param null $pass |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | * @throws \HttpException |
||
| 178 | */ |
||
| 179 | 1 | public function checkAdmin($user = NULL, $pass = NULL) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | protected function getAdminFromCookie() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Método privado para la generación del hash de la cookie de administración |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getHash() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Método que devuelve el usuario logado |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getUser() |
||
| 239 | { |
||
| 240 | return $this->user; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Método que devuelve el usuario administrador logado |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public function getAdmin() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Método que calcula si se está logado o para acceder a administración |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | 1 | public function canAccessRestrictedAdmin() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
| 263 | * |
||
| 264 | * @param string|null $route |
||
| 265 | * |
||
| 266 | * @return string|null |
||
| 267 | */ |
||
| 268 | public function notAuthorized($route) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Servicio que chequea si un usuario es super administrador o no |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public function isSuperAdmin() |
||
| 280 | { |
||
| 281 | $users = $this->getAdmins(); |
||
| 282 | $logged = $this->getAdminFromCookie(); |
||
| 283 | if ($users[$logged[0]]) { |
||
| 284 | $security = $users[$logged[0]]['profile']; |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Servicio que devuelve un dato de sesión |
||
| 294 | * |
||
| 295 | * @param string $key |
||
| 296 | * |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | public function getSessionKey($key) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Servicio que setea una variable de sesión |
||
| 311 | * |
||
| 312 | * @param string $key |
||
| 313 | * @param mixed $data |
||
| 314 | * |
||
| 315 | * @return Security |
||
| 316 | */ |
||
| 317 | public function setSessionKey($key, $data = NULL) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Servicio que devuelve los mensajes flash de sesiones |
||
| 326 | * @return mixed |
||
| 327 | */ |
||
| 328 | public function getFlashes() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Servicio que limpia los mensajes flash |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | public function clearFlashes() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Servicio que inserta un flash en sesión |
||
| 348 | * |
||
| 349 | * @param string $key |
||
| 350 | * @param mixed $data |
||
| 351 | */ |
||
| 352 | public function setFlash($key, $data = NULL) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Servicio que devuelve un flash de sesión |
||
| 364 | * |
||
| 365 | * @param string $key |
||
| 366 | * |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | public function getFlash($key) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Servicio que actualiza |
||
| 378 | * |
||
| 379 | * @param boolean $closeSession |
||
| 380 | * |
||
| 381 | * @return Security |
||
| 382 | */ |
||
| 383 | public function updateSession($closeSession = FALSE) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Servicio que limpia la sesión |
||
| 400 | */ |
||
| 401 | public function closeSession() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Generate a authorized token |
||
| 410 | * @param string $secret |
||
| 411 | * @param string $module |
||
| 412 | * @deprecated |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public static function generateToken($secret, $module = 'PSFS') |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Checks if auth token is correct |
||
| 422 | * @param string $token |
||
| 423 | * @param string $secret |
||
| 424 | * @param string $module |
||
| 425 | * @deprecated |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public static function checkToken($token, $secret, $module = 'PSFS') |
||
| 432 | |||
| 433 | } |
||
| 434 |
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.