| Conditions | 10 |
| Paths | 25 |
| Total Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 51 | public function authenticate(AdapterChainEvent $e) |
||
| 52 | { |
||
| 53 | if ($this->isSatisfied()) { |
||
| 54 | $storage = $this->getStorage()->read(); |
||
| 55 | $e->setIdentity($storage['identity']) |
||
| 56 | ->setCode(AuthenticationResult::SUCCESS) |
||
| 57 | ->setMessages(array('Authentication successful.')); |
||
| 58 | return; |
||
| 59 | } |
||
| 60 | |||
| 61 | $identity = $e->getRequest()->getPost()->get('identity'); |
||
| 62 | $credential = $e->getRequest()->getPost()->get('credential'); |
||
| 63 | $credential = $this->preProcessCredential($credential); |
||
| 64 | /** |
||
| 65 | * |
||
| 66 | * |
||
| 67 | * @var UserInterface|null $userObject |
||
| 68 | */ |
||
| 69 | $userObject = null; |
||
| 70 | |||
| 71 | // Cycle through the configured identity sources and test each |
||
| 72 | $fields = $this->getOptions()->getAuthIdentityFields(); |
||
| 73 | while (!is_object($userObject) && count($fields) > 0) { |
||
| 74 | $mode = array_shift($fields); |
||
| 75 | |||
| 76 | switch ($mode) { |
||
| 77 | case 'username': |
||
| 78 | $userObject = $this->getMapper()->findByUsername($identity); |
||
| 79 | break; |
||
| 80 | case 'email': |
||
| 81 | $userObject = $this->getMapper()->findByEmail($identity); |
||
| 82 | |||
| 83 | break; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!$userObject) { |
||
| 88 | $e->setCode(AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND) |
||
| 89 | ->setMessages(array('A record with the supplied identity could not be found.')); |
||
| 90 | $this->setSatisfied(false); |
||
| 91 | return false; |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($this->getOptions()->getEnableUserState()) { |
||
| 95 | // Don't allow user to login if state is not in allowed list |
||
| 96 | if (!in_array($userObject->getState(), $this->getOptions()->getAllowedLoginStates())) { |
||
| 97 | $e->setCode(AuthenticationResult::FAILURE_UNCATEGORIZED) |
||
| 98 | ->setMessages(array('A record with the supplied identity is not active.')); |
||
| 99 | $this->setSatisfied(false); |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | $bcrypt = new Bcrypt(); |
||
| 105 | $bcrypt->setCost($this->getOptions()->getPasswordCost()); |
||
| 106 | |||
| 107 | if (!$bcrypt->verify($credential, $userObject->getPassword())) { |
||
| 108 | // Password does not match |
||
| 109 | $e->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID) |
||
| 110 | ->setMessages(array('Supplied credential is invalid.')); |
||
| 111 | $this->setSatisfied(false); |
||
| 112 | return false; |
||
| 113 | } |
||
| 114 | |||
| 115 | // regen the id |
||
| 116 | $session = new SessionContainer($this->getStorage()->getNameSpace()); |
||
| 117 | $session->getManager()->regenerateId(); |
||
| 118 | |||
| 119 | // Success! |
||
| 120 | $e->setIdentity($userObject->getId()); |
||
| 121 | // Update user's password hash if the cost parameter has changed |
||
| 122 | $this->updateUserPasswordHash($userObject, $credential, $bcrypt); |
||
| 123 | $this->setSatisfied(true); |
||
| 124 | $storage = $this->getStorage()->read(); |
||
| 125 | $storage['identity'] = $e->getIdentity(); |
||
| 126 | $this->getStorage()->write($storage); |
||
| 127 | $e->setCode(AuthenticationResult::SUCCESS) |
||
| 128 | ->setMessages(array('Authentication successful.')); |
||
| 129 | } |
||
| 130 | |||
| 241 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.