Complex classes like Management 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 Management, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | trait Management { |
||
33 | /** |
||
34 | * User id after registration |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $reg_id = 0; |
||
38 | /** |
||
39 | * Search keyword in login, username and email |
||
40 | * |
||
41 | * @param string $search_phrase |
||
42 | * |
||
43 | * @return false|int[] |
||
44 | */ |
||
45 | 2 | public function search_users ($search_phrase) { |
|
46 | 2 | $search_phrase = trim($search_phrase, "%\n"); |
|
47 | 2 | $found_users = $this->db()->qfas( |
|
48 | 2 | "SELECT `id` |
|
49 | FROM `[prefix]users` |
||
50 | WHERE |
||
51 | ( |
||
52 | `login` LIKE '%1\$s' OR |
||
53 | `username` LIKE '%1\$s' OR |
||
54 | `email` LIKE '%1\$s' |
||
55 | ) AND |
||
56 | `status` != '%2\$s'", |
||
57 | 2 | $search_phrase, |
|
58 | 2 | User::STATUS_NOT_ACTIVATED |
|
59 | ); |
||
60 | 2 | if (!$found_users) { |
|
61 | 2 | return false; |
|
62 | } |
||
63 | 2 | return _int($found_users); |
|
64 | } |
||
65 | /** |
||
66 | * User registration |
||
67 | * |
||
68 | * @param string $email |
||
69 | * @param bool $confirmation If <b>true</b> - default system option is used, if <b>false</b> - registration will be finished without necessity of |
||
70 | * confirmation, independently from default system option (is used for manual registration). |
||
71 | * @param bool $auto_sign_in If <b>false</b> - no auto sign in, if <b>true</b> - according to system configuration |
||
72 | * |
||
73 | * @return array|false|string <b>exists</b> - if user with such email is already registered<br> |
||
74 | * <b>error</b> - if error occurred<br> |
||
75 | * <b>false</b> - if email is incorrect<br> |
||
76 | * <b>[<br> |
||
77 | * 'reg_key' => *,</b> //Registration confirmation key, or <b>true</b> if confirmation is not |
||
78 | * required<br> |
||
79 | * <b>'password' => *,</b> //Automatically generated password (empty if confirmation is needed and |
||
80 | * will be set during registration confirmation)<br> |
||
81 | * <b>'id' => *</b> //Id of registered user in DB<br> |
||
82 | * <b>]</b> |
||
83 | */ |
||
84 | 28 | public function registration ($email, $confirmation = true, $auto_sign_in = true) { |
|
85 | 28 | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
|
86 | 6 | return false; |
|
87 | } |
||
88 | 28 | $email = mb_strtolower($email); |
|
89 | 28 | $email_hash = hash('sha224', $email); |
|
90 | 28 | if ($this->get_id($email_hash)) { |
|
91 | 4 | return 'exists'; |
|
92 | } |
||
93 | 28 | list($login, $login_hash) = $this->registration_get_login_login_hash($email); |
|
94 | 28 | $this->delete_unconfirmed_users(); |
|
95 | 28 | if (!Event::instance()->fire( |
|
96 | 28 | 'System/User/registration/before', |
|
97 | [ |
||
98 | 28 | 'email' => $email |
|
99 | ] |
||
100 | ) |
||
101 | ) { |
||
102 | 2 | return false; |
|
103 | } |
||
104 | 28 | $Config = Config::instance(); |
|
105 | 28 | $confirmation = $confirmation && $Config->core['require_registration_confirmation']; |
|
106 | 28 | $reg_key = md5(random_bytes(1000)); |
|
107 | 28 | if ($this->db_prime()->q( |
|
108 | 28 | "INSERT INTO `[prefix]users` ( |
|
109 | `login`, |
||
110 | `login_hash`, |
||
111 | `email`, |
||
112 | `email_hash`, |
||
113 | `reg_date`, |
||
114 | `reg_ip`, |
||
115 | `reg_key`, |
||
116 | `status` |
||
117 | ) VALUES ( |
||
118 | '%s', |
||
119 | '%s', |
||
120 | '%s', |
||
121 | '%s', |
||
122 | '%s', |
||
123 | '%s', |
||
124 | '%s', |
||
125 | '%s' |
||
126 | )", |
||
127 | 28 | $login, |
|
128 | 28 | $login_hash, |
|
129 | 28 | $email, |
|
130 | 28 | $email_hash, |
|
131 | 28 | time(), |
|
132 | 28 | ip2hex(Request::instance()->ip), |
|
133 | 28 | $reg_key, |
|
134 | 28 | User::STATUS_NOT_ACTIVATED |
|
135 | ) |
||
136 | ) { |
||
137 | 28 | $this->reg_id = (int)$this->db_prime()->id(); |
|
138 | 28 | $password = ''; |
|
139 | 28 | if (!$confirmation) { |
|
140 | 26 | $password = $this->activate_registered_user($Config, $this->reg_id, $auto_sign_in && $Config->core['auto_sign_in_after_registration']); |
|
141 | } |
||
142 | 28 | if (!Event::instance()->fire( |
|
143 | 28 | 'System/User/registration/after', |
|
144 | [ |
||
145 | 28 | 'id' => $this->reg_id |
|
146 | ] |
||
147 | ) |
||
148 | ) { |
||
149 | 2 | $this->registration_cancel(); |
|
150 | 2 | return false; |
|
151 | } |
||
152 | 28 | if (!$confirmation) { |
|
153 | 26 | $this->set_groups([User::USER_GROUP_ID], $this->reg_id); |
|
154 | } |
||
155 | 28 | unset($this->cache->$login_hash); |
|
156 | return [ |
||
157 | 28 | 'reg_key' => !$confirmation ? true : $reg_key, |
|
158 | 28 | 'password' => $password, |
|
159 | 28 | 'id' => $this->reg_id |
|
160 | ]; |
||
161 | } else { |
||
162 | return 'error'; |
||
163 | } |
||
164 | } |
||
165 | /** |
||
166 | * @param string $email |
||
167 | * |
||
168 | * @return string[] |
||
169 | */ |
||
170 | 28 | protected function registration_get_login_login_hash ($email) { |
|
171 | 28 | $login = strstr($email, '@', true); |
|
172 | 28 | $login_hash = hash('sha224', $login); |
|
173 | 28 | if ($this->registration_login_already_occupied($login, $login_hash)) { |
|
174 | 2 | $login = $email; |
|
175 | 2 | $login_hash = hash('sha224', $login); |
|
176 | } |
||
177 | 28 | return [$login, $login_hash]; |
|
178 | } |
||
179 | /** |
||
180 | * @param string $login |
||
181 | * @param string $login_hash |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | 28 | protected function registration_login_already_occupied ($login, $login_hash) { |
|
193 | /** |
||
194 | * @param Config $Config |
||
195 | * @param int $id |
||
196 | * @param bool $create_session |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | 28 | protected function activate_registered_user ($Config, $id, $create_session) { |
|
213 | /** |
||
214 | * Confirmation of registration process |
||
215 | * |
||
216 | * @param string $reg_key |
||
217 | * |
||
218 | * @return array|false ['id' => <i>id</i>, 'email' => <i>email</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
219 | */ |
||
220 | 2 | public function registration_confirmation ($reg_key) { |
|
221 | 2 | if (!is_md5($reg_key)) { |
|
222 | 2 | return false; |
|
223 | } |
||
224 | 2 | if (!Event::instance()->fire( |
|
225 | 2 | 'System/User/registration/confirmation/before', |
|
226 | [ |
||
227 | 2 | 'reg_key' => $reg_key |
|
228 | ] |
||
229 | ) |
||
230 | ) { |
||
231 | 2 | $this->registration_cancel(); |
|
232 | 2 | return false; |
|
233 | } |
||
234 | 2 | $this->delete_unconfirmed_users(); |
|
235 | 2 | $data = $this->db_prime()->qf( |
|
236 | 2 | "SELECT |
|
237 | `id`, |
||
238 | `login_hash`, |
||
239 | `email` |
||
240 | FROM `[prefix]users` |
||
241 | WHERE |
||
242 | `reg_key` = '%s' AND |
||
243 | `status` = '%s' |
||
244 | LIMIT 1", |
||
245 | 2 | $reg_key, |
|
246 | 2 | User::STATUS_NOT_ACTIVATED |
|
247 | ); |
||
248 | 2 | if (!$data) { |
|
249 | 2 | return false; |
|
250 | } |
||
251 | 2 | $this->reg_id = (int)$data['id']; |
|
252 | 2 | $Config = Config::instance(); |
|
253 | 2 | $password = $this->activate_registered_user($Config, $this->reg_id, true); |
|
254 | 2 | if (!Event::instance()->fire( |
|
255 | 2 | 'System/User/registration/confirmation/after', |
|
256 | [ |
||
257 | 2 | 'id' => $this->reg_id |
|
258 | ] |
||
259 | ) |
||
260 | ) { |
||
261 | 2 | $this->registration_cancel(); |
|
262 | 2 | return false; |
|
263 | } |
||
264 | 2 | unset($this->cache->{$data['login_hash']}); |
|
265 | return [ |
||
266 | 2 | 'id' => $this->reg_id, |
|
267 | 2 | 'email' => $data['email'], |
|
268 | 2 | 'password' => $password |
|
269 | ]; |
||
270 | } |
||
271 | /** |
||
272 | * Canceling of bad/failed registration |
||
273 | */ |
||
274 | 6 | public function registration_cancel () { |
|
281 | /** |
||
282 | * Checks for unconfirmed registrations and deletes expired |
||
283 | */ |
||
284 | 28 | protected function delete_unconfirmed_users () { |
|
298 | /** |
||
299 | * Proper password setting without any need to deal with low-level implementation |
||
300 | * |
||
301 | * @param string $new_password |
||
302 | * @param false|int $user |
||
303 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | 28 | public function set_password ($new_password, $user = false, $already_prepared = false) { |
|
320 | /** |
||
321 | * Proper password validation without any need to deal with low-level implementation |
||
322 | * |
||
323 | * @param string $password |
||
324 | * @param false|int $user |
||
325 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | 8 | public function validate_password ($password, $user = false, $already_prepared = false) { |
|
350 | /** |
||
351 | * Restoring of password |
||
352 | * |
||
353 | * @param int $user |
||
354 | * |
||
355 | * @return false|string Key for confirmation or <b>false</b> on failure |
||
356 | */ |
||
357 | 4 | public function restore_password ($user) { |
|
369 | /** |
||
370 | * Confirmation of password restoring process |
||
371 | * |
||
372 | * @param string $key |
||
373 | * |
||
374 | * @return array|false ['id' => <i>id</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
375 | */ |
||
376 | 2 | public function restore_password_confirmation ($key) { |
|
377 | 2 | if (!is_md5($key)) { |
|
378 | 2 | return false; |
|
379 | } |
||
380 | 2 | $id = (int)$this->db_prime()->qfs( |
|
381 | 2 | "SELECT `id` |
|
382 | FROM `[prefix]users` |
||
383 | WHERE |
||
384 | `reg_key` = '%s' AND |
||
385 | `status` = '%s' |
||
386 | LIMIT 1", |
||
387 | 2 | $key, |
|
388 | 2 | User::STATUS_ACTIVE |
|
389 | ); |
||
390 | 2 | if (!$id) { |
|
391 | 2 | return false; |
|
392 | } |
||
393 | 2 | $restore_until = $this->get_data('restore_until', $id); |
|
394 | 2 | $this->del_data('restore_until', $id); |
|
395 | 2 | if ($restore_until < time()) { |
|
396 | 2 | return false; |
|
397 | } |
||
398 | 2 | $Config = Config::instance(); |
|
399 | 2 | $password = password_generate($Config->core['password_min_length'], $Config->core['password_min_strength']); |
|
400 | 2 | $this->set_password($password, $id); |
|
401 | 2 | Session::instance()->add($id); |
|
402 | return [ |
||
403 | 2 | 'id' => $id, |
|
404 | 2 | 'password' => $password |
|
405 | ]; |
||
406 | } |
||
407 | /** |
||
408 | * Delete specified user or array of users |
||
409 | * |
||
410 | * @param int|int[] $user User id or array of users ids |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | 16 | public function del_user ($user) { |
|
461 | } |
||
462 |