Passed
Pull Request — master (#7)
by
unknown
03:38
created

Otp::sendNewOtpToUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * @copyright 2018 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\Otp\Http\Middleware;
9
10
use Closure;
11
use Illuminate\Http\Request;
12
use Erdemkeren\Otp\OtpFacade;
13
use Erdemkeren\Otp\TokenInterface;
14
use Illuminate\Http\RedirectResponse;
15
use Illuminate\Contracts\Auth\Authenticatable;
16
17
class Otp
18
{
19
    /**
20
     * Handle an incoming request.
21
     *
22
     * @param \Illuminate\Http\Request $request
23
     * @param \Closure                 $next
24
     * @param null|string              $guard
25
     *
26
     * @return mixed
27
     */
28 5
    public function handle(Request $request, Closure $next, $guard = null)
29
    {
30 5
        if (! $user = $request->user($guard)) {
31 1
            throw new \LogicException(
32 1
                'The otp access control middleware requires user authentication via laravel guards.'
33
            );
34
        }
35
36 4
        if (! $request->hasCookie('otp_token')) {
37 2
            OtpFacade::sendNewOtpToUser($user);
38
39 1
            return $this->redirectToOtpPage();
40
        }
41
42 2
        $token = OtpFacade::retrieveByCipherText(
43 2
            $user->getAuthIdentifier(),
44 2
            $request->cookie('otp_token')
0 ignored issues
show
Bug introduced by
It seems like $request->cookie('otp_token') can also be of type array; however, parameter $cipherText of Erdemkeren\Otp\OtpFacade::retrieveByCipherText() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

44
            /** @scrutinizer ignore-type */ $request->cookie('otp_token')
Loading history...
45
        );
46
47 2
        if (! $token || $token->expired()) {
48 1
            OtpFacade::sendNewOtpToUser($user);
49
50 1
            return $this->redirectToOtpPage();
51
        }
52
53 1
        $request->macro('otpToken', function () use ($token): TokenInterface {
54 1
            return $token;
55 1
        });
56
57 1
        return $next($request);
58
    }
59
60
    /**
61
     * Get the redirect url if check do not pass.
62
     *
63
     * @return RedirectResponse
64
     */
65 2
    protected function redirectToOtpPage(): RedirectResponse
66
    {
67 2
        session([
68 2
            'otp_requested'    => true,
69 2
            'otp_redirect_url' => url()->current(),
70
        ]);
71
72 2
        return redirect()->route('otp.create');
73
    }
74
}
75