| Conditions | 7 |
| Paths | 7 |
| Total Lines | 68 |
| Code Lines | 49 |
| 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 |
||
| 57 | public function loadUserByOAuthUserResponse(UserResponseInterface $response) |
||
| 58 | { |
||
| 59 | $oAuth = $this->oAuthService->getRepository()->findOneBy([ |
||
| 60 | 'type' => $response->getResourceOwner()->getName(), |
||
| 61 | 'identifier' => $response->getUsername() |
||
| 62 | ]); |
||
| 63 | |||
| 64 | if ($oAuth) { |
||
| 65 | $oAuth->setToken($response->getAccessToken()); |
||
| 66 | $user = $oAuth->getUser(); |
||
| 67 | } else { |
||
| 68 | $email = $response->getEmail(); |
||
| 69 | |||
| 70 | if (null === $email) { |
||
| 71 | throw new AccountNotLinkedException('Email is not defined.'); |
||
| 72 | } |
||
| 73 | |||
| 74 | $user = $this->userManager->findUserByUsernameOrEmail($response->getEmail()); |
||
| 75 | |||
| 76 | if ($user) { |
||
|
|
|||
| 77 | throw new AccountNotLinkedException('Username is not available.'); |
||
| 78 | } |
||
| 79 | |||
| 80 | $owner = substr($response->getResourceOwner()->getName(), 11); |
||
| 81 | $data = [ |
||
| 82 | '%email%' => $response->getEmail(), |
||
| 83 | '%firstName%' => $response->getFirstName(), |
||
| 84 | '%lastName%' => $response->getLastName() |
||
| 85 | ]; |
||
| 86 | |||
| 87 | switch ($owner) { |
||
| 88 | case 'github': |
||
| 89 | break; |
||
| 90 | |||
| 91 | case 'google': |
||
| 92 | break; |
||
| 93 | |||
| 94 | case 'twitter': |
||
| 95 | break; |
||
| 96 | } |
||
| 97 | |||
| 98 | $registration = $this->registrationService->createInstance(); |
||
| 99 | $registration |
||
| 100 | ->setOwner($this->configService->get('app.registration.individual.owner.type')) |
||
| 101 | ->setOwnerUuid($this->configService->get('app.registration.individual.owner.uuid')) |
||
| 102 | ->setIdentity(Identity::INDIVIDUAL) |
||
| 103 | ->setUsername($email) |
||
| 104 | ->setPassword(sha1(uniqid().microtime())) |
||
| 105 | ->setData(json_decode(strtr($this->configService->get('app.registration.individual.data.'.$owner), $data), true)); |
||
| 106 | $manager = $this->registrationService->getManager(); |
||
| 107 | $manager->persist($registration); |
||
| 108 | $manager->flush(); |
||
| 109 | $user = $registration->getUser(); |
||
| 110 | $oAuth = new OAuth; |
||
| 111 | $oAuth |
||
| 112 | ->setUser($user) |
||
| 113 | ->setType($response->getResourceOwner()->getName()) |
||
| 114 | ->setIdentifier($response->getUsername()) |
||
| 115 | ->setToken($response->getAccessToken()) |
||
| 116 | ->setOwner($registration->getOwner()) |
||
| 117 | ->setOwnerUuid($registration->getOwnerUuid()); |
||
| 118 | } |
||
| 119 | |||
| 120 | $manager = $this->oAuthService->getManager(); |
||
| 121 | $manager->persist($oAuth); |
||
| 122 | $manager->flush(); |
||
| 123 | |||
| 124 | return $user; |
||
| 125 | } |
||
| 151 |