Completed
Push — master ( 12e529...b07676 )
by Travis
02:18
created

SocialAuthController::getProviderDetails()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 5
nop 1
1
<?php
2
3
namespace NukaCode\Users\Http\Controllers;
4
5
use App\Http\Controllers\BaseController;
6
use App\Models\User;
7
use Laravel\Socialite\Facades\Socialite;
8
use NukaCode\Users\Events\UserLoggedIn;
9
use NukaCode\Users\Events\UserRegistered;
10
11
class SocialAuthController extends BaseController
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $providers;
17
18
    /**
19
     * @var string
20
     */
21
    protected $driver;
22
23
    /**
24
     * @var array
25
     */
26
    protected $scopes;
27
28
    /**
29
     * @var array
30
     */
31
    protected $extras;
32
33
    public function __construct()
34
    {
35
        parent::__construct();
36
37
        $this->providers = collect(config('nukacode-user.providers'))->keyBy('driver');
1 ignored issue
show
Documentation Bug introduced by
It seems like collect(config('nukacode...ers'))->keyBy('driver') of type object<Illuminate\Support\Collection> is incompatible with the declared type array of property $providers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
    }
39
40
    /**
41
     * Redirect the user to the social providers auth page.
42
     *
43
     * @param null|string $provider
44
     *
45
     * @return mixed
46
     */
47
    public function login($provider = null)
48
    {
49
        $this->getProviderDetails($provider);
50
51
        return Socialite::driver($this->driver)
52
                        ->scopes($this->scopes)
53
                        ->with($this->extras)
54
                        ->redirect();
55
    }
56
57
    /**
58
     * Use the returned user to register (if needed) and login.
59
     *
60
     * @param null|string $provider
61
     *
62
     * @return mixed
63
     */
64
    public function callback($provider = null)
65
    {
66
        $this->getProviderDetails($provider);
67
68
        $socialUser = Socialite::driver($this->driver)->user();
69
        $user       = User::where('email', $socialUser->getEmail())->first();
70
71
        if (is_null($user)) {
72
            $user = $this->register($socialUser);
73
        }
74
75
        if (! $user->hasProvider($this->driver)) {
76
            $user->addSocial($socialUser, $this->driver);
77
        }
78
79
        auth()->login($user, request('remember', false));
80
        event(new UserLoggedIn($user));
0 ignored issues
show
Documentation introduced by
new \NukaCode\Users\Events\UserLoggedIn($user) is of type object<NukaCode\Users\Events\UserLoggedIn>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
82
        return redirect()
83
            ->intended(route('home'))
84
            ->with('message', 'You have been logged in.');
85
    }
86
87
    /**
88
     * Create a new user from a social user.
89
     *
90
     * @param $socialUser
91
     *
92
     * @return mixed
93
     */
94
    private function register($socialUser)
95
    {
96
        $names = explode(' ', $socialUser->getName());
97
        $username = is_null($socialUser->getNickname()) ? $socialUser->getEmail() : $socialUser->getNickname();
98
99
        $userDetails = [
100
            'username'      => $username,
101
            'email'         => $socialUser->getEmail(),
102
            'first_name'    => isset($names[0]) ? $names[0] : null,
103
            'last_name'     => isset($names[1]) ? $names[1] : null,
104
            'display_name'  => $username,
105
        ];
106
107
        $user = User::create($userDetails);
108
        $user->assignRole(config('nukacode-user.default'));
109
        $user->addSocial($socialUser, $this->driver);
110
111
        event(new UserRegistered($user));
0 ignored issues
show
Documentation introduced by
new \NukaCode\Users\Events\UserRegistered($user) is of type object<NukaCode\Users\Events\UserRegistered>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
113
        return $user;
114
    }
115
116
    /**
117
     * Log the user out.
118
     *
119
     * @return mixed
120
     */
121
    public function logout()
122
    {
123
        auth()->logout();
124
125
        return redirect(route('home'))
126
            ->with('message', 'You have been logged out.');
127
    }
128
129
    /**
130
     * Find the provider's driver, scopes and extras based on a given provider name.
131
     *
132
     * @param $provider
133
     *
134
     * @throws \Exception
135
     */
136
    private function getProviderDetails($provider)
137
    {
138
        if (empty($this->providers)) {
139
            throw new \Exception('No Providers have been set in nukacode-user config.');
140
        }
141
142
        $provider = is_null($provider) ? $this->providers->first() : $this->providers->get($provider);
2 ignored issues
show
Bug introduced by
The method first cannot be called on $this->providers (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method get cannot be called on $this->providers (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
143
144
        if (is_null($provider['driver'])) {
145
            throw new \InvalidArgumentException('You must set a social driver to use the social authenticating features.');
146
        }
147
148
        $this->driver = $provider['driver'];
149
        $this->scopes = $provider['scopes'];
150
        $this->extras = $provider['extras'];
151
    }
152
}
153