Completed
Push — master ( cdb114...273f6a )
by Abdelrahman
02:18
created

processPhoneVerification()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 22
nc 4
nop 1
dl 0
loc 34
rs 8.5806
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Http\Controllers\Frontend;
17
18
use Illuminate\Support\Facades\Auth;
19
use Rinvex\Fort\Guards\SessionGuard;
20
use Rinvex\Fort\Http\Controllers\AbstractController;
21
use Rinvex\Fort\Http\Requests\Frontend\PhoneVerification;
22
use Rinvex\Fort\Http\Requests\Frontend\PhoneVerificationRequest;
23
24
class PhoneVerificationController extends AbstractController
25
{
26
    /**
27
     * Show the phone verification form.
28
     *
29
     * @param \Rinvex\Fort\Http\Requests\Frontend\PhoneVerificationRequest $request
30
     *
31
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
     */
33
    public function request(PhoneVerificationRequest $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...
34
    {
35
        // If Two-Factor authentication failed, remember Two-Factor persistence
36
        Auth::guard($this->getGuard())->rememberTwoFactor();
37
38
        return view('rinvex.fort::frontend.verification.phone.request');
39
    }
40
41
    /**
42
     * Process the phone verification request form.
43
     *
44
     * @param \Rinvex\Fort\Http\Requests\Frontend\PhoneVerificationRequest $request
45
     *
46
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
47
     */
48
    public function send(PhoneVerificationRequest $request)
49
    {
50
        $status = app('rinvex.fort.verifier')
51
            ->broker($this->getBroker())
52
            ->sendPhoneVerification(Auth::guard($this->getGuard())->user(), $request->get('method')) ? 'sent' : 'failed';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
53
54
        return intend([
55
            'intended' => route('rinvex.fort.frontend.verification.phone.verify'),
56
            'with'     => ['rinvex.fort.alert.success' => trans('rinvex.fort::frontend/messages.verification.phone.'.$status)],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
57
        ]);
58
    }
59
60
    /**
61
     * Show the phone verification form.
62
     *
63
     * @param \Rinvex\Fort\Http\Requests\Frontend\PhoneVerification $request
64
     *
65
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
66
     */
67
    public function verify(PhoneVerification $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...
68
    {
69
        // If Two-Factor authentication failed, remember Two-Factor persistence
70
        Auth::guard($this->getGuard())->rememberTwoFactor();
71
72
        $methods = session('rinvex.fort.twofactor.methods');
73
74
        return view('rinvex.fort::frontend.verification.phone.token', compact('methods'));
75
    }
76
77
    /**
78
     * Process the phone verification form.
79
     *
80
     * @param \Rinvex\Fort\Http\Requests\Frontend\PhoneVerification $request
81
     *
82
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Illuminate\Http\Redirec...inate\Http\JsonResponse.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
83
     */
84
    public function process(PhoneVerification $request)
85
    {
86
        $guard  = $this->getGuard();
87
        $token  = $request->get('token');
88
        $user   = Auth::guard($guard)->user();
89
        $result = Auth::guard($guard)->attemptTwoFactor($user, $token);
90
91
        switch ($result) {
92
            case SessionGuard::AUTH_PHONE_VERIFIED:
93
                return intend([
94
                    'intended' => route('rinvex.fort.frontend.user.settings'),
95
                    'with'     => ['rinvex.fort.alert.success' => trans($result)],
96
                ]);
97
98
            case SessionGuard::AUTH_LOGIN:
99
                Auth::guard($guard)->login($user, session('rinvex.fort.twofactor.remember'), session('rinvex.fort.twofactor.persistence'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
100
101
                return intend([
102
                    'intended' => url('/'),
103
                    'with'     => ['rinvex.fort.alert.success' => trans($result)],
104
                ]);
105
106
            case SessionGuard::AUTH_TWOFACTOR_FAILED:
107
            default:
108
                // If Two-Factor authentication failed, remember Two-Factor persistence
109
                Auth::guard($guard)->rememberTwoFactor();
110
111
                return intend([
112
                    'back'       => true,
113
                    'withInput'  => $request->only(['token']),
114
                    'withErrors' => ['token' => trans($result)],
115
                ]);
116
        }
117
    }
118
}
119