Conditions | 9 |
Paths | 34 |
Total Lines | 66 |
Code Lines | 50 |
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 |
||
38 | public function loadUserByOAuthUserResponse(UserResponseInterface $response) |
||
39 | { |
||
40 | $socialID = $response->getUsername(); |
||
41 | /** @var User $user */ |
||
42 | $user = $socialID ? $this->userManager->findUserBy([$this->getProperty($response) => $socialID]) : null; |
||
43 | $email = $response->getEmail(); |
||
44 | if (!$user) { |
||
|
|||
45 | $user = $this->userManager->findUserByEmail($email); |
||
46 | |||
47 | if (!$user || !$user instanceof UserInterface) { |
||
48 | try { |
||
49 | $user = $this->userManager->createUser(); |
||
50 | $user->setName($response->getFirstName()); |
||
51 | $user->setSurname($response->getLastName()); |
||
52 | $user->setEmail($email); |
||
53 | $user->setPlainPassword(md5(uniqid())); |
||
54 | $user->setEnabled(true); |
||
55 | $errors = $this->container->get('validator')->validate($user); |
||
56 | if ($errors->count() > 0) { |
||
57 | throw new \Exception('need_data'); |
||
58 | } |
||
59 | $this->userManager->updateUser($user); |
||
60 | } catch (\Exception $e) { |
||
61 | $needUserData = new NeedUserDataException('needUserData'); |
||
62 | $responseArr = []; |
||
63 | $responseArr['first_name'] = $response->getFirstName(); |
||
64 | $responseArr['last_name'] = $response->getLastName(); |
||
65 | $responseArr['email'] = $email; |
||
66 | |||
67 | $responseArr = array_merge( |
||
68 | $responseArr, |
||
69 | ['socialID' => $socialID], |
||
70 | ['service' => $response->getResourceOwner()->getName()] |
||
71 | ); |
||
72 | $needUserData->setResponse($responseArr); |
||
73 | $this->container->get('session')->getFlashBag()->set('fos_user_success', 'registration.flash.user_need_data'); |
||
74 | throw $needUserData; |
||
75 | } |
||
76 | |||
77 | $this->container->get('stfalcon_event.mailer_helper')->sendEasyEmail( |
||
78 | $this->container->get('translator')->trans('registration.email.subject'), |
||
79 | '@FOSUser/Registration/email.on_registration.html.twig', |
||
80 | ['user' => $user], |
||
81 | $user |
||
82 | ); |
||
83 | |||
84 | $this->container->get('session')->getFlashBag()->set('fos_user_success', 'registration.flash.user_created'); |
||
85 | } |
||
86 | $service = $response->getResourceOwner()->getName(); |
||
87 | $socialID = $response->getUsername(); |
||
88 | switch ($service) { |
||
89 | case 'google': |
||
90 | $user->setGoogleID($socialID); |
||
91 | break; |
||
92 | case 'facebook': |
||
93 | $user->setFacebookID($socialID); |
||
94 | break; |
||
95 | } |
||
96 | |||
97 | $this->userManager->updateUser($user); |
||
98 | } else { |
||
99 | $checker = new UserChecker(); |
||
100 | $checker->checkPreAuth($user); |
||
101 | } |
||
102 | |||
103 | return $user; |
||
104 | } |
||
106 |