| Conditions | 8 |
| Paths | 6 |
| Total Lines | 67 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 53 | protected function userLoader(AccessToken $accessToken): User |
||
| 54 | { |
||
| 55 | $providerParams = $this->authenticationConfigHelper->getParams('generic'); |
||
| 56 | |||
| 57 | /** @var GenericResourceOwner $resourceOwner */ |
||
| 58 | $resourceOwner = $this->client->fetchUserFromToken($accessToken); |
||
| 59 | $resourceOwnerData = $resourceOwner->toArray(); |
||
| 60 | $resourceOwnerId = $resourceOwner->getId(); |
||
| 61 | |||
| 62 | if (empty($resourceOwnerId)) { |
||
| 63 | throw new UnexpectedValueException('Value for the resource owner identifier not found at the configured key'); |
||
| 64 | } |
||
| 65 | |||
| 66 | $fieldType = (int) ExtraField::getExtraFieldTypeFromString('user'); |
||
| 67 | $extraField = $this->extraFieldRepository->findByVariable($fieldType, self::EXTRA_FIELD_OAUTH2_ID); |
||
| 68 | |||
| 69 | $existingUserExtraFieldValue = $this->extraFieldValuesRepository->findByVariableAndValue( |
||
| 70 | $extraField, |
||
| 71 | $resourceOwnerId |
||
| 72 | ); |
||
| 73 | |||
| 74 | if (null === $existingUserExtraFieldValue) { |
||
| 75 | $username = $this->getValueByKey( |
||
| 76 | $resourceOwnerData, |
||
| 77 | $providerParams['resource_owner_username_field'], |
||
| 78 | "oauth2user_$resourceOwnerId" |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** @var User $user */ |
||
| 82 | $user = $this->userRepository->findOneBy(['username' => $username]); |
||
| 83 | |||
| 84 | if (!$user || 'platform' !== $user->getAuthSource()) { |
||
|
|
|||
| 85 | if (!$providerParams['allow_create_new_users']) { |
||
| 86 | throw new AuthenticationException('This user doesn\'t have an account yet and auto-provisioning is not enabled. Please contact this portal administration team to request access.'); |
||
| 87 | } |
||
| 88 | |||
| 89 | // set default values, real values are set in self::updateUserInfo method |
||
| 90 | $user = (new User()) |
||
| 91 | ->setFirstname('OAuth2 User default firstname') |
||
| 92 | ->setLastname('OAuth2 User default firstname') |
||
| 93 | ->setEmail('oauth2user_'.$resourceOwnerId.'@'.(gethostname() or 'localhost')) |
||
| 94 | ->setUsername($username) |
||
| 95 | ->setPlainPassword($username) |
||
| 96 | ->setStatus(STUDENT) |
||
| 97 | ->setCreatorId($this->userRepository->getRootUser()->getId()) |
||
| 98 | ; |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->saveUserInfo($user, $resourceOwnerData, $providerParams); |
||
| 102 | |||
| 103 | $this->extraFieldValuesRepository->updateItemData( |
||
| 104 | $extraField, |
||
| 105 | $user, |
||
| 106 | $resourceOwnerId |
||
| 107 | ); |
||
| 108 | } else { |
||
| 109 | /** @var User $user */ |
||
| 110 | $user = $this->userRepository->find( |
||
| 111 | $existingUserExtraFieldValue->getItemId() |
||
| 112 | ); |
||
| 113 | |||
| 114 | if ($providerParams['allow_update_user_info']) { |
||
| 115 | $this->saveUserInfo($user, $resourceOwnerData, $providerParams); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | return $user; |
||
| 120 | } |
||
| 202 |