| Conditions | 12 |
| Paths | 74 |
| Total Lines | 96 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| Bugs | 2 | 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 |
||
| 48 | public function getSocialHandle($provider, Request $request) |
||
| 49 | { |
||
| 50 | $denied = $request->denied ? $request->denied : null; |
||
| 51 | $socialUser = null; |
||
| 52 | |||
| 53 | if ($denied != null || $denied != '') { |
||
| 54 | return redirect()->to('login') |
||
| 55 | ->with('status', 'danger') |
||
| 56 | ->with('message', trans('socials.denied')); |
||
| 57 | } |
||
| 58 | |||
| 59 | $socialUserObject = Socialite::driver($provider)->user(); |
||
| 60 | |||
| 61 | // Check if email is already registered |
||
| 62 | $userCheck = User::where('email', '=', $socialUserObject->email)->first(); |
||
| 63 | |||
| 64 | $email = $socialUserObject->email; |
||
| 65 | |||
| 66 | if (! $socialUserObject->email) { |
||
| 67 | $email = 'missing'.str_random(10).'@'.str_random(10).'.example.org'; |
||
| 68 | } |
||
| 69 | |||
| 70 | // If user is not registered |
||
| 71 | if (empty($userCheck)) { |
||
| 72 | $sameSocialId = Social::where('social_id', '=', $socialUserObject->id) |
||
| 73 | ->where('provider', '=', $provider) |
||
| 74 | ->first(); |
||
| 75 | |||
| 76 | if (empty($sameSocialId)) { |
||
| 77 | $ipAddress = new CaptureIpTrait(); |
||
| 78 | $socialData = new Social(); |
||
| 79 | $profile = new Profile(); |
||
| 80 | $role = Role::where('slug', '=', 'user')->first(); |
||
| 81 | $fullname = explode(' ', $socialUserObject->name); |
||
| 82 | if (count($fullname) == 1) { |
||
| 83 | $fullname[1] = ''; |
||
| 84 | } |
||
| 85 | $username = $socialUserObject->nickname; |
||
| 86 | |||
| 87 | if ($username == null) { |
||
| 88 | foreach ($fullname as $name) { |
||
| 89 | $username .= $name; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | // Check to make sure username does not already exist in DB before recording |
||
| 94 | $username = $this->checkUserName($username, $email); |
||
| 95 | |||
| 96 | $user = User::create([ |
||
| 97 | 'name' => $username, |
||
| 98 | 'first_name' => $fullname[0], |
||
| 99 | 'last_name' => $fullname[1], |
||
| 100 | 'email' => $email, |
||
| 101 | 'password' => bcrypt(str_random(40)), |
||
| 102 | 'token' => str_random(64), |
||
| 103 | 'activated' => true, |
||
| 104 | 'signup_sm_ip_address' => $ipAddress->getClientIp(), |
||
| 105 | |||
| 106 | ]); |
||
| 107 | |||
| 108 | $socialData->social_id = $socialUserObject->id; |
||
| 109 | $socialData->provider = $provider; |
||
| 110 | $user->social()->save($socialData); |
||
| 111 | $user->attachRole($role); |
||
| 112 | $user->activated = true; |
||
| 113 | |||
| 114 | $user->profile()->save($profile); |
||
| 115 | $user->save(); |
||
| 116 | |||
| 117 | if ($socialData->provider == 'github') { |
||
| 118 | $user->profile->github_username = $socialUserObject->nickname; |
||
| 119 | } |
||
| 120 | |||
| 121 | // Twitter User Object details: https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object |
||
| 122 | if ($socialData->provider == 'twitter') { |
||
| 123 | //$user->profile()->twitter_username = $socialUserObject->screen_name; |
||
| 124 | //If the above fails try (The documentation shows screen_name however so Twitters docs may be out of date.): |
||
| 125 | $user->profile()->twitter_username = $socialUserObject->nickname; |
||
| 126 | } |
||
| 127 | $user->profile->save(); |
||
| 128 | |||
| 129 | $socialUser = $user; |
||
| 130 | } else { |
||
| 131 | $socialUser = $sameSocialId->user; |
||
| 132 | } |
||
| 133 | |||
| 134 | auth()->login($socialUser, true); |
||
| 135 | |||
| 136 | return redirect($this->redirectSuccessLogin)->with('success', trans('socials.registerSuccess')); |
||
| 137 | } |
||
| 138 | |||
| 139 | $socialUser = $userCheck; |
||
| 140 | |||
| 141 | auth()->login($socialUser, true); |
||
| 142 | |||
| 143 | return redirect($this->redirectSuccessLogin); |
||
| 144 | } |
||
| 189 |