SocialController::getSocialHandle()   B
last analyzed

Complexity

Conditions 10
Paths 37

Size

Total Lines 87
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 55
c 2
b 0
f 0
nc 37
nop 1
dl 0
loc 87
rs 7.1151

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\Profile;
7
use App\Models\Social;
8
use App\Models\User;
9
use App\Traits\ActivationTrait;
10
use App\Traits\CaptureIpTrait;
11
use Illuminate\Support\Facades\Config;
12
use Illuminate\Support\Facades\Input;
13
use jeremykenedy\LaravelRoles\Models\Role;
14
use Laravel\Socialite\Facades\Socialite;
15
16
class SocialController extends Controller
17
{
18
    use ActivationTrait;
0 ignored issues
show
Bug introduced by
The trait App\Traits\ActivationTrait requires the property $email which is not provided by App\Http\Controllers\Auth\SocialController.
Loading history...
19
20
    public function getSocialRedirect($provider)
21
    {
22
        $providerKey = Config::get('services.'.$provider);
23
24
        if (empty($providerKey)) {
25
            return view('pages.status')
26
                ->with('error', trans('socials.noProvider'));
27
        }
28
29
        return Socialite::driver($provider)->redirect();
30
    }
31
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();
0 ignored issues
show
Bug introduced by
Accessing email on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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)
0 ignored issues
show
Bug introduced by
Accessing id on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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);
0 ignored issues
show
Bug introduced by
Accessing name on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
64
                if (count($fullname) == 1) {
65
                    $fullname[1] = '';
66
                }
67
                $username = $socialUserObject->nickname;
0 ignored issues
show
Bug introduced by
Accessing nickname on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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;
0 ignored issues
show
Bug introduced by
Accessing username on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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
    }
120
}
121