Complex classes like Authenticator 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 Authenticator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Authenticator |
||
| 12 | { |
||
| 13 | use Config, ErrorBag, Response, Session; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The auth instance. |
||
| 17 | * |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | protected $auth; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The request instance. |
||
| 24 | * |
||
| 25 | * @var |
||
| 26 | */ |
||
| 27 | protected $request; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The current password. |
||
| 31 | * |
||
| 32 | * @var |
||
| 33 | */ |
||
| 34 | protected $password; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Authenticator constructor. |
||
| 38 | * |
||
| 39 | * @param Request $request |
||
| 40 | */ |
||
| 41 | public function __construct(Request $request) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Authenticator boot. |
||
| 48 | * |
||
| 49 | * @param $request |
||
| 50 | * |
||
| 51 | * @return Authenticator |
||
| 52 | */ |
||
| 53 | public function boot($request) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Check if it is already logged in or passable without checking for an OTP. |
||
| 60 | * |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | protected function canPassWithoutCheckingOTP() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get the user Google2FA secret. |
||
| 73 | * |
||
| 74 | * @throws InvalidSecretKey |
||
| 75 | * |
||
| 76 | * @return mixed |
||
| 77 | */ |
||
| 78 | protected function getGoogle2FASecretKey() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the previous OTP. |
||
| 91 | * |
||
| 92 | * @return null|void |
||
| 93 | */ |
||
| 94 | protected function getOldOneTimePassword() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the OTP from user input. |
||
| 105 | * |
||
| 106 | * @throws InvalidOneTimePassword |
||
| 107 | * |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | protected function getOneTimePassword() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the request instance. |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | public function getRequest() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the OTP view. |
||
| 137 | * |
||
| 138 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 139 | */ |
||
| 140 | private function getView() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Keep this OTP session alive. |
||
| 147 | */ |
||
| 148 | protected function keepAlive() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Check if the request input has the OTP. |
||
| 157 | * |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | protected function inputHasOneTimePassword() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get minutes since last activity. |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | protected function minutesSinceLastActivity() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Check if no user is authenticated using OTP. |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | protected function noUserIsAuthenticated() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Check if OTP has expired. |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | protected function passwordExpired() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Put a var value to the current session. |
||
| 207 | * |
||
| 208 | * @param $var |
||
| 209 | * @param $value |
||
| 210 | * |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | protected function sessionPut($var, $value) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Forget a session var. |
||
| 225 | * |
||
| 226 | * @param null $var |
||
| 227 | */ |
||
| 228 | protected function sessionForget($var = null) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set the request property. |
||
| 237 | * |
||
| 238 | * @param mixed $request |
||
| 239 | * |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function setRequest($request) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set current auth as valid. |
||
| 251 | */ |
||
| 252 | protected function storeAuthPassed() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Store the old OTP. |
||
| 261 | * |
||
| 262 | * @param $key |
||
| 263 | * |
||
| 264 | * @return mixed |
||
| 265 | */ |
||
| 266 | protected function storeOldOneTimePassord($key) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Verifies, in the current session, if a 2fa check has already passed. |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | protected function twoFactorAuthStillValid() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the current user. |
||
| 285 | * |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | protected function getUser() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Check if the current use is authenticated via OTP. |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function isAuthenticated() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Check if the module is enabled. |
||
| 308 | * |
||
| 309 | * @return mixed |
||
| 310 | */ |
||
| 311 | protected function isEnabled() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Check if the input OTP is valid. |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | protected function checkOTP() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * OTP logout. |
||
| 336 | */ |
||
| 337 | public function logout() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Update the current auth time. |
||
| 344 | */ |
||
| 345 | protected function updateCurrentAuthTime() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Verify the OTP. |
||
| 352 | * |
||
| 353 | * @return mixed |
||
| 354 | */ |
||
| 355 | protected function verifyGoogle2FA() |
||
| 367 | } |
||
| 368 |