| Conditions | 10 |
| Paths | 37 |
| Total Lines | 87 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 32 | public function getSocialHandle($provider) |
||
| 33 | { |
||
| 34 | if (Input::get('denied') != '') { |
||
| 35 | return redirect()->to('login') |
||
| 36 | ->with('status', 'danger') |
||
| 37 | ->with('message', trans('socials.denied')); |
||
| 38 | } |
||
| 39 | |||
| 40 | $socialUserObject = Socialite::driver($provider)->user(); |
||
| 41 | |||
| 42 | $socialUser = null; |
||
| 43 | |||
| 44 | // Check if email is already registered |
||
| 45 | $userCheck = User::where('email', '=', $socialUserObject->email)->first(); |
||
| 46 | |||
| 47 | $email = $socialUserObject->email; |
||
| 48 | |||
| 49 | if (!$socialUserObject->email) { |
||
| 50 | $email = 'missing'.str_random(10); |
||
| 51 | } |
||
| 52 | |||
| 53 | if (empty($userCheck)) { |
||
| 54 | $sameSocialId = Social::where('social_id', '=', $socialUserObject->id) |
||
| 55 | ->where('provider', '=', $provider) |
||
| 56 | ->first(); |
||
| 57 | |||
| 58 | if (empty($sameSocialId)) { |
||
| 59 | $ipAddress = new CaptureIpTrait(); |
||
| 60 | $socialData = new Social(); |
||
| 61 | $profile = new Profile(); |
||
| 62 | $role = Role::where('name', '=', 'user')->first(); |
||
| 63 | $fullname = explode(' ', $socialUserObject->name); |
||
| 64 | if (count($fullname) == 1) { |
||
| 65 | $fullname[1] = ''; |
||
| 66 | } |
||
| 67 | $username = $socialUserObject->nickname; |
||
| 68 | |||
| 69 | if ($username == null) { |
||
| 70 | foreach ($fullname as $name) { |
||
| 71 | $username .= $name; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $user = User::create([ |
||
| 76 | 'name' => $username, |
||
| 77 | 'first_name' => $fullname[0], |
||
| 78 | 'last_name' => $fullname[1], |
||
| 79 | 'email' => $email, |
||
| 80 | 'password' => bcrypt(str_random(40)), |
||
| 81 | 'token' => str_random(64), |
||
| 82 | 'activated' => true, |
||
| 83 | 'signup_sm_ip_address' => $ipAddress->getClientIp(), |
||
| 84 | |||
| 85 | ]); |
||
| 86 | |||
| 87 | $socialData->social_id = $socialUserObject->id; |
||
| 88 | $socialData->provider = $provider; |
||
| 89 | $user->social()->save($socialData); |
||
| 90 | $user->attachRole($role); |
||
| 91 | $user->activated = true; |
||
| 92 | |||
| 93 | $user->profile()->save($profile); |
||
| 94 | $user->save(); |
||
| 95 | |||
| 96 | if ($socialData->provider == 'github') { |
||
| 97 | $user->profile->github_username = $socialUserObject->nickname; |
||
| 98 | } |
||
| 99 | if ($socialData->provider == 'twitter') { |
||
| 100 | $user->profile()->twitter_username = $socialUserObject->username; |
||
| 101 | } |
||
| 102 | $user->profile->save(); |
||
| 103 | |||
| 104 | $socialUser = $user; |
||
| 105 | } else { |
||
| 106 | $socialUser = $sameSocialId->user; |
||
| 107 | } |
||
| 108 | |||
| 109 | auth()->login($socialUser, true); |
||
| 110 | |||
| 111 | return redirect('home')->with('success', trans('socials.registerSuccess')); |
||
| 112 | } |
||
| 113 | |||
| 114 | $socialUser = $userCheck; |
||
| 115 | |||
| 116 | auth()->login($socialUser, true); |
||
| 117 | |||
| 118 | return redirect('home'); |
||
| 119 | } |
||
| 121 |