Completed
Push — master ( 8927df...f70445 )
by Abdelrahman
02:05
created

PhoneVerificationController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 100
rs 10
c 3
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 9 1
A request() 0 7 1
A send() 0 10 1
B process() 0 40 5
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 Carbon\Carbon;
19
use Illuminate\Support\Facades\Auth;
20
use Rinvex\Fort\Guards\SessionGuard;
21
use Rinvex\Fort\Http\Controllers\AbstractController;
22
use Rinvex\Fort\Http\Requests\Frontend\PhoneVerificationRequest;
23
use Rinvex\Fort\Http\Requests\Frontend\PhoneVerificationSendRequest;
24
25
class PhoneVerificationController extends AbstractController
26
{
27
    /**
28
     * Show the phone verification form.
29
     *
30
     * @param \Rinvex\Fort\Http\Requests\Frontend\PhoneVerificationSendRequest $request
31
     *
32
     * @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...
33
     */
34
    public function request(PhoneVerificationSendRequest $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...
35
    {
36
        // If Two-Factor authentication failed, remember Two-Factor persistence
37
        Auth::guard($this->getGuard())->rememberTwoFactor();
38
39
        return view('rinvex/fort::frontend/verification.phone.request');
40
    }
41
42
    /**
43
     * Process the phone verification request form.
44
     *
45
     * @param \Rinvex\Fort\Http\Requests\Frontend\PhoneVerificationSendRequest $request
46
     *
47
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
48
     */
49
    public function send(PhoneVerificationSendRequest $request)
50
    {
51
        // Send phone verification notification
52
        $request->user($this->getGuard())->sendPhoneVerificationNotification(false, $request->get('method'));
53
54
        return intend([
55
            'route' => 'rinvex.fort.frontend.verification.phone.verify',
56
            'with'  => ['rinvex.fort.alert.success' => trans('rinvex/fort::frontend/messages.verification.phone.sent')],
57
        ]);
58
    }
59
60
    /**
61
     * Show the phone verification form.
62
     *
63
     * @param \Rinvex\Fort\Http\Requests\Frontend\PhoneVerificationRequest $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(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...
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\PhoneVerificationRequest $request
81
     *
82
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
83
     */
84
    public function process(PhoneVerificationRequest $request)
85
    {
86
        $guard  = $this->getGuard();
87
        $token  = $request->get('token');
88
        $user   = session('rinvex.fort.twofactor.user') ?: $request->user($this->getGuard());
89
        $result = Auth::guard($guard)->attemptTwoFactor($user, $token);
90
91
        switch ($result) {
92
            case SessionGuard::AUTH_PHONE_VERIFIED:
93
                // Update user account
94
                app('rinvex.fort.user')->update($user, [
95
                    'phone_verified'    => true,
96
                    'phone_verified_at' => new Carbon(),
97
                ]);
98
99
                return intend([
100
                    'route' => 'rinvex.fort.frontend.user.settings',
101
                    'with'  => ['rinvex.fort.alert.success' => trans($result)],
102
                ]);
103
104
            case SessionGuard::AUTH_LOGIN:
105
                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...
106
107
                return intend([
108
                    'intended' => url('/'),
109
                    'with'     => ['rinvex.fort.alert.success' => trans($result)],
110
                ]);
111
112
            case SessionGuard::AUTH_TWOFACTOR_FAILED:
113
            default:
114
                // If Two-Factor authentication failed, remember Two-Factor persistence
115
                Auth::guard($guard)->rememberTwoFactor();
116
117
                return intend([
118
                    'back'       => true,
119
                    'withInput'  => $request->only(['token']),
120
                    'withErrors' => ['token' => trans($result)],
121
                ]);
122
        }
123
    }
124
}
125