Complex classes like AuthController 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 AuthController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class AuthController extends ControllerBase{ |
||
| 20 | /** |
||
| 21 | * @var AuthFiles |
||
| 22 | */ |
||
| 23 | protected $authFiles; |
||
| 24 | protected $_controller; |
||
| 25 | protected $_action; |
||
| 26 | protected $_actionParams; |
||
| 27 | protected $_noAccessMsg; |
||
| 28 | protected $_loginCaption; |
||
| 29 | protected $_attemptsSessionKey="_attempts"; |
||
| 30 | |||
| 31 | public function __construct(){ |
||
| 39 | |||
| 40 | public function index(){ |
||
| 54 | |||
| 55 | /** |
||
| 56 | * To override |
||
| 57 | * Return the base route for this Auth controller |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function _getBaseRoute(){ |
||
| 63 | /** |
||
| 64 | * {@inheritDoc} |
||
| 65 | * @see \controllers\ControllerBase::isValid() |
||
| 66 | */ |
||
| 67 | public final function isValid($action) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Action called when the user does not have access rights to a requested resource |
||
| 73 | * @param array|string $urlParts |
||
| 74 | */ |
||
| 75 | public function noAccess($urlParts){ |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Override for modifying the noAccess message |
||
| 88 | * @param FlashMessage $fMessage |
||
| 89 | */ |
||
| 90 | protected function noAccessMessage(FlashMessage $fMessage){ |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Override for modifying attempts message |
||
| 96 | * You can use {_timer} and {_attemptsCount} variables in message content |
||
| 97 | * @param FlashMessage $fMessage |
||
| 98 | * @param int $attempsCount |
||
| 99 | */ |
||
| 100 | protected function attemptsNumberMessage(FlashMessage $fMessage,$attempsCount){ |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Override to implement the complete connection procedure |
||
| 106 | */ |
||
| 107 | public function connect(){ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Processes the data posted by the login form |
||
| 125 | * Have to return the connected user instance |
||
| 126 | */ |
||
| 127 | abstract protected function _connect(); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param object $connected |
||
| 131 | */ |
||
| 132 | abstract protected function onConnect($connected); |
||
| 133 | |||
| 134 | /** |
||
| 135 | * To override for defining a new action when creditentials are invalid |
||
| 136 | */ |
||
| 137 | protected function onBadCreditentials(){ |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Default Action for invalid creditentials |
||
| 143 | */ |
||
| 144 | public function badLogin(){ |
||
| 166 | |||
| 167 | protected function noAttempts(){ |
||
| 188 | |||
| 189 | /** |
||
| 190 | * To override for modifying the bad login message |
||
| 191 | * @param FlashMessage $fMessage |
||
| 192 | */ |
||
| 193 | protected function badLoginMessage(FlashMessage $fMessage){ |
||
| 196 | |||
| 197 | private function authLoadView($viewName,$vars=[]){ |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Logout action |
||
| 211 | * Terminate the session and display a logout message |
||
| 212 | */ |
||
| 213 | public function terminate(){ |
||
| 220 | |||
| 221 | public function _disConnected(){ |
||
| 229 | |||
| 230 | /** |
||
| 231 | * To override for modifying the logout message |
||
| 232 | * @param FlashMessage $fMessage |
||
| 233 | */ |
||
| 234 | protected function terminateMessage(FlashMessage $fMessage){ |
||
| 237 | |||
| 238 | /** |
||
| 239 | * To override for modifying the disconnect message |
||
| 240 | * @param FlashMessage $fMessage |
||
| 241 | */ |
||
| 242 | protected function disconnectedMessage(FlashMessage $fMessage){ |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Action displaying the logged user information |
||
| 248 | * if _displayInfoAsString returns true, use _infoUser var in views to display user info |
||
| 249 | * @return string|null |
||
| 250 | */ |
||
| 251 | public function info(){ |
||
| 254 | |||
| 255 | protected function fMessage(FlashMessage $fMessage,$id=null){ |
||
| 258 | |||
| 259 | public function message($type,$header,$body,$icon="info",$id=null){ |
||
| 262 | |||
| 263 | protected function getOriginalURL(){ |
||
| 266 | |||
| 267 | /** |
||
| 268 | * To override for defining user session key, default : "activeUser" |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function _getUserSessionKey(){ |
||
| 274 | |||
| 275 | /** |
||
| 276 | * To override for getting active user, default : USession::get("activeUser") |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function _getActiveUser(){ |
||
| 282 | |||
| 283 | /** |
||
| 284 | * To override |
||
| 285 | * Returns the maximum number of allowed login attempts |
||
| 286 | */ |
||
| 287 | protected function attemptsNumber(){ |
||
| 290 | |||
| 291 | /** |
||
| 292 | * To override |
||
| 293 | * Returns the time before trying to connect again |
||
| 294 | * Effective only if attemptsNumber return a number |
||
| 295 | * @return number |
||
| 296 | */ |
||
| 297 | protected function attemptsTimeout(){ |
||
| 300 | |||
| 301 | public function _checkConnection(){ |
||
| 305 | |||
| 306 | /** |
||
| 307 | * return boolean true if activeUser is valid |
||
| 308 | */ |
||
| 309 | abstract public function _isValidUser(); |
||
| 310 | |||
| 311 | /** |
||
| 312 | * To override for changing view files |
||
| 313 | * @return AuthFiles |
||
| 314 | */ |
||
| 315 | protected function getFiles ():AuthFiles{ |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Override to define if info is displayed as string |
||
| 321 | * if set to true, use _infoUser var in views to display user info |
||
| 322 | */ |
||
| 323 | public function _displayInfoAsString(){ |
||
| 326 | |||
| 327 | public function _checkConnectionTimeout(){ |
||
| 330 | |||
| 331 | private function _getFiles():AuthFiles{ |
||
| 337 | |||
| 338 | public function _getLoginInputName(){ |
||
| 341 | |||
| 342 | protected function loginLabel(){ |
||
| 345 | |||
| 346 | public function _getPasswordInputName(){ |
||
| 349 | |||
| 350 | protected function passwordLabel(){ |
||
| 353 | |||
| 354 | public function _getBodySelector(){ |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Sets the default noAccess message |
||
| 360 | * Default : "You are not authorized to access the page <b>{url}</b> !" |
||
| 361 | * @param string $content |
||
| 362 | * @param string $title |
||
| 363 | * @param string $type |
||
| 364 | * @param string $icon |
||
| 365 | */ |
||
| 366 | public function _setNoAccessMsg($content,$title=NULL,$type=NULL,$icon=null) { |
||
| 369 | /** |
||
| 370 | * @param string $_loginCaption |
||
| 371 | */ |
||
| 372 | public function _setLoginCaption($_loginCaption) { |
||
| 375 | |||
| 376 | protected function rememberCaption(){ |
||
| 379 | |||
| 380 | protected function getViewVars($viewname){ |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Saves the connected user identifier in a cookie |
||
| 386 | * @param object $connected |
||
| 387 | */ |
||
| 388 | protected function rememberMe($connected){ |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Returns the cookie for auto connection |
||
| 397 | * @return NULL|string |
||
| 398 | */ |
||
| 399 | protected function getCookieUser(){ |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns the value from connected user to save in the cookie for auto connection |
||
| 405 | * @param object $connected |
||
| 406 | */ |
||
| 407 | protected function getPhpCookieTransformer($connected){ |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Loads the user from database using the cookie value |
||
| 413 | * @param string $cookie |
||
| 414 | */ |
||
| 415 | protected function getDbUserFromCookie($cookie){ |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Auto connect the user |
||
| 421 | */ |
||
| 422 | public function _autoConnect() { |
||
| 431 | /** |
||
| 432 | * Deletes the cookie for auto connection and returns to index |
||
| 433 | */ |
||
| 434 | public function forgetConnection(){ |
||
| 438 | } |
||
| 439 |