| Conditions | 10 |
| Paths | 25 |
| Total Lines | 71 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 44 | public function authenticate(AuthEvent $e) |
||
| 45 | { |
||
| 46 | if ($this->isSatisfied()) { |
||
| 47 | $storage = $this->getStorage()->read(); |
||
| 48 | $e->setIdentity($storage['identity']) |
||
| 49 | ->setCode(AuthenticationResult::SUCCESS) |
||
| 50 | ->setMessages(array('Authentication successful.')); |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | $identity = $e->getRequest()->getPost()->get('identity'); |
||
| 55 | $credential = $e->getRequest()->getPost()->get('credential'); |
||
| 56 | $credential = $this->preProcessCredential($credential); |
||
| 57 | $userObject = null; |
||
| 58 | |||
| 59 | // Cycle through the configured identity sources and test each |
||
| 60 | $fields = $this->getOptions()->getAuthIdentityFields(); |
||
| 61 | while (!is_object($userObject) && count($fields) > 0) { |
||
| 62 | $mode = array_shift($fields); |
||
| 63 | switch ($mode) { |
||
| 64 | case 'username': |
||
| 65 | $userObject = $this->getMapper()->findByUsername($identity); |
||
| 66 | break; |
||
| 67 | case 'email': |
||
| 68 | $userObject = $this->getMapper()->findByEmail($identity); |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | if (!$userObject) { |
||
| 74 | $e->setCode(AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND) |
||
| 75 | ->setMessages(array('A record with the supplied identity could not be found.')); |
||
| 76 | $this->setSatisfied(false); |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($this->getOptions()->getEnableUserState()) { |
||
| 81 | // Don't allow user to login if state is not in allowed list |
||
| 82 | if (!in_array($userObject->getState(), $this->getOptions()->getAllowedLoginStates())) { |
||
| 83 | $e->setCode(AuthenticationResult::FAILURE_UNCATEGORIZED) |
||
| 84 | ->setMessages(array('A record with the supplied identity is not active.')); |
||
| 85 | $this->setSatisfied(false); |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $bcrypt = new Bcrypt(); |
||
| 91 | $bcrypt->setCost($this->getOptions()->getPasswordCost()); |
||
| 92 | if (!$bcrypt->verify($credential, $userObject->getPassword())) { |
||
| 93 | // Password does not match |
||
| 94 | $e->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID) |
||
| 95 | ->setMessages(array('Supplied credential is invalid.')); |
||
| 96 | $this->setSatisfied(false); |
||
| 97 | return false; |
||
| 98 | } |
||
| 99 | |||
| 100 | // regen the id |
||
| 101 | $session = new SessionContainer($this->getStorage()->getNameSpace()); |
||
| 102 | $session->getManager()->regenerateId(); |
||
| 103 | |||
| 104 | // Success! |
||
| 105 | $e->setIdentity($userObject->getId()); |
||
| 106 | // Update user's password hash if the cost parameter has changed |
||
| 107 | $this->updateUserPasswordHash($userObject, $credential, $bcrypt); |
||
| 108 | $this->setSatisfied(true); |
||
| 109 | $storage = $this->getStorage()->read(); |
||
| 110 | $storage['identity'] = $e->getIdentity(); |
||
| 111 | $this->getStorage()->write($storage); |
||
| 112 | $e->setCode(AuthenticationResult::SUCCESS) |
||
| 113 | ->setMessages(array('Authentication successful.')); |
||
| 114 | } |
||
| 115 | |||
| 222 |