Completed
Push — socialite ( 2694eb )
by Fèvre
11:22
created

SocialiteController::findOrCreateUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
namespace Xetaravel\Http\Controllers\Auth;
3
4
use Exception;
5
use Illuminate\Http\Request;
6
use Illuminate\Foundation\Auth\RedirectsUsers;
7
use Illuminate\Support\Facades\Auth;
8
use Laravel\Socialite\Facades\Socialite;
9
use Laravel\Socialite\Two\User as ProviderUser;
10
use Xetaravel\Events\RegisterEvent;
11
use Xetaravel\Http\Controllers\Controller;
12
use Xetaravel\Models\User;
13
use Xetaravel\Models\Repositories\UserRepository;
14
use Xetaravel\Models\Role;
15
16
class SocialiteController extends Controller
17
{
18
    use RedirectsUsers;
19
20
    /**
21
     * Where to redirect users after login.
22
     *
23
     * @var string
24
     */
25
    protected $redirectTo = '/';
26
27
    /**
28
     * Redirect the user to the GitHub authentication page.
29
     *
30
     * @return Response
31
     */
32
    public function redirectToProvider()
33
    {
34
        return Socialite::driver('github')->redirect();
35
    }
36
37
    /**
38
     * Obtain the user information from GitHub.
39
     *
40
     * @return Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
41
     */
42
    public function handleProviderCallback(Request $request)
43
    {
44
        try {
45
            $user = Socialite::driver('github')->user();
46
        } catch (Exception $e) {
47
            return redirect()
48
                ->route('users.auth.login')
49
                ->with('danger', 'An error occurred while getting your information from GitHub !');
50
        }
51
        $user = $this->findOrCreateUser($user);
52
53
        Auth::login($user, true);
54
55
        return $this->authenticated($request, $user) ?: redirect()->intended($this->redirectPath());
56
    }
57
58
    /**
59
     * The user has been authenticated.
60
     *
61
     * @param \Illuminate\Http\Request $request The request object.
62
     * @param \Xetaravel\Models\User $user The user that has been logged in.
63
     *
64
     * @return void
65
     */
66 View Code Duplication
    protected function authenticated(Request $request, $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        event(new RegisterEvent($user));
69
70
        $request->session()->flash(
0 ignored issues
show
Bug introduced by
The method flash() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            'success',
72
            'Welcome back <strong>' . e($user->username) . '</strong>! You\'re successfully connected !'
73
        );
74
    }
75
76
    /**
77
     * Find the user if he exists or create it.
78
     *
79
     * @param \Laravel\Socialite\Two\User $providerUser
80
     *
81
     * @return \Xetaravel\Models\User
82
     */
83
    protected function findOrCreateUser(ProviderUser $providerUser): User
84
    {
85
        if ($user = User::where('github_id', $providerUser->id)->first()) {
86
            return $user;
87
        }
88
89
        $user = UserRepository::create(
90
            [
91
                'username' => $providerUser->nickname,
92
                'email' => $providerUser->email
93
            ],
94
            [
95
                'github_id' => $providerUser->id
96
            ],
97
            true
98
        );
99
100
        $this->registered($user);
101
102
        return $user;
103
    }
104
105
    /**
106
     * The user has been registered.
107
     *
108
     * @param \Xetaravel\Models\User $user The user that has been registered.
109
     *
110
     * @return void
111
     */
112 View Code Duplication
    protected function registered(User $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        $role = Role::where('slug', 'user')->first();
115
        $user->attachRole($role);
116
117
        $user->clearMediaCollection('avatar');
118
        $user->addMedia(resource_path('assets/images/avatar.png'))
119
            ->preservingOriginal()
120
            ->setName(substr(md5($user->username), 0, 10))
121
            ->setFileName(substr(md5($user->username), 0, 10) . '.png')
122
            ->toMediaCollection('avatar');
123
    }
124
}
125