| 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 |
||
| 46 | public function authenticate(AuthEvent $e) |
||
| 47 | { |
||
| 48 | if ($this->isSatisfied()) { |
||
| 49 | $storage = $this->getStorage()->read(); |
||
| 50 | $e->setIdentity($storage['identity']) |
||
| 51 | ->setCode(AuthenticationResult::SUCCESS) |
||
| 52 | ->setMessages(array('Authentication successful.')); |
||
| 53 | return; |
||
| 54 | } |
||
| 55 | |||
| 56 | $identity = $e->getRequest()->getPost()->get('identity'); |
||
| 57 | $credential = $e->getRequest()->getPost()->get('credential'); |
||
| 58 | $credential = $this->preProcessCredential($credential); |
||
| 59 | $userObject = null; |
||
| 60 | |||
| 61 | // Cycle through the configured identity sources and test each |
||
| 62 | $fields = $this->getOptions()->getAuthIdentityFields(); |
||
| 63 | while (!is_object($userObject) && count($fields) > 0) { |
||
| 64 | $mode = array_shift($fields); |
||
| 65 | switch ($mode) { |
||
| 66 | case 'username': |
||
| 67 | $userObject = $this->getMapper()->findByUsername($identity); |
||
| 68 | break; |
||
| 69 | case 'email': |
||
| 70 | $userObject = $this->getMapper()->findByEmail($identity); |
||
| 71 | break; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | if (!$userObject) { |
||
| 76 | $e->setCode(AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND) |
||
| 77 | ->setMessages(array('A record with the supplied identity could not be found.')); |
||
| 78 | $this->setSatisfied(false); |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($this->getOptions()->getEnableUserState()) { |
||
| 83 | // Don't allow user to login if state is not in allowed list |
||
| 84 | if (!in_array($userObject->getState(), $this->getOptions()->getAllowedLoginStates())) { |
||
| 85 | $e->setCode(AuthenticationResult::FAILURE_UNCATEGORIZED) |
||
| 86 | ->setMessages(array('A record with the supplied identity is not active.')); |
||
| 87 | $this->setSatisfied(false); |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $bcrypt = new Bcrypt(); |
||
| 93 | $bcrypt->setCost($this->getOptions()->getPasswordCost()); |
||
| 94 | if (!$bcrypt->verify($credential, $userObject->getPassword())) { |
||
| 95 | // Password does not match |
||
| 96 | $e->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID) |
||
| 97 | ->setMessages(array('Supplied credential is invalid.')); |
||
| 98 | $this->setSatisfied(false); |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | // regen the id |
||
| 103 | $session = new SessionContainer($this->getStorage()->getNameSpace()); |
||
| 104 | $session->getManager()->regenerateId(); |
||
| 105 | |||
| 106 | // Success! |
||
| 107 | $e->setIdentity($userObject->getId()); |
||
| 108 | // Update user's password hash if the cost parameter has changed |
||
| 109 | $this->updateUserPasswordHash($userObject, $credential, $bcrypt); |
||
| 110 | $this->setSatisfied(true); |
||
| 111 | $storage = $this->getStorage()->read(); |
||
| 112 | $storage['identity'] = $e->getIdentity(); |
||
| 113 | $this->getStorage()->write($storage); |
||
| 114 | $e->setCode(AuthenticationResult::SUCCESS) |
||
| 115 | ->setMessages(array('Authentication successful.')); |
||
| 116 | } |
||
| 117 | |||
| 224 |