Passed
Push — master ( 71a230...3e0e39 )
by Jeremy
04:55 queued 11s
created

SocialController::getSocialHandle()   C

Complexity

Conditions 12
Paths 74

Size

Total Lines 96
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 0
Metric Value
cc 12
eloc 57
c 10
b 2
f 0
nc 74
nop 2
dl 0
loc 96
rs 6.5115

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\Http\Request;
12
use Illuminate\Support\Facades\Config;
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
    private $redirectSuccessLogin = 'home';
21
22
    /**
23
     * Gets the social redirect.
24
     *
25
     * @param string $provider The provider
26
     *
27
     * @return Redirect
28
     */
29
    public function getSocialRedirect($provider)
30
    {
31
        $providerKey = Config::get('services.'.$provider);
32
33
        if (empty($providerKey)) {
34
            return view('pages.status')
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.statu...('socials.noProvider')) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Auth\Redirect.
Loading history...
35
                ->with('error', trans('socials.noProvider'));
36
        }
37
38
        return Socialite::driver($provider)->redirect();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Laravel\Socialite...($provider)->redirect() returns the type Symfony\Component\HttpFoundation\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Auth\Redirect.
Loading history...
39
    }
40
41
    /**
42
     * Gets the social handle.
43
     *
44
     * @param string $provider The provider
45
     *
46
     * @return Redirect
47
     */
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')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->to('l...rans('socials.denied')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Auth\Redirect.
Loading history...
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();
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...
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)
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...
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);
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...
82
                if (count($fullname) == 1) {
83
                    $fullname[1] = '';
84
                }
85
                $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...
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'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($this->r...ials.registerSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Auth\Redirect.
Loading history...
137
        }
138
139
        $socialUser = $userCheck;
140
141
        auth()->login($socialUser, true);
142
143
        return redirect($this->redirectSuccessLogin);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($this->redirectSuccessLogin) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Auth\Redirect.
Loading history...
144
    }
145
146
    /**
147
     * Check if username against database and return valid username.
148
     * If username is not in the DB return the username
149
     * else generate, check, and return the username.
150
     *
151
     * @param string $username
152
     * @param string $email
153
     *
154
     * @return string
155
     */
156
    public function checkUserName($username, $email)
0 ignored issues
show
Unused Code introduced by
The parameter $email is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

156
    public function checkUserName($username, /** @scrutinizer ignore-unused */ $email)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
    {
158
        $userNameCheck = User::where('name', '=', $username)->first();
159
160
        if ($userNameCheck) {
161
            $i = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $i is dead and can be removed.
Loading history...
162
            do {
163
                $username = $this->generateUserName($username);
164
                $newCheck = User::where('name', '=', $username)->first();
165
166
                if ($newCheck == null) {
167
                    $newCheck = 0;
168
                } else {
169
                    $newCheck = count($newCheck);
170
                }
171
            } while ($newCheck != 0);
172
        }
173
174
        return $username;
175
    }
176
177
    /**
178
     * Generate Username.
179
     *
180
     * @param string $username
181
     *
182
     * @return string
183
     */
184
    public function generateUserName($username)
185
    {
186
        return $username.'_'.str_random(10);
187
    }
188
}
189