Otp::sendNewOtpToUser()   A
last analyzed

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 Erdemkeren\Otp\OtpFacade;
12
use Erdemkeren\Otp\TokenInterface;
13
use Illuminate\Contracts\Auth\Authenticatable;
14
use Illuminate\Http\RedirectResponse;
15
use Illuminate\Http\Request;
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
            $this->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
            $this->sendNewOtpToUser($user);
49
50 1
            return $this->redirectToOtpPage();
51
        }
52
53
        $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
    /**
76
     * Create a new otp and notify the user.
77
     *
78
     * @param Authenticatable $user
79
     */
80 3
    private function sendNewOtpToUser(Authenticatable $user): void
81
    {
82 3
        $token = OtpFacade::create($user, 6);
83
84 3
        if (! method_exists($user, 'notify')) {
85 1
            throw new \UnexpectedValueException(
86 1
                'The otp owner should be an instance of notifiable or implement the notify method.'
87
            );
88
        }
89
90 2
        $user->notify($token->toNotification());
0 ignored issues
show
Unused Code introduced by
The call to Erdemkeren\Otp\Http\Midd...Authenticable::notify() has too many arguments starting with $token->toNotification(). ( Ignorable by Annotation )

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

90
        $user->/** @scrutinizer ignore-call */ 
91
               notify($token->toNotification());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91 2
    }
92
}
93