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 | private function getBaseUrl(){ |
||
| 67 | /** |
||
| 68 | * {@inheritDoc} |
||
| 69 | * @see \controllers\ControllerBase::isValid() |
||
| 70 | */ |
||
| 71 | public final function isValid($action) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Action called when the user does not have access rights to a requested resource |
||
| 77 | * @param array|string $urlParts |
||
| 78 | */ |
||
| 79 | public function noAccess($urlParts){ |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Override for modifying the noAccess message |
||
| 92 | * @param FlashMessage $fMessage |
||
| 93 | */ |
||
| 94 | protected function noAccessMessage(FlashMessage $fMessage){ |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Override for modifying attempts message |
||
| 100 | * You can use {_timer} and {_attemptsCount} variables in message content |
||
| 101 | * @param FlashMessage $fMessage |
||
| 102 | * @param int $attempsCount |
||
| 103 | */ |
||
| 104 | protected function attemptsNumberMessage(FlashMessage $fMessage,$attempsCount){ |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Override to implement the complete connection procedure |
||
| 110 | */ |
||
| 111 | public function connect(){ |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Processes the data posted by the login form |
||
| 129 | * Have to return the connected user instance |
||
| 130 | */ |
||
| 131 | abstract protected function _connect(); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param object $connected |
||
| 135 | */ |
||
| 136 | abstract protected function onConnect($connected); |
||
| 137 | |||
| 138 | /** |
||
| 139 | * To override for defining a new action when creditentials are invalid |
||
| 140 | */ |
||
| 141 | protected function onBadCreditentials(){ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Default Action for invalid creditentials |
||
| 147 | */ |
||
| 148 | public function badLogin(){ |
||
| 170 | |||
| 171 | protected function noAttempts(){ |
||
| 192 | |||
| 193 | /** |
||
| 194 | * To override for modifying the bad login message |
||
| 195 | * @param FlashMessage $fMessage |
||
| 196 | */ |
||
| 197 | protected function badLoginMessage(FlashMessage $fMessage){ |
||
| 200 | |||
| 201 | private function authLoadView($viewName,$vars=[]){ |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Logout action |
||
| 215 | * Terminate the session and display a logout message |
||
| 216 | */ |
||
| 217 | public function terminate(){ |
||
| 224 | |||
| 225 | public function _disConnected(){ |
||
| 233 | |||
| 234 | /** |
||
| 235 | * To override for modifying the logout message |
||
| 236 | * @param FlashMessage $fMessage |
||
| 237 | */ |
||
| 238 | protected function terminateMessage(FlashMessage $fMessage){ |
||
| 241 | |||
| 242 | /** |
||
| 243 | * To override for modifying the disconnect message |
||
| 244 | * @param FlashMessage $fMessage |
||
| 245 | */ |
||
| 246 | protected function disconnectedMessage(FlashMessage $fMessage){ |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Action displaying the logged user information |
||
| 252 | * if _displayInfoAsString returns true, use _infoUser var in views to display user info |
||
| 253 | * @return string|null |
||
| 254 | */ |
||
| 255 | public function info(){ |
||
| 258 | |||
| 259 | protected function fMessage(FlashMessage $fMessage,$id=null){ |
||
| 262 | |||
| 263 | public function message($type,$header,$body,$icon="info",$id=null){ |
||
| 266 | |||
| 267 | protected function getOriginalURL(){ |
||
| 270 | |||
| 271 | /** |
||
| 272 | * To override for defining user session key, default : "activeUser" |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function _getUserSessionKey(){ |
||
| 278 | |||
| 279 | /** |
||
| 280 | * To override for getting active user, default : USession::get("activeUser") |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function _getActiveUser(){ |
||
| 286 | |||
| 287 | /** |
||
| 288 | * To override |
||
| 289 | * Returns the maximum number of allowed login attempts |
||
| 290 | */ |
||
| 291 | protected function attemptsNumber(){ |
||
| 294 | |||
| 295 | /** |
||
| 296 | * To override |
||
| 297 | * Returns the time before trying to connect again |
||
| 298 | * Effective only if attemptsNumber return a number |
||
| 299 | * @return number |
||
| 300 | */ |
||
| 301 | protected function attemptsTimeout(){ |
||
| 304 | |||
| 305 | public function _checkConnection(){ |
||
| 309 | |||
| 310 | /** |
||
| 311 | * return boolean true if activeUser is valid |
||
| 312 | */ |
||
| 313 | abstract public function _isValidUser(); |
||
| 314 | |||
| 315 | /** |
||
| 316 | * To override for changing view files |
||
| 317 | * @return AuthFiles |
||
| 318 | */ |
||
| 319 | protected function getFiles ():AuthFiles{ |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Override to define if info is displayed as string |
||
| 325 | * if set to true, use _infoUser var in views to display user info |
||
| 326 | */ |
||
| 327 | public function _displayInfoAsString(){ |
||
| 330 | |||
| 331 | public function _checkConnectionTimeout(){ |
||
| 334 | |||
| 335 | private function _getFiles():AuthFiles{ |
||
| 341 | |||
| 342 | public function _getLoginInputName(){ |
||
| 345 | |||
| 346 | protected function loginLabel(){ |
||
| 349 | |||
| 350 | public function _getPasswordInputName(){ |
||
| 353 | |||
| 354 | protected function passwordLabel(){ |
||
| 357 | |||
| 358 | public function _getBodySelector(){ |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Sets the default noAccess message |
||
| 364 | * Default : "You are not authorized to access the page <b>{url}</b> !" |
||
| 365 | * @param string $content |
||
| 366 | * @param string $title |
||
| 367 | * @param string $type |
||
| 368 | * @param string $icon |
||
| 369 | */ |
||
| 370 | public function _setNoAccessMsg($content,$title=NULL,$type=NULL,$icon=null) { |
||
| 373 | /** |
||
| 374 | * @param string $_loginCaption |
||
| 375 | */ |
||
| 376 | public function _setLoginCaption($_loginCaption) { |
||
| 379 | |||
| 380 | protected function rememberCaption(){ |
||
| 383 | |||
| 384 | protected function getViewVars($viewname){ |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Saves the connected user identifier in a cookie |
||
| 390 | * @param object $connected |
||
| 391 | */ |
||
| 392 | protected function rememberMe($connected){ |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Returns the cookie for auto connection |
||
| 401 | * @return NULL|string |
||
| 402 | */ |
||
| 403 | protected function getCookieUser(){ |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Returns the value from connected user to save it in the cookie for auto connection |
||
| 409 | * @param object $connected |
||
| 410 | */ |
||
| 411 | protected function toCookie($connected){ |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Loads the user from database using the cookie value |
||
| 417 | * @param string $cookie |
||
| 418 | */ |
||
| 419 | protected function fromCookie($cookie){ |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Auto connect the user |
||
| 425 | */ |
||
| 426 | public function _autoConnect() { |
||
| 435 | /** |
||
| 436 | * Deletes the cookie for auto connection and returns to index |
||
| 437 | */ |
||
| 438 | public function forgetConnection(){ |
||
| 442 | } |
||
| 443 |