Complex classes like Kohana_Auth_Jam 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 Kohana_Auth_Jam, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') OR die('No direct access allowed.'); |
||
| 10 | abstract class Kohana_Auth_Jam extends Auth { |
||
| 11 | |||
| 12 | protected $_services = array(); |
||
| 13 | |||
| 14 | public static function clear_cache() |
||
| 18 | |||
| 19 | 15 | public static function access($action, $access = array()) |
|
| 30 | |||
| 31 | 37 | public function __construct($config = array()) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Get all the available services, or only one service if provided a name |
||
| 44 | * @param string $name the name of the service, e.g. 'facebook' |
||
| 45 | * @return array|Auth_Service |
||
| 46 | */ |
||
| 47 | 13 | public function services($name = NULL) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Checks if a session is active. |
||
| 54 | * |
||
| 55 | * @param mixed $role Role name string, role Jam object, or array with role names |
||
| 56 | * @return boolean |
||
| 57 | */ |
||
| 58 | 12 | public function logged_in($role = NULL) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Getter |
||
| 90 | * The session instance for the configured session_type |
||
| 91 | * @return Session |
||
| 92 | */ |
||
| 93 | 37 | public function session() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Logs a user in. |
||
| 104 | * |
||
| 105 | * @param string $username username |
||
| 106 | * @param string $password password |
||
| 107 | * @param boolean $remember enable autologin |
||
| 108 | * @return boolean |
||
| 109 | */ |
||
| 110 | 6 | protected function _login($user, $password, $remember) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Create autologin token |
||
| 140 | * @param Model_User $user |
||
| 141 | * @return Model_User_Token |
||
| 142 | */ |
||
| 143 | 1 | public function remember($user) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Forces a user to be logged in, without specifying a password. |
||
| 155 | * |
||
| 156 | * @param mixed $user username string, or user Jam object |
||
| 157 | * @param boolean $mark_session_as_forced mark the session as forced |
||
| 158 | * @param boolean $remember force to remeber the user |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | 2 | public function force_login($user, $mark_session_as_forced = FALSE, $remember = FALSE) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * login using a spesific token |
||
| 182 | * @param string $token token hash |
||
| 183 | * @return Model_User|NULL |
||
| 184 | */ |
||
| 185 | 4 | public function login_with_token($token) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Attempt to login the user using specific service |
||
| 217 | * @param string $name the name of the service |
||
| 218 | * @return Model_User|NULL |
||
| 219 | */ |
||
| 220 | public function login_with_service($name) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Attempt to login the user using specific service |
||
| 227 | * @param string $name the name of the service |
||
| 228 | * @param boolean $remember create autologin token |
||
| 229 | * @return Model_User|NULL |
||
| 230 | */ |
||
| 231 | 1 | public function complete_login_with_service($name, $remember = FALSE) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Logs a user in, based on the authautologin cookie. |
||
| 250 | * |
||
| 251 | * @return mixed |
||
| 252 | */ |
||
| 253 | 12 | public function auto_login() |
|
| 254 | { |
||
| 255 | 12 | if ($token = $this->_autologin_cookie()) |
|
| 256 | { |
||
| 257 | if ($user = $this->login_with_token($token)) |
||
| 258 | { |
||
| 259 | return $user; |
||
| 260 | } |
||
| 261 | else |
||
| 262 | { |
||
| 263 | $this->_autologin_cookie(FALSE); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | 12 | if (Request::current() AND $token = Arr::get(Request::current()->query(), '_token')) |
|
| 268 | { |
||
| 269 | if ($user = $this->login_with_token($token)) |
||
| 270 | return $user; |
||
| 271 | } |
||
| 272 | |||
| 273 | 12 | foreach ($this->services() as $service) |
|
| 274 | { |
||
| 275 | 12 | if ($service->auto_login_enabled() AND $user = $service->get_user()) |
|
| 276 | { |
||
| 277 | // $this->remember($user); |
||
| 278 | $this->complete_login($user); |
||
| 279 | return $user; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | 12 | return FALSE; |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Gets the currently logged in user from the session (with auto_login check). |
||
| 288 | * Returns FALSE if no user is currently logged in. |
||
| 289 | * |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | 14 | public function get_user($default = NULL) |
|
| 306 | |||
| 307 | protected function _load_user($user) |
||
| 314 | |||
| 315 | protected function _load_token($token) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Getter / Setter of the autologin cookie |
||
| 325 | * Extend this method in your tests so you can remove dependance on cookies there |
||
| 326 | * |
||
| 327 | * @param string $token |
||
| 328 | * @param integer $expires days lifetime |
||
| 329 | * @return mixed |
||
| 330 | */ |
||
| 331 | protected function _autologin_cookie($token = NULL, $expires = NULL) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Log a user out and remove any autologin cookies, and goes to each service to log out the user there |
||
| 350 | * |
||
| 351 | * @param boolean $destroy completely destroy the session |
||
| 352 | * @param boolean $logout_all remove all tokens for user |
||
| 353 | * @return boolean |
||
| 354 | */ |
||
| 355 | 9 | public function logout($destroy = FALSE, $logout_all = FALSE) |
|
| 356 | { |
||
| 357 | // Set by force_login() |
||
| 358 | 9 | $this->session()->delete('auth_forced'); |
|
| 359 | |||
| 360 | 9 | if ($token = $this->_autologin_cookie()) |
|
| 361 | { |
||
| 362 | // Delete the autologin cookie to prevent re-login |
||
| 363 | 2 | $this->_autologin_cookie(FALSE); |
|
| 364 | |||
| 365 | // Clear the autologin token from the database |
||
| 366 | 2 | $token = $this->_load_token($token); |
|
| 367 | |||
| 368 | 2 | if ($token AND $token->loaded() AND $token->user) |
|
| 369 | { |
||
| 370 | 2 | if ($logout_all) |
|
| 371 | { |
||
| 372 | $token->user->user_tokens->clear(); |
||
| 373 | } |
||
| 374 | 2 | $token->delete(); |
|
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | 9 | foreach ($this->services() as $service) |
|
| 379 | { |
||
| 380 | 9 | if ($user = $service->get_user()) |
|
| 381 | { |
||
| 382 | $service->logout(); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | 9 | return parent::logout($destroy); |
|
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get the stored password for a username. |
||
| 391 | * |
||
| 392 | * @param mixed $user username string, or user Jam object |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public function password($user) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Complete the login for a user by incrementing the logins and setting |
||
| 402 | * session data: user_id, username, roles. |
||
| 403 | * |
||
| 404 | * @param object $user Jam object |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | 8 | protected function complete_login($user) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Compare password with original (hashed). Works for current (logged in) user |
||
| 424 | * |
||
| 425 | * @param string $password |
||
| 426 | * @return boolean |
||
| 427 | */ |
||
| 428 | 1 | public function check_password($password) |
|
| 437 | |||
| 438 | } // End Auth Jam |
||
| 439 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.