| 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 |
||
| 56 | protected function userLoader(AccessToken $accessToken): User |
||
| 57 | { |
||
| 58 | $providerParams = $this->authenticationConfigHelper->getParams('generic'); |
||
| 59 | |||
| 60 | /** @var GenericResourceOwner $resourceOwner */ |
||
| 61 | $resourceOwner = $this->client->fetchUserFromToken($accessToken); |
||
| 62 | $resourceOwnerData = $resourceOwner->toArray(); |
||
| 63 | $resourceOwnerId = $resourceOwner->getId(); |
||
| 64 | |||
| 65 | if (empty($resourceOwnerId)) { |
||
| 66 | throw new UnexpectedValueException('Value for the resource owner identifier not found at the configured key'); |
||
| 67 | } |
||
| 68 | |||
| 69 | $fieldType = (int) ExtraField::getExtraFieldTypeFromString('user'); |
||
| 70 | $extraField = $this->extraFieldRepository->findByVariable($fieldType, self::EXTRA_FIELD_OAUTH2_ID); |
||
| 71 | |||
| 72 | $existingUserExtraFieldValue = $this->extraFieldValuesRepository->findByVariableAndValue( |
||
| 73 | $extraField, |
||
| 74 | $resourceOwnerId |
||
| 75 | ); |
||
| 76 | |||
| 77 | if (null === $existingUserExtraFieldValue) { |
||
| 78 | $username = $this->getValueByKey( |
||
| 79 | $resourceOwnerData, |
||
| 80 | $providerParams['resource_owner_username_field'], |
||
| 81 | "oauth2user_$resourceOwnerId" |
||
| 82 | ); |
||
| 83 | |||
| 84 | /** @var User $user */ |
||
| 85 | $user = $this->userRepository->findOneBy(['username' => $username]); |
||
| 86 | |||
| 87 | if (!$user || 'platform' !== $user->getAuthSource()) { |
||
|
|
|||
| 88 | if (!$providerParams['allow_create_new_users']) { |
||
| 89 | 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.'); |
||
| 90 | } |
||
| 91 | |||
| 92 | // set default values, real values are set in self::updateUserInfo method |
||
| 93 | $user = (new User()) |
||
| 94 | ->setFirstname('OAuth2 User default firstname') |
||
| 95 | ->setLastname('OAuth2 User default firstname') |
||
| 96 | ->setEmail('oauth2user_'.$resourceOwnerId.'@'.(gethostname() ?: 'localhost')) |
||
| 97 | ->setUsername($username) |
||
| 98 | ->setPlainPassword($username) |
||
| 99 | ->setStatus(STUDENT) |
||
| 100 | ->setCreatorId($this->userRepository->getRootUser()->getId()) |
||
| 101 | ; |
||
| 102 | } |
||
| 103 | |||
| 104 | $this->saveUserInfo($user, $resourceOwnerData, $providerParams); |
||
| 105 | |||
| 106 | $this->extraFieldValuesRepository->updateItemData( |
||
| 107 | $extraField, |
||
| 108 | $user, |
||
| 109 | $resourceOwnerId |
||
| 110 | ); |
||
| 111 | } else { |
||
| 112 | /** @var User $user */ |
||
| 113 | $user = $this->userRepository->find( |
||
| 114 | $existingUserExtraFieldValue->getItemId() |
||
| 115 | ); |
||
| 116 | |||
| 117 | if ($providerParams['allow_update_user_info']) { |
||
| 118 | $this->saveUserInfo($user, $resourceOwnerData, $providerParams); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return $user; |
||
| 123 | } |
||
| 207 |