Completed
Push — master ( a83fd7...d4f1d7 )
by Jeremy
05:12
created

SocialController::checkUserName()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
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
use Illuminate\Support\Facades\Log;
17
18
class SocialController extends Controller
19
{
20
    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...
21
22
    /**
23
     * Gets the social redirect.
24
     *
25
     * @param string  $provider  The provider
26
     *
27
     * @return Redirect
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Auth\Redirect was not found. Did you mean Redirect? If so, make sure to prefix the type with \.
Loading history...
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)
49
    {
50
        if (Input::get('denied') != '') {
51
            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...
52
                ->with('status', 'danger')
53
                ->with('message', trans('socials.denied'));
54
        }
55
56
        $socialUserObject = Socialite::driver($provider)->user();
57
58
        $socialUser = null;
59
60
        // Check if email is already registered
61
        $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...
62
63
        $email = $socialUserObject->email;
64
65
        if (!$socialUserObject->email) {
66
            $email = 'missing'.str_random(10).'@'.str_random(10).'.example.org';
67
        }
68
69
        if (empty($userCheck)) {
70
            $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...
71
                ->where('provider', '=', $provider)
72
                ->first();
73
74
            if (empty($sameSocialId)) {
75
                $ipAddress = new CaptureIpTrait();
76
                $socialData = new Social();
77
                $profile = new Profile();
78
                $role = Role::where('slug', '=', 'user')->first();
79
                $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...
80
                if (count($fullname) == 1) {
81
                    $fullname[1] = '';
82
                }
83
                $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...
84
85
                if ($username == null) {
86
                    foreach ($fullname as $name) {
87
                        $username .= $name;
88
                    }
89
                }
90
91
                // Check to make sure username does not already exist in DB before recording
92
                $username = $this->checkUserName($username, $email);
93
94
                $user = User::create([
95
                    'name'                 => $username,
96
                    'first_name'           => $fullname[0],
97
                    'last_name'            => $fullname[1],
98
                    'email'                => $email,
99
                    'password'             => bcrypt(str_random(40)),
100
                    'token'                => str_random(64),
101
                    'activated'            => true,
102
                    'signup_sm_ip_address' => $ipAddress->getClientIp(),
103
104
                ]);
105
106
                $socialData->social_id = $socialUserObject->id;
107
                $socialData->provider = $provider;
108
                $user->social()->save($socialData);
109
                $user->attachRole($role);
110
                $user->activated = true;
111
112
                $user->profile()->save($profile);
113
                $user->save();
114
115
                if ($socialData->provider == 'github') {
116
                    $user->profile->github_username = $socialUserObject->nickname;
117
                }
118
119
                // Twitter User Object details: https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object
120
                if ($socialData->provider == 'twitter') {
121
                    $user->profile()->twitter_username = $socialUserObject->screen_name;
0 ignored issues
show
Bug introduced by
Accessing screen_name on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
122
                }
123
                $user->profile->save();
124
125
                $socialUser = $user;
126
            } else {
127
                $socialUser = $sameSocialId->user;
128
            }
129
130
            auth()->login($socialUser, true);
131
132
            return redirect('home')->with('success', trans('socials.registerSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('home')-...ials.registerSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Auth\Redirect.
Loading history...
133
        }
134
135
        $socialUser = $userCheck;
136
137
        auth()->login($socialUser, true);
138
139
        return redirect('home');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('home') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Auth\Redirect.
Loading history...
140
    }
141
142
    /**
143
     * Check if username against database and return valid username.
144
     * If username is not in the DB return the username
145
     * else generate, check, and return the username.
146
     *
147
     * @param string $username
148
     * @param string $email
149
     *
150
     * @return string || re-run method
0 ignored issues
show
Documentation Bug introduced by
The doc comment string || re-run at position 2 could not be parsed: Unknown type name '|' at position 2 in string || re-run.
Loading history...
151
     */
152
    public function checkUserName($username, $email)
153
    {
154
        $userNameCheck = User::where('name', '=', $username)->first();
155
156
        if ($userNameCheck) {
157
            $i = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $i is dead and can be removed.
Loading history...
158
            do {
159
                $username = $this->generateUserName($username, $email);
160
                $newCheck = User::where('name', '=', $username)->first();
161
162
                if ($newCheck == null) {
163
                    $newCheck = 0;
164
                } else {
165
                    $newCheck = count($newCheck);
166
                }
167
168
            } while ($newCheck != 0);
169
        }
170
171
        return $username;
172
    }
173
174
    /**
175
     * Generate Username
176
     *
177
     * @param string $username
178
     * @param string $email
179
     *
180
     * @return string
181
     */
182
    public function generateUserName($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

182
    public function generateUserName($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...
183
    {
184
        return $username . '_' . str_random(10);
185
    }
186
187
}
188