Completed
Push — master ( ac6145...bdf07d )
by Şəhriyar
14:49
created

AuthController::getOAuth()   C

Complexity

Conditions 12
Paths 19

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 32
ccs 0
cts 18
cp 0
rs 5.1612
cc 12
eloc 19
nc 19
nop 4
crap 156

How to fix   Complexity   

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 namespace App\Http\Controllers\Users;
2
3
use App\Contracts\Registrar;
4
use App\Exceptions\Users\LoginNotValidException;
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
use Laravel\Socialite\Contracts\Factory as SocialiteContract;
8
use Laravel\Socialite\AbstractUser as SocialiteUser;
9
10
class AuthController extends Controller
11
{
12
    private $redirectTo;
13
14
    /**
15
     * Create a new authentication controller instance.
16
     */
17 10
    public function __construct()
18
    {
19 10
        if (!empty($locale = app('translator')->getLocale()) && $locale != app('config')->get('app.locale')) {
20 2
            $this->redirectTo = '/' . $locale . $this->redirectTo;
21 2
        }
22
23 10
        $this->middleware('guest', ['except' => 'getLogout']);
24 10
    }
25
26
    /**
27
     * Handle OAuth login.
28
     *
29
     * @param \Illuminate\Http\Request             $request
30
     * @param \App\Contracts\Registrar             $registrar
31
     * @param \Laravel\Socialite\Contracts\Factory $socialite
32
     * @param string                               $provider
33
     *
34
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
35
     */
36
    public function getOAuth(Request $request, Registrar $registrar, SocialiteContract $socialite, $provider)
37
    {
38
        switch ($provider) {
39
            case 'google':
40
            case 'facebook':
41
                if (!$request->exists('code')) {
42
                    return redirect('/login')->withErrors(trans('passwords.oauth_failed'));
43
                }
44
                break;
45
            case 'twitter':
46
                if (!$request->exists('oauth_token') || !$request->exists('oauth_verifier')) {
47
                    return redirect('/login')->withErrors(trans('passwords.oauth_failed'));
48
                }
49
                break;
50
        }
51
52
        /** @var SocialiteUser $userInfo */
53
        $userInfo = $socialite->driver($provider)->user();
54
        if ($registrar->loginViaOAuth($userInfo, $provider)) {
55
            if ($request->ajax() || $request->wantsJson()) {
56
                return response()->json(['message' => 'Login successful']); // TODO: Move to API app (Lumen based?)
57
            }
58
59
            return redirect()->intended($this->redirectPath());
60
        }
61
62
        if ($request->ajax() || $request->wantsJson()) {
63
            throw new LoginNotValidException(trans('passwords.oauth_failed')); // TODO: Move to API app (Lumen based?)
64
        }
65
66
        return redirect('/login')->withErrors(trans('passwords.oauth_failed'));
67
    }
68
69
    /**
70
     * Show the application login form.
71
     *
72
     * @param \Illuminate\Http\Request             $request
73
     * @param \Laravel\Socialite\Contracts\Factory $socialite
74
     * @param string                               $provider
75
     *
76
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View|\Illuminate\Contracts\View\Factory
77
     */
78 6
    public function getLogin(Request $request, SocialiteContract $socialite, $provider = null)
79
    {
80 6
        if (!is_null($provider)) {
81
            return $socialite->driver($provider)->redirect();
82
        }
83
84 6
        if ($request->ajax() || $request->wantsJson()) {
85
            return response()->json(['message' => 'Ready']);
86
        }
87
88 6
        return view('auth/login');
89
    }
90
91
    /**
92
     * Log the user in.
93
     *
94
     * @param \Illuminate\Http\Request $request
95
     * @param \App\Contracts\Registrar $registrar
96
     *
97
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
98
     */
99 4
    public function postLogin(Request $request, Registrar $registrar)
100
    {
101 4
        $user = $registrar->login();
102
103 2
        if ($request->ajax() || $request->wantsJson()) {
104 1
            return response()->json(['message' => 'Login successful', 'data' => $user]);
105
        }
106
107 1
        return redirect()->intended($this->redirectPath());
108
    }
109
110
    /**
111
     * Log the user out of the application.
112
     *
113
     * @param \Illuminate\Http\Request $request
114
     * @param \App\Contracts\Registrar $registrar
115
     *
116
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
117
     */
118 2
    public function getLogout(Request $request, Registrar $registrar)
119
    {
120 2
        $registrar->logout();
121
122 2
        if ($request->ajax() || $request->wantsJson()) {
123 1
            return response()->json(['message' => 'Logout successful']);
124
        }
125
126 1
        return redirect()->intended($this->redirectPath());
127
    }
128
129
    /**
130
     * Get the post register / login redirect path.
131
     *
132
     * @return string
133
     */
134 2
    private function redirectPath()
135
    {
136 2
        if (isset($this->redirectPath)) {
137
            return $this->redirectPath;
0 ignored issues
show
Bug introduced by
The property redirectPath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
138
        }
139
140 2
        return isset($this->redirectTo) ? $this->redirectTo : '/';
141
    }
142
}
143