Completed
Branch v3.0.0-beta (7109c1)
by Hilmi Erdem
136:43 queued 81:06
created

OtpController::store()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.9528
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
/*
4
 * @copyright 2018 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\TemporaryAccess\Http\Controllers;
9
10
use Illuminate\Http\Request;
11
use Illuminate\Contracts\View\View;
12
use Illuminate\Http\RedirectResponse;
13
use Erdemkeren\TemporaryAccess\TokenInterface;
14
use Illuminate\Contracts\Auth\Authenticatable;
15
use Illuminate\Support\Facades\Validator as ValidatorFacade;
16
use Illuminate\Contracts\Validation\Validator as ValidatorInterface;
17
use Erdemkeren\TemporaryAccess\TemporaryAccessFacade as TemporaryAccess;
18
19
/**
20
 * Class OtpController.
21
 */
22
class OtpController
23
{
24
    /**
25
     * * Show the form for the otp submission.
26
     *
27
     * @param Request $request
28
     *
29
     * @return RedirectResponse|View
30
     */
31
    public function create(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        if (!$this->otpHasBeenRequested()) {
34
            return redirect('/');
35
        }
36
37
        return view('otp.create');
38
    }
39
40
    /**
41
     * Store the otp in cookies and redirect user
42
     * to their original path.
43
     *
44
     * @param Request $request
45
     *
46
     * @return RedirectResponse
47
     */
48
    public function store(Request $request): RedirectResponse
49
    {
50
        if (!$this->otpHasBeenRequested()) {
51
            return redirect('/');
52
        }
53
54
        $validator = $this->getOtpSubmissionRequestValidator($request);
55
56
        if ($validator->fails()) {
57
            return redirect()->back()->withErrors($validator);
58
        }
59
60
        if (! $token = $this->retrieveOtpTokenByPlainText(
61
            $request->user(),
62
            $request->input('password')
1 ignored issue
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array or null; however, Erdemkeren\TemporaryAcce...veOtpTokenByPlainText() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
63
        )) {
64
            $validator->getMessageBag()->add(
65
                'password',
66
                'The password is not valid.'
67
            );
68
69
            return redirect()->back()->withErrors($validator);
70
        }
71
72
        if ($token->expired()) {
73
            $validator->getMessageBag()->add(
74
                'password',
75
                'The password is expired.'
76
            );
77
78
            redirect()->back()->withErrors($validator);
79
        }
80
81
        session()->forget('otp_requested');
82
83
        return redirect()
84
            ->to(session()->pull('otp_redirect_url'))
85
            ->withCookie(
86
                cookie()->make('otp_token', (string) $token, $token->expiryTime() / 60)
87
            );
88
    }
89
90
    /**
91
     * Validate the given otp submission request.
92
     *
93
     * @param Request $request
94
     *
95
     * @return ValidatorInterface
96
     */
97
    private function getOtpSubmissionRequestValidator(Request $request): ValidatorInterface
98
    {
99
        return ValidatorFacade::make($request->all(), [
100
            'password' => 'required|string',
101
        ]);
102
    }
103
104
    /**
105
     * Retrieve a token by the given user and password.
106
     *
107
     * @param Authenticatable $user
108
     * @param string          $password
109
     *
110
     * @return mixed
111
     */
112
    private function retrieveOtpTokenByPlainText(Authenticatable $user, string $password): ?TokenInterface
113
    {
114
        return TemporaryAccess::retrieveByPlainText($user, $password);
115
    }
116
117
    /**
118
     * Determine if an otp requested or not.
119
     *
120
     * @return mixed
121
     */
122
    private function otpHasBeenRequested()
123
    {
124
        return session('otp_requested', false);
125
    }
126
}
127