Complex classes like Registrar 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 Registrar, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace App\Services; |
||
| 22 | class Registrar implements RegistrarContract |
||
| 23 | { |
||
| 24 | use ValidatesRequests; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Request instance. |
||
| 28 | * |
||
| 29 | * @var Request |
||
| 30 | */ |
||
| 31 | protected $request; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \Cartalyst\Sentinel\Users\UserRepositoryInterface|\Cartalyst\Sentinel\Users\IlluminateUserRepository |
||
| 35 | */ |
||
| 36 | protected $userRepository; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \Cartalyst\Sentinel\Reminders\ReminderRepositoryInterface|\Cartalyst\Sentinel\Reminders\IlluminateReminderRepository |
||
| 40 | */ |
||
| 41 | protected $reminderRepository; |
||
| 42 | |||
| 43 | 18 | public function __construct() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Create a new user instance after a valid registration. |
||
| 52 | * |
||
| 53 | * @return \Cartalyst\Sentinel\Users\UserInterface |
||
| 54 | * @throws \App\Exceptions\Common\ValidationException |
||
| 55 | */ |
||
| 56 | 4 | public function register() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param SocialiteUser $oauthUserData |
||
| 78 | * @param string $provider |
||
| 79 | * |
||
| 80 | * @return \Cartalyst\Sentinel\Users\UserInterface|bool |
||
| 81 | */ |
||
| 82 | public function registerViaOAuth(SocialiteUser $oauthUserData, $provider) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $token |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | * @throws \App\Exceptions\Common\ValidationException |
||
| 118 | * @throws \App\Exceptions\Users\TokenNotValidException |
||
| 119 | */ |
||
| 120 | public function activate($token = null) |
||
| 121 | { |
||
| 122 | $data = !is_null($token) ? ['token' => $token] : $this->request->all(); |
||
| 123 | $validator = app('validator')->make($data, [ |
||
| 124 | 'token' => 'required|string', |
||
| 125 | ]); |
||
| 126 | if ($validator->fails()) { |
||
| 127 | throw new ValidationException($validator); |
||
| 128 | } |
||
| 129 | |||
| 130 | $activation = EloquentActivation::whereCode($data['token'])->first(); |
||
| 131 | if (!$activation) { |
||
| 132 | throw new TokenNotValidException; |
||
| 133 | } |
||
| 134 | $user = $this->userRepository->findById($activation->user_id); |
||
| 135 | |||
| 136 | return app('sentinel.activations')->complete($user, $data['token']); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param integer $id |
||
| 141 | * |
||
| 142 | * @return boolean |
||
| 143 | * @throws \App\Exceptions\Common\ValidationException |
||
| 144 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 145 | */ |
||
| 146 | public function delete($id) |
||
| 147 | { |
||
| 148 | $validator = app('validator')->make($this->request->all(), [ |
||
| 149 | 'password' => 'required|min:' . config('auth.password.min_length'), |
||
| 150 | ]); |
||
| 151 | if ($validator->fails()) { |
||
| 152 | throw new ValidationException($validator); |
||
| 153 | } |
||
| 154 | |||
| 155 | $user = $this->get($id); |
||
| 156 | if (!$this->userRepository->getHasher()->check($this->request->input("password"), $user->password)) { |
||
| 157 | throw new PasswordNotValidException; |
||
| 158 | } |
||
| 159 | |||
| 160 | return (bool)User::destroy($id); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param integer $id |
||
| 165 | * |
||
| 166 | * @return \App\Models\User|\Cartalyst\Sentinel\Users\UserInterface |
||
| 167 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 168 | */ |
||
| 169 | 2 | public function get($id) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param integer $id |
||
| 180 | * |
||
| 181 | * @return boolean |
||
| 182 | * @throws \App\Exceptions\Common\ValidationException |
||
| 183 | */ |
||
| 184 | public function update($id) |
||
| 185 | { |
||
| 186 | $user = $this->get($id); |
||
| 187 | |||
| 188 | $validator = app('validator')->make($this->request->all(), [ |
||
| 189 | 'name' => 'sometimes|required|max:255', |
||
| 190 | 'email' => 'required|email|max:255|unique:users,email,' . $id |
||
| 191 | ]); |
||
| 192 | if ($validator->fails()) { |
||
| 193 | throw new ValidationException($validator); |
||
| 194 | } |
||
| 195 | |||
| 196 | $userBefore = clone $user; |
||
| 197 | |||
| 198 | $this->request->has('name') && $user->name = $this->request->input("name"); |
||
| 199 | $user->email = $this->request->input("email"); |
||
| 200 | |||
| 201 | return $user->save() && event(new Updated($userBefore, $user)); // Fire the event on success only! |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool|\Cartalyst\Sentinel\Users\UserInterface |
||
| 206 | * |
||
| 207 | * @throws \App\Exceptions\Common\ValidationException |
||
| 208 | * @throws \App\Exceptions\Users\LoginNotValidException |
||
| 209 | */ |
||
| 210 | 3 | public function login() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param SocialiteUser $oauthUserData |
||
| 233 | * @param string $provider |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function loginViaOAuth(SocialiteUser $oauthUserData, $provider) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return boolean |
||
| 254 | */ |
||
| 255 | 1 | public function logout() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @return boolean |
||
| 266 | * |
||
| 267 | * @throws \App\Exceptions\Common\ValidationException |
||
| 268 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 269 | */ |
||
| 270 | 2 | public function sendResetPasswordLinkViaEmail() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @return boolean |
||
| 291 | * |
||
| 292 | * @throws \App\Exceptions\Common\ValidationException |
||
| 293 | * @throws \App\Exceptions\Users\TokenNotValidException |
||
| 294 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 295 | */ |
||
| 296 | 6 | public function resetPassword() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param SocialiteUser $oauthUserData |
||
| 326 | * @param string $provider |
||
| 327 | * @param User $ownerAccount |
||
| 328 | * |
||
| 329 | * @return \App\Models\User|bool |
||
| 330 | */ |
||
| 331 | private function linkOAuthAccount(SocialiteUser $oauthUserData, $provider, $ownerAccount) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get the failed login message. |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | 2 | private function getFailedLoginMessage() |
|
| 368 | } |
||
| 369 |