Completed
Pull Request — develop (#42)
by Abdelrahman
03:12 queued 01:27
created

handleProviderCallback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 48
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 48
rs 9.125
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Controllers\Frontarea;
6
7
use Illuminate\Http\Request;
8
use Laravel\Socialite\Facades\Socialite;
9
use Illuminate\Database\Eloquent\Builder;
10
11
class SocialAuthenticationController extends AuthenticationController
12
{
13
    /**
14
     * Redirect the user to the provider authentication page.
15
     *
16
     * @param string $provider
17
     *
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function redirectToProvider(string $provider)
21
    {
22
        return Socialite::driver($provider)->redirect();
23
    }
24
25
    /**
26
     * Obtain the user information from Provider.
27
     *
28
     * @param string $provider
29
     *
30
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
31
     */
32
    public function handleProviderCallback(string $provider)
33
    {
34
        $providerUser = Socialite::driver($provider)->user();
35
36
        $attributes = [
37
            'id' => $providerUser->id,
38
            'email' => $providerUser->email,
39
            'username' => $providerUser->nickname ?? trim(mb_strstr($providerUser->email, '@', true)),
40
            'first_name' => trim(mb_strstr($providerUser->name, ' ', true)),
41
            'last_name' => trim(mb_strstr($providerUser->name, ' ')),
42
        ];
43
44
        //switch ($provider) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
        //    case 'twitter':
46
        //        $attributes['bio'] = $providerUser->user['description'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
        //        $attributes['profile_picture'] = $providerUser->avatar_original;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
        //        break;
49
        //    case 'github':
50
        //        $attributes['bio'] = $providerUser->user['bio'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
        //        $attributes['profile_picture'] = $providerUser->avatar;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
        //        break;
53
        //    case 'facebook':
54
        //        $attributes['profile_picture'] = $providerUser->avatar_original;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
        //        break;
56
        //    case 'linkedin':
57
        //        $attributes['bio'] = $providerUser->headline;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
        //        $attributes['profile_picture'] = $providerUser->avatar_original;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
        //        break;
60
        //    case 'google':
61
        //        $attributes['bio'] = $providerUser->tagline;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
        //        $attributes['profile_picture'] = $providerUser->avatar_original;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
        //        break;
64
        //}
65
66
        //dd($providerUser, $attributes);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
68
        if (! ($localUser = $this->getLocalUser($provider, $providerUser->id))) {
69
            $localUser = $this->createLocalUser($provider, $attributes);
70
        }
71
72
        $loginResult = auth()->guard($this->getGuard())->attempt([
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73
            'is_active' => $localUser->is_active,
74
            'email' => $localUser->email,
75
            'social' => true,
76
        ], true);
77
78
        return $this->getLoginResponse(request(), $loginResult);
79
    }
80
81
    /**
82
     * Get local user for the given provider.
83
     *
84
     * @param string $provider
85
     * @param string $providerUserId
86
     *
87
     * @return \Illuminate\Database\Eloquent\Model|null
88
     */
89
    protected function getLocalUser(string $provider, string $providerUserId)
90
    {
91
        return app('rinvex.fort.user')->whereHas('socialites', function (Builder $builder) use ($provider, $providerUserId) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
92
            $builder->where('provider', $provider)->where('provider_uid', $providerUserId);
93
        })->first();
94
    }
95
96
    /**
97
     * Create local user for the given provider.
98
     *
99
     * @param string $provider
100
     * @param array  $attributes
101
     *
102
     * @return \Illuminate\Database\Eloquent\Model|null
103
     */
104
    protected function createLocalUser(string $provider, array $attributes)
105
    {
106
        $defaultRole = app('rinvex.fort.role')->where('slug', config('rinvex.fort.registration.default_role'))->first();
107
108
        $attributes['password'] = str_random();
109
        $attributes['email_verified'] = true;
110
        $attributes['email_verified_at'] = now();
111
        $attributes['is_active'] = ! config('rinvex.fort.registration.moderated');
112
        $attributes['roles'] = $defaultRole ? [$defaultRole->id] : null;
113
114
        $localUser = app('rinvex.fort.user')->fill($attributes)->save();
115
116
        // Fire the register success event
117
        event('rinvex.fort.register.success', [$localUser]);
118
119
        $localUser->socialites()->create([
120
            'provider' => $provider,
121
            'provider_uid' => $attributes['id'],
122
        ]);
123
124
        return $localUser;
125
    }
126
}
127