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 |
||
| 18 | abstract class AuthController extends ControllerBase{ |
||
| 19 | /** |
||
| 20 | * @var AuthFiles |
||
| 21 | */ |
||
| 22 | protected $authFiles; |
||
| 23 | protected $_controller; |
||
| 24 | protected $_action; |
||
| 25 | protected $_actionParams; |
||
| 26 | protected $_noAccessMsg; |
||
| 27 | protected $_loginCaption; |
||
| 28 | protected $_attemptsSessionKey="_attempts"; |
||
| 29 | |||
| 30 | public function __construct(){ |
||
| 38 | |||
| 39 | public function index(){ |
||
| 52 | |||
| 53 | /** |
||
| 54 | * To override |
||
| 55 | * Return the base route for this Auth controller |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function _getBaseRoute(){ |
||
| 61 | /** |
||
| 62 | * {@inheritDoc} |
||
| 63 | * @see \controllers\ControllerBase::isValid() |
||
| 64 | */ |
||
| 65 | public final function isValid($action) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Action called when the user does not have access rights to a requested resource |
||
| 71 | * @param array|string $urlParts |
||
| 72 | */ |
||
| 73 | public function noAccess($urlParts){ |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Override for modifying the noAccess message |
||
| 86 | * @param FlashMessage $fMessage |
||
| 87 | */ |
||
| 88 | protected function noAccessMessage(FlashMessage $fMessage){ |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Override for modifying attempts message |
||
| 94 | * You can use {_timer} and {_attemptsCount} variables in message content |
||
| 95 | * @param FlashMessage $fMessage |
||
| 96 | * @param int $attempsCount |
||
| 97 | */ |
||
| 98 | protected function attemptsNumberMessage(FlashMessage $fMessage,$attempsCount){ |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Override to implement the complete connection procedure |
||
| 104 | */ |
||
| 105 | public function connect(){ |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Processes the data posted by the login form |
||
| 120 | * Have to return the connected user instance |
||
| 121 | */ |
||
| 122 | abstract protected function _connect(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param object $connected |
||
| 126 | */ |
||
| 127 | abstract protected function onConnect($connected); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * To override for defining a new action when creditentials are invalid |
||
| 131 | */ |
||
| 132 | protected function onBadCreditentials(){ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Default Action for invalid creditentials |
||
| 138 | */ |
||
| 139 | public function badLogin(){ |
||
| 161 | |||
| 162 | protected function noAttempts(){ |
||
| 183 | |||
| 184 | /** |
||
| 185 | * To override for modifying the bad login message |
||
| 186 | * @param FlashMessage $fMessage |
||
| 187 | */ |
||
| 188 | protected function badLoginMessage(FlashMessage $fMessage){ |
||
| 191 | |||
| 192 | private function authLoadView($viewName,$vars=[]){ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Logout action |
||
| 206 | * Terminate the session and display a logout message |
||
| 207 | */ |
||
| 208 | public function terminate(){ |
||
| 215 | |||
| 216 | public function _disConnected(){ |
||
| 224 | |||
| 225 | /** |
||
| 226 | * To override for modifying the logout message |
||
| 227 | * @param FlashMessage $fMessage |
||
| 228 | */ |
||
| 229 | protected function terminateMessage(FlashMessage $fMessage){ |
||
| 232 | |||
| 233 | /** |
||
| 234 | * To override for modifying the disconnect message |
||
| 235 | * @param FlashMessage $fMessage |
||
| 236 | */ |
||
| 237 | protected function disconnectedMessage(FlashMessage $fMessage){ |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Action displaying the logged user information |
||
| 243 | * if _displayInfoAsString returns true, use _infoUser var in views to display user info |
||
| 244 | * @return string|null |
||
| 245 | */ |
||
| 246 | public function info(){ |
||
| 249 | |||
| 250 | protected function fMessage(FlashMessage $fMessage,$id=null){ |
||
| 253 | |||
| 254 | public function message($type,$header,$body,$icon="info",$id=null){ |
||
| 257 | |||
| 258 | protected function getOriginalURL(){ |
||
| 261 | |||
| 262 | /** |
||
| 263 | * To override for defining user session key, default : "activeUser" |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function _getUserSessionKey(){ |
||
| 269 | |||
| 270 | /** |
||
| 271 | * To override for getting active user, default : USession::get("activeUser") |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function _getActiveUser(){ |
||
| 277 | |||
| 278 | /** |
||
| 279 | * To override |
||
| 280 | * Returns the maximum number of allowed login attempts |
||
| 281 | */ |
||
| 282 | protected function attemptsNumber(){ |
||
| 285 | |||
| 286 | /** |
||
| 287 | * To override |
||
| 288 | * Returns the time before trying to connect again |
||
| 289 | * Effective only if attemptsNumber return a number |
||
| 290 | * @return number |
||
| 291 | */ |
||
| 292 | protected function attemptsTimeout(){ |
||
| 295 | |||
| 296 | public function _checkConnection(){ |
||
| 300 | |||
| 301 | /** |
||
| 302 | * return boolean true if activeUser is valid |
||
| 303 | */ |
||
| 304 | abstract public function _isValidUser(); |
||
| 305 | |||
| 306 | /** |
||
| 307 | * To override for changing view files |
||
| 308 | * @return AuthFiles |
||
| 309 | */ |
||
| 310 | protected function getFiles ():AuthFiles{ |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Override to define if info is displayed as string |
||
| 316 | * if set to true, use _infoUser var in views to display user info |
||
| 317 | */ |
||
| 318 | public function _displayInfoAsString(){ |
||
| 321 | |||
| 322 | public function _checkConnectionTimeout(){ |
||
| 325 | |||
| 326 | private function _getFiles():AuthFiles{ |
||
| 332 | |||
| 333 | public function _getLoginInputName(){ |
||
| 336 | |||
| 337 | protected function loginLabel(){ |
||
| 340 | |||
| 341 | public function _getPasswordInputName(){ |
||
| 344 | |||
| 345 | protected function passwordLabel(){ |
||
| 348 | |||
| 349 | public function _getBodySelector(){ |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Sets the default noAccess message |
||
| 355 | * Default : "You are not authorized to access the page <b>{url}</b> !" |
||
| 356 | * @param string $content |
||
| 357 | * @param string $title |
||
| 358 | * @param string $type |
||
| 359 | * @param string $icon |
||
| 360 | */ |
||
| 361 | public function _setNoAccessMsg($content,$title=NULL,$type=NULL,$icon=null) { |
||
| 364 | /** |
||
| 365 | * @param string $_loginCaption |
||
| 366 | */ |
||
| 367 | public function _setLoginCaption($_loginCaption) { |
||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | |||
| 374 | } |
||
| 375 |