Completed
Push — master ( 8bec52...18abe2 )
by Hilmi Erdem
02:42
created

OtpAccess::handle()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 17
cts 17
cp 1
rs 9.1128
c 0
b 0
f 0
cc 5
nc 4
nop 3
crap 5
1
<?php
2
3
/*
4
 * @copyright 2018 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\TemporaryAccess\Http\Middleware;
9
10
use Closure;
11
use Illuminate\Http\Request;
12
use Illuminate\Http\RedirectResponse;
13
use Erdemkeren\TemporaryAccess\TokenInterface;
14
use Illuminate\Contracts\Auth\Authenticatable;
15
use Erdemkeren\TemporaryAccess\TemporaryAccessFacade as TemporaryAccess;
16
17
class OtpAccess
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 = TemporaryAccess::retrieveByCipherText(
43 2
            $user->getAuthIdentifier(),
44 2
            $request->cookie('otp_token')
45
        );
46
47 2
        if (! $token || $token->expired()) {
48 1
            $this->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
    /**
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 = TemporaryAccess::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());
1 ignored issue
show
Bug introduced by
The method notify() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91 2
    }
92
}
93