SocialiteController::redirectSocialite()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
eloc 36
c 2
b 1
f 0
nc 5
nop 1
dl 0
loc 56
rs 8.4106

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
namespace Pratiksh\Adminetic\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Hash;
9
use Illuminate\Support\Str;
10
use Laravel\Socialite\Facades\Socialite;
11
use Pratiksh\Adminetic\Models\Admin\Preference;
12
use Pratiksh\Adminetic\Models\Admin\Role;
13
14
class SocialiteController extends Controller
15
{
16
    public function github()
17
    {
18
        return Socialite::driver('github')->stateless()->redirect();
0 ignored issues
show
Bug introduced by
The method stateless() does not exist on Laravel\Socialite\Contracts\Provider. It seems like you code against a sub-type of Laravel\Socialite\Contracts\Provider such as Laravel\Socialite\Two\AbstractProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        return Socialite::driver('github')->/** @scrutinizer ignore-call */ stateless()->redirect();
Loading history...
19
    }
20
21
    public function githubRedirect()
22
    {
23
        return $this->redirectSocialite('github');
24
    }
25
26
    public function facebook()
27
    {
28
        return Socialite::driver('facebook')->stateless()->redirect();
29
    }
30
31
    public function facebookRedirect()
32
    {
33
        return $this->redirectSocialite('facebook');
34
    }
35
36
    public function google()
37
    {
38
        return Socialite::driver('google')->stateless()->redirect();
39
    }
40
41
    public function googleRedirect()
42
    {
43
        return $this->redirectSocialite('google');
44
    }
45
46
    private function redirectSocialite($driver)
47
    {
48
        $socialiteuser = Socialite::driver($driver)->stateless()->user();
49
50
        $user = User::where('email', $socialiteuser->email)->first();
51
52
        if (! isset($user)) {
53
            $user = User::create([
54
                'name' => $socialiteuser->name,
55
                'email' => $socialiteuser->email,
56
                'password' => Hash::make(Str::random(24)),
57
            ]);
58
            // Assigning Default Role to New User
59
            $default_user_role = config('adminetic.default_user_role', 'user');
60
            $default_user_role_level = config('adminetic.default_user_role_level', 1);
61
            $role = Role::where('name', $default_user_role)->first();
62
            if ($role) {
63
                $user->roles()->attach($role);
64
            } else {
65
                Role::create([
66
                    'name' => $default_user_role,
67
                    'description' => 'Default role for newly registered user.',
68
                    'level' => $default_user_role_level,
69
                ]);
70
            }
71
            // Creating Profile
72
            $user->profile()->create([
73
                'username' => $socialiteuser->user->nickname ?? 'N/A',
74
                'address' => $socialiteuser->location ?? 'N/A',
75
                'profile_pic' => $socialiteuser->getAvatar(),
76
            ]);
77
78
            // Creating Preference
79
            $preferences = Preference::all();
80
            if (isset($preferences)) {
81
                foreach ($preferences as $preference) {
82
                    if (! isset($preference->roles)) {
83
                        $user->preferences()->attach($preference->id, [
84
                            'enabled' => $preference->active,
85
                        ]);
86
                    } else {
87
                        if (array_intersect($user->roles->pluck('id')->toArray(), $preference->roles) != null) {
88
                            $user->preferences()->attach($preference->id, [
89
                                'enabled' => $preference->active,
90
                            ]);
91
                        }
92
                    }
93
                }
94
            }
95
            Auth::login($user, true);
96
97
            return redirect(adminRedirectRoute(config('adminetic.oauth_redirect_route_name', 'dashboard')));
98
        } else {
99
            Auth::login($user, true);
100
101
            return redirect(adminRedirectRoute(config('adminetic.oauth_redirect_route_name', 'dashboard')));
102
        }
103
    }
104
}
105