Passed
Push — task/laravel-breadcrumbs ( 3beccb...a96280 )
by Yonathan
10:46 queued 10s
created

Google2FA::handle()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 33
rs 7.6666
c 0
b 0
f 0
cc 10
nc 11
nop 2

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
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use PragmaRX\Google2FALaravel\Support\Authenticator;
7
use Illuminate\Support\Facades\Cookie;
8
use Illuminate\Support\Facades\Log;
9
use Illuminate\Support\Facades\Session;
10
use Illuminate\Support\Facades\URL;
11
12
/**
13
 * Based on \PragmaRX\Google2FALaravel\Middleware
14
 */
15
class Google2FA
16
{
17
    public function handle($request, Closure $next)
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function handle()
Loading history...
18
    {
19
        $authenticator = app(Authenticator::class)->boot($request);
20
        $user = $request->user();
21
22
        if ($user !== null) {
23
            $remember = $request->cookie($user->getRememberDeviceKey());
24
            // If tokens do not match, cookie is no longer valid.
25
            if ($remember !== null && $remember !== $user->getRememberDeviceToken()) {
26
                    Cookie::queue(
27
                        Cookie::forget($user->getRememberDeviceKey())
28
                    );
29
            }
30
31
            if ($authenticator->isAuthenticated() || ($remember !== null && $remember === $user->getRememberDeviceToken())) {
32
                if (!$authenticator->isAuthenticated()) {
33
                    Log::notice('User skipped OTP entry with known device.', ['id' => $request->user()->id]);
34
                    $authenticator->login();
35
                }
36
                return $next($request);
37
            }
38
        } else {
39
            if ($authenticator->isAuthenticated()) {
40
                return $next($request);
41
            }
42
        }
43
44
        // Unlike \PragmaRX\Google2FALaravel\Middleware, set the intended url.
45
        // Check if the intended url already exists, if not then store in global session.
46
        if (!session()->has('url.expected')) {
47
            Session::put('url.expected', URL::full());
48
        }
49
        return $authenticator->makeRequestOneTimePasswordResponse();
50
    }
51
}
52