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 | 2 | private function initSession() { |
|
80 | |||
81 | /** |
||
82 | * @return array |
||
83 | */ |
||
84 | 2 | public static function getProfiles() |
|
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | 1 | public function getAdminProfiles() |
|
100 | |||
101 | /** |
||
102 | * @return array |
||
|
|||
103 | */ |
||
104 | 2 | public static function getCleanProfiles() |
|
112 | |||
113 | /** |
||
114 | * @return array |
||
115 | */ |
||
116 | 1 | public function getAdminCleanProfiles() |
|
120 | |||
121 | /** |
||
122 | * @param mixed $user |
||
123 | * @return bool |
||
124 | * @throws exception\GeneratorException |
||
125 | * @throws ConfigException |
||
126 | */ |
||
127 | 1 | public static function save($user) |
|
137 | |||
138 | /** |
||
139 | * @param mixed $user |
||
140 | * @return bool |
||
141 | * @throws exception\GeneratorException |
||
142 | */ |
||
143 | 1 | public function saveUser($user) |
|
151 | |||
152 | /** |
||
153 | * @param mixed $user |
||
154 | */ |
||
155 | 1 | public function updateUser($user) |
|
159 | |||
160 | /** |
||
161 | * @param $alias |
||
162 | * @param $profile |
||
163 | */ |
||
164 | 1 | public function updateAdmin($alias, $profile) { |
|
171 | |||
172 | /** |
||
173 | * @return array|null |
||
174 | */ |
||
175 | 4 | public function getAdmins() |
|
179 | |||
180 | /** |
||
181 | * @param string $user |
||
182 | * @param string $pass |
||
183 | * @param boolean $force |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | 3 | public function checkAdmin($user = NULL, $pass = NULL, $force = false) |
|
225 | |||
226 | /** |
||
227 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
228 | * @return array |
||
229 | */ |
||
230 | 1 | protected function getAdminFromCookie() |
|
240 | |||
241 | /** |
||
242 | * Método privado para la generación del hash de la cookie de administración |
||
243 | * @return string |
||
244 | */ |
||
245 | 1 | public function getHash() |
|
249 | |||
250 | /** |
||
251 | * Método que devuelve el usuario logado |
||
252 | * @return array |
||
253 | */ |
||
254 | 2 | public function getUser() |
|
258 | |||
259 | /** |
||
260 | * Método que devuelve el usuario administrador logado |
||
261 | * @return array |
||
262 | */ |
||
263 | 2 | public function getAdmin() |
|
267 | |||
268 | /** |
||
269 | * Método que calcula si se está logado o para acceder a administración |
||
270 | * @return bool |
||
271 | */ |
||
272 | 3 | public function canAccessRestrictedAdmin() |
|
276 | |||
277 | /** |
||
278 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
279 | * |
||
280 | * @param string|null $route |
||
281 | * |
||
282 | * @return string|null |
||
283 | */ |
||
284 | public function notAuthorized($route) |
||
290 | |||
291 | /** |
||
292 | * @return bool |
||
293 | */ |
||
294 | 1 | public function isSuperAdmin() |
|
295 | { |
||
296 | 1 | $users = $this->getAdmins(); |
|
297 | 1 | $logged = $this->getAdmin(); |
|
298 | 1 | if (is_array($logged) |
|
299 | 1 | && array_key_exists('alias', $logged) |
|
300 | 1 | && array_key_exists($logged['alias'], $users)) { |
|
301 | 1 | $security = $users[$logged['alias']]['profile']; |
|
302 | 1 | return self::ADMIN_ID_TOKEN === $security; |
|
303 | } |
||
304 | |||
305 | return FALSE; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * |
||
310 | * @param string $key |
||
311 | * |
||
312 | * @return mixed |
||
313 | */ |
||
314 | 7 | public function getSessionKey($key) |
|
323 | |||
324 | /** |
||
325 | * |
||
326 | * @param string $key |
||
327 | * @param mixed $data |
||
328 | * |
||
329 | * @return Security |
||
330 | */ |
||
331 | 6 | public function setSessionKey($key, $data = NULL) |
|
337 | |||
338 | /** |
||
339 | * @return mixed |
||
340 | */ |
||
341 | 2 | public function getFlashes() |
|
347 | |||
348 | /** |
||
349 | * @return $this |
||
350 | */ |
||
351 | 3 | public function clearFlashes() |
|
357 | |||
358 | /** |
||
359 | * |
||
360 | * @param string $key |
||
361 | * @param mixed $data |
||
362 | */ |
||
363 | 1 | public function setFlash($key, $data = NULL) |
|
372 | |||
373 | /** |
||
374 | * |
||
375 | * @param string $key |
||
376 | * |
||
377 | * @return mixed |
||
378 | */ |
||
379 | 2 | public function getFlash($key) |
|
385 | |||
386 | /** |
||
387 | * |
||
388 | * @param boolean $closeSession |
||
389 | * |
||
390 | * @return Security |
||
391 | */ |
||
392 | 2 | public function updateSession($closeSession = FALSE) |
|
406 | |||
407 | 1 | public function closeSession() |
|
414 | |||
415 | } |
||
416 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.