Passed
Pull Request — master (#20)
by Hilmi Erdem
10:06
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 2021 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\Otp\Http\Middleware;
9
10
use Closure;
11
use Erdemkeren\Otp\OtpToken;
12
use Illuminate\Http\Request;
13
use Erdemkeren\Otp\OtpFacade;
14
use Illuminate\Http\RedirectResponse;
15
use Erdemkeren\Otp\Exceptions\AuthenticationException;
16
17
class Otp
18
{
19
    /**
20
     * Handle an incoming request.
21
     *
22
     * @param Request     $request
23
     * @param Closure     $next
24
     * @param string|null $guard
25
     *
26
     * @return mixed
27
     */
28 5
    public function handle(Request $request, Closure $next, ?string $guard = null): mixed
29
    {
30 5
        if (! $user = $request->user($guard)) {
31 1
            throw AuthenticationException::create();
32 1
        }
33
34
        if (!$cipher = $request->cookie('otp_token') || $request->header('otp_token')) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $cipher = ($request->coo...t->header('otp_token')), Probably Intended Meaning: ($cipher = $request->coo...st->header('otp_token')
Loading history...
35
            OtpFacade::sendNewOtp($user);
36 4
37 2
            return $this->redirectToOtpPage();
38
        }
39 1
40
        $token = OtpFacade::retrieveByCipherText($cipher);
0 ignored issues
show
Bug introduced by
$cipher of type true is incompatible with the type string expected by parameter $cipherText of Erdemkeren\Otp\OtpFacade::retrieveByCipherText(). ( Ignorable by Annotation )

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

40
        $token = OtpFacade::retrieveByCipherText(/** @scrutinizer ignore-type */ $cipher);
Loading history...
41
42 2
        if (! $token || $token->expired()) {
43 2
            OtpFacade::sendNewOtp($user);
44 2
45
            return $this->redirectToOtpPage();
46
        }
47 2
48 1
        $request->macro('otpToken', function () use ($token): OtpToken {
49
            return $token;
50 1
        });
51
52
        return $next($request);
53
    }
54 1
55 1
    /**
56
     * Get the redirect url if check do not pass.
57 1
     *
58
     * @return RedirectResponse
59
     */
60
    protected function redirectToOtpPage(): RedirectResponse
61
    {
62
        session([
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

62
        /** @scrutinizer ignore-call */ 
63
        session([
Loading history...
63
            'otp_requested'    => true,
64
            'otp_redirect_url' => url()->current(),
0 ignored issues
show
Bug introduced by
The function url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

64
            'otp_redirect_url' => /** @scrutinizer ignore-call */ url()->current(),
Loading history...
65 2
        ]);
66
67 2
        return redirect()->route('otp.create');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

67
        return /** @scrutinizer ignore-call */ redirect()->route('otp.create');
Loading history...
68 2
    }
69
}
70