Complex classes like profile 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 profile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait profile { |
||
| 21 | 2 | public static function profile_get () { |
|
| 39 | /** |
||
| 40 | * @param \cs\Request $Request |
||
| 41 | * |
||
| 42 | * @throws ExitException |
||
| 43 | */ |
||
| 44 | 2 | public static function profile_patch ($Request) { |
|
| 74 | /** |
||
| 75 | * @param \cs\Request $Request |
||
| 76 | * |
||
| 77 | * @throws ExitException |
||
| 78 | */ |
||
| 79 | 2 | public static function profile_change_password ($Request) { |
|
| 80 | 2 | $data = $Request->data('current_password', 'new_password'); |
|
| 81 | 2 | if (!$data) { |
|
| 82 | 2 | throw new ExitException(400); |
|
| 83 | } |
||
| 84 | 2 | $User = User::instance(); |
|
| 85 | 2 | if (!$User->user()) { |
|
| 86 | 2 | throw new ExitException(403); |
|
| 87 | } |
||
| 88 | 2 | $L = Language::prefix('system_profile_'); |
|
| 89 | 2 | if (!$data['new_password']) { |
|
| 90 | 2 | throw new ExitException($L->please_type_new_password, 400); |
|
| 91 | } |
||
| 92 | 2 | if (!$User->validate_password($data['current_password'], $User->id, true)) { |
|
| 93 | 2 | throw new ExitException($L->wrong_current_password, 400); |
|
| 94 | } |
||
| 95 | 2 | $id = $User->id; |
|
| 96 | 2 | if ($User->set_password($data['new_password'], $id, true)) { |
|
| 97 | 2 | Session::instance()->add($id); |
|
| 98 | } else { |
||
| 99 | 2 | throw new ExitException($L->change_password_server_error, 500); |
|
| 100 | } |
||
| 101 | 2 | } |
|
| 102 | /** |
||
| 103 | * @param \cs\Request $Request |
||
| 104 | * @param \cs\Response $Response |
||
| 105 | * |
||
| 106 | * @throws ExitException |
||
| 107 | */ |
||
| 108 | 2 | public static function profile_registration ($Request, $Response) { |
|
| 109 | 2 | $Config = Config::instance(); |
|
| 110 | 2 | $L = Language::prefix('system_profile_registration_'); |
|
| 111 | 2 | $User = User::instance(); |
|
| 112 | 2 | if (!$User->guest()) { |
|
| 113 | 2 | throw new ExitException(403); |
|
| 114 | } |
||
| 115 | 2 | if (!$Config->core['allow_user_registration']) { |
|
| 116 | 2 | throw new ExitException($L->prohibited, 403); |
|
| 117 | } |
||
| 118 | 2 | $email = $Request->data('email'); |
|
| 119 | 2 | $email = mb_strtolower($email); |
|
| 120 | 2 | $result = static::try_to_register($User, $L, $email); |
|
| 121 | 2 | $confirm = $result['reg_key'] !== true; |
|
| 122 | 2 | static::fill_optional_profile_data($Request, $User, $result['id']); |
|
| 123 | 2 | $title = $L->success_mail($Config->core['site_name']); |
|
| 124 | 2 | $body = $L->success_mail( |
|
| 125 | 2 | $User->username($result['id']), |
|
| 126 | 2 | $Config->core['site_name'], |
|
| 127 | 2 | $Config->core_url().'/profile/settings', |
|
| 128 | 2 | $User->get('login', $result['id']) |
|
| 129 | ); |
||
| 130 | 2 | if ($confirm) { |
|
| 131 | 2 | $title = $L->need_confirmation_mail($Config->core['site_name']); |
|
| 132 | 2 | $body = $L->need_confirmation_mail_body( |
|
| 133 | 2 | $User->username($result['id']), |
|
| 134 | 2 | $Config->core['site_name'], |
|
| 135 | 2 | $Config->core_url()."/profile/registration_confirmation/$result[reg_key]", |
|
| 136 | 2 | $L->time($Config->core['registration_confirmation_time'], 'd') |
|
| 137 | ); |
||
| 138 | 2 | } elseif (!$Request->data('password') && $result['password']) { |
|
| 139 | 2 | $body = $L->success_mail_with_password_body( |
|
| 140 | 2 | $User->username($result['id']), |
|
| 141 | 2 | $Config->core['site_name'], |
|
| 142 | 2 | $Config->core_url().'/profile/settings', |
|
| 143 | 2 | $User->get('login', $result['id']), |
|
| 144 | 2 | $result['password'] |
|
| 145 | ); |
||
| 146 | } |
||
| 147 | 2 | if (!Mail::instance()->send_to($email, $title, $body)) { |
|
| 148 | 2 | $User->registration_cancel(); |
|
| 149 | 2 | throw new ExitException($L->mail_sending_error, 500); |
|
| 150 | } |
||
| 151 | 2 | $Response->code = $confirm ? 202 : 201; |
|
| 152 | 2 | } |
|
| 153 | /** |
||
| 154 | * @param User $User |
||
| 155 | * @param Language\Prefix $L |
||
| 156 | * @param string $email |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | * |
||
| 160 | * @throws ExitException |
||
| 161 | */ |
||
| 162 | 2 | protected static function try_to_register ($User, $L, $email) { |
|
| 163 | 2 | $result = $User->registration($email); |
|
| 164 | 2 | if ($result === false) { |
|
| 165 | 2 | throw new ExitException($L->please_type_correct_email, 400); |
|
| 166 | } |
||
| 167 | 2 | if ($result == 'error') { |
|
| 168 | throw new ExitException($L->server_error, 500); |
||
| 169 | } |
||
| 170 | 2 | if ($result == 'exists') { |
|
| 171 | 2 | throw new ExitException($L->error_exists, 400); |
|
| 172 | } |
||
| 173 | 2 | return $result; |
|
| 174 | } |
||
| 175 | /** |
||
| 176 | * @param \cs\Request $Request |
||
| 177 | * @param User $User |
||
| 178 | * @param int $user_id |
||
| 179 | */ |
||
| 180 | 2 | protected static function fill_optional_profile_data ($Request, $User, $user_id) { |
|
| 181 | 2 | if ($Request->data('username')) { |
|
| 182 | 2 | $User->set('username', $Request->data['username'], $user_id); |
|
| 183 | } |
||
| 184 | // Actually `sha512(sha512(password) + public_key)` instead of plain password |
||
| 185 | 2 | if ($Request->data('password')) { |
|
| 186 | 2 | $User->set_password($Request->data['password'], $user_id, true); |
|
| 187 | } |
||
| 188 | 2 | if ($Request->data('language')) { |
|
| 189 | 2 | $User->set('language', $Request->data['language'], $user_id); |
|
| 190 | } |
||
| 191 | 2 | if ($Request->data('timezone')) { |
|
| 192 | 2 | $User->set('timezone', $Request->data['timezone'], $user_id); |
|
| 193 | } |
||
| 194 | 2 | if ($Request->data('avatar')) { |
|
| 195 | 2 | $User->set('avatar', $Request->data['avatar'], $user_id); |
|
| 196 | } |
||
| 197 | 2 | } |
|
| 198 | /** |
||
| 199 | * @param \cs\Request $Request |
||
| 200 | * |
||
| 201 | * @throws ExitException |
||
| 202 | */ |
||
| 203 | 2 | public static function profile_restore_password ($Request) { |
|
| 204 | 2 | $User = User::instance(); |
|
| 205 | 2 | if (!$User->guest()) { |
|
| 206 | 2 | throw new ExitException(403); |
|
| 207 | } |
||
| 208 | 2 | $L = Language::prefix('system_profile_restore_password_'); |
|
| 209 | 2 | $email = $Request->data('email'); |
|
| 210 | 2 | if (!$email) { |
|
| 211 | 2 | throw new ExitException($L->please_type_your_email, 400); |
|
| 212 | } |
||
| 213 | 2 | $id = $User->get_id(mb_strtolower($email)); |
|
| 214 | 2 | if (!$id) { |
|
| 215 | 2 | throw new ExitException($L->user_with_such_login_email_not_found, 400); |
|
| 216 | } |
||
| 217 | 2 | $key = $User->restore_password($id); |
|
| 218 | 2 | $Config = Config::instance(); |
|
| 219 | if ( |
||
| 220 | 2 | !$key || |
|
| 221 | 2 | !Mail::instance()->send_to( |
|
| 222 | 2 | $User->get('email', $id), |
|
| 223 | 2 | $L->confirmation_mail($Config->core['site_name']), |
|
| 224 | 2 | $L->confirmation_mail_body( |
|
| 225 | 2 | $User->username($id), |
|
| 226 | 2 | $Config->core['site_name'], |
|
| 227 | 2 | $Config->core_url()."/profile/restore_password_confirmation/$key", |
|
| 228 | 2 | $L->time($Config->core['registration_confirmation_time'], 'd') |
|
| 229 | ) |
||
| 230 | ) |
||
| 231 | ) { |
||
| 232 | 2 | throw new ExitException($L->server_error, 500); |
|
| 233 | } |
||
| 234 | 2 | } |
|
| 235 | /** |
||
| 236 | * @param \cs\Request $Request |
||
| 237 | * |
||
| 238 | * @throws ExitException |
||
| 239 | */ |
||
| 240 | 2 | public static function profile_sign_in ($Request) { |
|
| 285 | /** |
||
| 286 | * @param \cs\Request $Request |
||
| 287 | * @param \cs\Response $Response |
||
| 288 | * |
||
| 289 | * @throws ExitException |
||
| 290 | */ |
||
| 291 | public static function profile_sign_out ( |
||
| 315 | } |
||
| 316 |