OtpController::store()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 39
rs 9.2568
c 0
b 0
f 0
ccs 23
cts 23
cp 1
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
/*
4
 * @copyright 2018 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\Otp\Http\Controllers;
9
10
use Erdemkeren\Otp\OtpFacade as Otp;
11
use Erdemkeren\Otp\TokenInterface;
12
use Illuminate\Contracts\Auth\Authenticatable;
13
use Illuminate\Contracts\Validation\Validator as ValidatorInterface;
14
use Illuminate\Contracts\View\View;
15
use Illuminate\Http\RedirectResponse;
16
use Illuminate\Http\Request;
17
use Illuminate\Support\Facades\Validator as ValidatorFacade;
18
19
/**
20
 * Class OtpController.
21
 */
22
class OtpController
23
{
24
    /**
25
     * * Show the form for the otp submission.
26
     *
27
     * @return RedirectResponse|View
28
     */
29 2
    public function create()
30
    {
31 2
        if (! $this->otpHasBeenRequested()) {
32 1
            return redirect('/');
33
        }
34
35 1
        return view('otp.create');
36
    }
37
38
    /**
39
     * Store the otp in cookies and redirect user
40
     * to their original path.
41
     *
42
     * @param Request $request
43
     *
44
     * @return RedirectResponse
45
     */
46 5
    public function store(Request $request): RedirectResponse
47
    {
48 5
        if (! $this->otpHasBeenRequested()) {
49 1
            return redirect('/');
50
        }
51
52 4
        $validator = $this->getOtpSubmissionRequestValidator($request);
53
54 4
        if ($validator->fails()) {
55 1
            return redirect()->back()->withErrors($validator);
56
        }
57
58 3
        if (! $token = $this->retrieveOtpTokenByPlainText(
59 3
            $request->user(),
60 3
            $request->input('password')
61
        )) {
62 1
            $validator->getMessageBag()->add(
63 1
                'password',
64 1
                'The password is not valid.'
65
            );
66
67 1
            return redirect()->back()->withErrors($validator);
68
        }
69
70 2
        if ($token->expired()) {
71 1
            $validator->getMessageBag()->add(
72 1
                'password',
73 1
                'The password is expired.'
74
            );
75
76 1
            return redirect()->back()->withErrors($validator);
77
        }
78
79 1
        session()->forget('otp_requested');
80
81 1
        return redirect()
82 1
            ->to(session()->pull('otp_redirect_url'))
83 1
            ->withCookie(
84 1
                cookie()->make('otp_token', (string) $token, $token->expiryTime() / 60)
85
            );
86
    }
87
88
    /**
89
     * Validate the given otp submission request.
90
     *
91
     * @param Request $request
92
     *
93
     * @return ValidatorInterface
94
     */
95 4
    private function getOtpSubmissionRequestValidator(Request $request): ValidatorInterface
96
    {
97 4
        return ValidatorFacade::make($request->all(), [
98 4
            'password' => 'required|string',
99
        ]);
100
    }
101
102
    /**
103
     * Retrieve a token by the given user and password.
104
     *
105
     * @param Authenticatable $user
106
     * @param string          $password
107
     *
108
     * @return mixed
109
     */
110 3
    private function retrieveOtpTokenByPlainText(Authenticatable $user, string $password): ?TokenInterface
111
    {
112 3
        return Otp::retrieveByPlainText($user, $password);
113
    }
114
115
    /**
116
     * Determine if an otp requested or not.
117
     *
118
     * @return mixed
119
     */
120 7
    private function otpHasBeenRequested()
121
    {
122 7
        return session('otp_requested', false);
123
    }
124
}
125