Completed
Push — develop ( 25eb07...507691 )
by Abdelrahman
01:42
created

handleGithubCallback()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 28
nc 5
nop 2
dl 0
loc 51
rs 8.6588
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A SocialAuthenticationController::getLocalUser() 0 6 1

How to fix   Long Method   

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
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
dd($providerUser);
36
        // @TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
37
        switch ($provider) {
38
            case 'github':
39
                $asd = 'asd';
0 ignored issues
show
Unused Code introduced by
$asd is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
                break;
41
        }
42
43
        $attributes = [
44
            'id' => $providerUser->id,
45
            'email' => $providerUser->email,
46
            'username' => $providerUser->nickname,
47
            'last_token' => $providerUser->token,
48
        ];
49
50
        if (! ($localUser = $this->getLocalUser($provider, $providerUser->id))) {
51
            $localUser = $this->createLocalUser($provider, $attributes);
52
        }
53
54
        $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...
55
            'is_active' => $localUser->is_active,
56
            'email' => $localUser->email,
57
            'social' => true,
58
        ], true);
59
60
        return $this->getLoginResponse(request(), $loginResult);
61
    }
62
63
    /**
64
     * Get local user for the given provider.
65
     *
66
     * @param string $provider
67
     * @param int    $providerUserId
68
     *
69
     * @return \Illuminate\Database\Eloquent\Model|null
70
     */
71
    protected function getLocalUser(string $provider, int $providerUserId)
72
    {
73
        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...
74
            $builder->where('provider', $provider)->where('provider_uid', $providerUserId);
75
        })->first();
76
    }
77
78
    /**
79
     * Create local user for the given provider.
80
     *
81
     * @param string $provider
82
     * @param array  $attributes
83
     *
84
     * @return \Illuminate\Database\Eloquent\Model|null
85
     */
86
    protected function createLocalUser(string $provider, array $attributes)
87
    {
88
        $defaultRole = app('rinvex.fort.role')->where('slug', config('rinvex.fort.registration.default_role'))->first();
89
90
        $localUser = app('rinvex.fort.user')->fill([
91
            'password' => str_random(),
92
            'username' => $attributes['username'],
93
            'email' => $attributes['email'],
94
            'email_verified' => true,
95
            'email_verified_at' => now(),
96
            'is_active' => ! config('rinvex.fort.registration.moderated'),
97
            'roles' => $defaultRole ? [$defaultRole->id] : null,
98
        ])->save();
99
100
        // Fire the register success event
101
        event('rinvex.fort.register.success', [$localUser]);
102
103
        $localUser->socialites()->create([
104
            'provider' => $provider,
105
            'provider_uid' => $attributes['id'],
106
            'last_token' => $attributes['last_token'],
107
        ]);
108
109
        return $localUser;
110
    }
111
}
112