Completed
Push — master ( cc63b0...03b333 )
by Abdelrahman
02:27
created

AccountController::currentUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
rs 10
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;
17
18
use Carbon\Carbon;
19
use Rinvex\Country\Models\Country;
20
use Illuminate\Support\MessageBag;
21
use Illuminate\Support\Facades\Auth;
22
use Illuminate\Support\ViewErrorBag;
23
use Illuminate\Support\Facades\Lang;
24
use Rinvex\Fort\Http\Requests\AccountUpdate;
25
use Rinvex\Fort\Http\Requests\TwoFactorTotp;
26
use Rinvex\Fort\Http\Requests\TwoFactorPhone;
27
use Rinvex\Fort\Services\TwoFactorTotpProvider;
28
use Rinvex\Fort\Contracts\UserRepositoryContract;
29
30
class AccountController extends FoundationController
31
{
32
    /**
33
     * Whitelisted methods.
34
     *
35
     * Array of whitelisted methods which do not need to be authorized.
36
     *
37
     * @var array
38
     */
39
    protected $authWhitelist = [];
40
41
    /**
42
     * The users repository instance.
43
     *
44
     * @var \Rinvex\Fort\Contracts\UserRepositoryContract
45
     */
46
    protected $users;
47
48
    /**
49
     * Create a new account controller instance.
50
     *
51
     * @param \Rinvex\Fort\Contracts\UserRepositoryContract $users
52
     *
53
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
54
     */
55
    public function __construct(UserRepositoryContract $users)
56
    {
57
        $this->users = $users;
58
59
        $this->middleware($this->getAuthMiddleware(), ['except' => $this->authWhitelist]);
60
    }
61
62
    /**
63
     * Show the account update form.
64
     *
65
     * @return \Illuminate\Http\Response
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 showAccountUpdate(Country $country)
68
    {
69
        $twoFactor = $this->currentUser()->getTwoFactor();
70
        $countries = $country->findAll()->pluck('name.common', 'iso_3166_1_alpha2');
71
72
        return view('rinvex.fort::account.page', compact('twoFactor', 'countries'));
73
    }
74
75
    /**
76
     * Process the account update form.
77
     *
78
     * @param \Rinvex\Fort\Http\Requests\AccountUpdate $request
79
     *
80
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
81
     */
82
    public function processAccountUpdate(AccountUpdate $request)
83
    {
84
        $currentUser = $this->currentUser();
85
        $data        = $request->except(['_token', 'id']);
86
        $twoFactor   = $currentUser->getTwoFactor();
87
88
        if (isset($data['password'])) {
89
            $data['password'] = bcrypt($data['password']);
90
        }
91
92
        $emailVerification = $data['email'] != $currentUser->email ? [
0 ignored issues
show
Bug introduced by
Accessing email on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
93
            'email_verified'    => false,
94
            'email_verified_at' => null,
95
        ] : [];
96
97
        $phoneVerification = $data['phone'] != $currentUser->phone ? [
0 ignored issues
show
Bug introduced by
Accessing phone on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
98
            'phone_verified'    => false,
99
            'phone_verified_at' => null,
100
        ] : [];
101
102
        $countryVerification = $data['country'] !== $currentUser->country;
0 ignored issues
show
Bug introduced by
Accessing country on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
103
104
        if ($phoneVerification || $countryVerification) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $phoneVerification of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
            array_set($twoFactor, 'phone.enabled', false);
106
        }
107
108
        $this->users->update($request->get('id'), $data + $emailVerification + $phoneVerification + $twoFactor);
109
110
        return intend([
111
            'back' => true,
112
            'with' => [
113
                          'rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.account.'.(! empty($emailVerification) ? 'reverify' : 'updated')),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 155 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...
114
                      ] + ($twoFactor !== $currentUser->getTwoFactor() ? ['rinvex.fort.alert.warning' => Lang::get('rinvex.fort::message.verification.twofactor.phone.auto_disabled')] : []),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 189 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...
115
        ]);
116
    }
117
118
    /**
119
     * Show the account sessions.
120
     *
121
     * @return \Illuminate\Http\Response
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...
122
     */
123
    public function showAccountSessions()
124
    {
125
        return view('rinvex.fort::account.sessions');
126
    }
127
128
    /**
129
     * Flush the given session.
130
     *
131
     * @param string $token
0 ignored issues
show
Documentation introduced by
Should the type for parameter $token not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
132
     *
133
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
134
     */
135
    public function processSessionFlush($token = null)
136
    {
137
        $status = '';
138
139
        if ($token) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $token of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
140
            app('rinvex.fort.persistence')->delete($token);
141
            $status = Lang::get('rinvex.fort::message.auth.session.flushed');
142
        } elseif (request()->get('confirm')) {
143
            app('rinvex.fort.persistence')->deleteByUser($this->currentUser()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
144
            $status = Lang::get('rinvex.fort::message.auth.session.flushedall');
145
        }
146
147
        return intend([
148
            'back' => true,
149
            'with' => ['rinvex.fort.alert.warning' => $status],
150
        ]);
151
    }
152
153
    /**
154
     * Show the Two-Factor TOTP enable form.
155
     *
156
     * @param \Rinvex\Fort\Http\Requests\TwoFactorTotp    $request
157
     * @param \Rinvex\Fort\Services\TwoFactorTotpProvider $totpProvider
158
     *
159
     * @return \Illuminate\Http\Response
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...
160
     */
161
    public function showTwoFactorTotpEnable(TwoFactorTotp $request, TwoFactorTotpProvider $totpProvider)
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...
162
    {
163
        $currentUser = $this->currentUser();
164
        $settings    = $currentUser->getTwoFactor();
165
166
        if (array_get($settings, 'totp.enabled') && ! session()->get('rinvex.fort.alert.success') && ! session()->get('errors')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
167
            $messageBag = new MessageBag([Lang::get('rinvex.fort::message.verification.twofactor.totp.already')]);
168
            $errors     = (new ViewErrorBag())->put('default', $messageBag);
169
        }
170
171
        if (! $secret = array_get($settings, 'totp.secret')) {
172
            array_set($settings, 'totp.enabled', false);
173
            array_set($settings, 'totp.secret', $secret = $totpProvider->generateSecretKey());
174
175
            $this->users->update($currentUser->id, [
0 ignored issues
show
Bug introduced by
Accessing id on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
176
                'two_factor' => $settings,
177
            ]);
178
        }
179
180
        $qrCode = $totpProvider->getQRCodeInline(config('rinvex.fort.twofactor.issuer'), $currentUser->email, $secret);
0 ignored issues
show
Bug introduced by
Accessing email on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
181
182
        return view('rinvex.fort::account.twofactor', compact('secret', 'qrCode', 'settings', 'errors'));
183
    }
184
185
    /**
186
     * Process the Two-Factor TOTP enable form.
187
     *
188
     * @param \Rinvex\Fort\Http\Requests\TwoFactorTotp    $request
189
     * @param \Rinvex\Fort\Services\TwoFactorTotpProvider $totpProvider
190
     *
191
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
192
     */
193
    public function processTwoFactorTotpEnable(TwoFactorTotp $request, TwoFactorTotpProvider $totpProvider)
194
    {
195
        $currentUser = $this->currentUser();
196
        $settings    = $currentUser->getTwoFactor();
197
        $secret      = array_get($settings, 'totp.secret');
198
        $backup      = array_get($settings, 'totp.backup');
199
        $backupAt    = array_get($settings, 'totp.backup_at');
200
201
        if ($totpProvider->verifyKey($secret, $request->get('token'))) {
202
            array_set($settings, 'totp.enabled', true);
203
            array_set($settings, 'totp.secret', $secret);
204
            array_set($settings, 'totp.backup', $backup ?: $this->generateTwoFactorTotpBackups());
205
            array_set($settings, 'totp.backup_at', $backupAt ?: (new Carbon())->toDateTimeString());
206
207
            // Update Two-Factor settings
208
            $this->users->update($currentUser->id, [
0 ignored issues
show
Bug introduced by
Accessing id on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
209
                'two_factor' => $settings,
210
            ]);
211
212
            return intend([
213
                'back' => true,
214
                'with' => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.totp.enabled')],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 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...
215
            ]);
216
        }
217
218
        return intend([
219
            'back'       => true,
220
            'withErrors' => ['token' => Lang::get('rinvex.fort::message.verification.twofactor.totp.invalid_token')],
221
        ]);
222
    }
223
224
    /**
225
     * Process the Two-Factor TOTP disable.
226
     *
227
     * @param \Rinvex\Fort\Http\Requests\TwoFactorTotp $request
228
     *
229
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
230
     */
231
    public function processTwoFactorTotpDisable(TwoFactorTotp $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...
232
    {
233
        $currentUser = $this->currentUser();
234
        $settings    = $currentUser->getTwoFactor();
235
236
        array_set($settings, 'totp', []);
237
238
        $this->users->update($currentUser->id, [
0 ignored issues
show
Bug introduced by
Accessing id on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
239
            'two_factor' => $settings,
240
        ]);
241
242
        return intend([
243
            'intended' => route('rinvex.fort.account.page'),
244
            'with'     => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.totp.disabled')],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
245
        ]);
246
    }
247
248
    /**
249
     * Process the Two-Factor Phone enable.
250
     *
251
     * @param \Rinvex\Fort\Http\Requests\TwoFactorPhone $request
252
     *
253
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
254
     */
255
    public function processTwoFactorPhoneEnable(TwoFactorPhone $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...
256
    {
257
        $currentUser = $this->currentUser();
258
259
        if (! $currentUser->phone || ! $currentUser->phone_verified) {
0 ignored issues
show
Bug introduced by
Accessing phone on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing phone_verified on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
260
            return intend([
261
                'intended'   => route('rinvex.fort.account.page'),
262
                'withErrors' => ['phone' => Lang::get('rinvex.fort::message.account.phone_required')],
263
            ]);
264
        }
265
266
        $settings = $this->currentUser()->getTwoFactor();
267
268
        array_set($settings, 'phone.enabled', true);
269
270
        $this->users->update($currentUser->id, [
0 ignored issues
show
Bug introduced by
Accessing id on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
271
            'two_factor' => $settings,
272
        ]);
273
274
        return intend([
275
            'intended' => route('rinvex.fort.account.page'),
276
            'with'     => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.phone.enabled')],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
277
        ]);
278
    }
279
280
    /**
281
     * Process the Two-Factor Phone disable.
282
     *
283
     * @param \Rinvex\Fort\Http\Requests\TwoFactorPhone $request
284
     *
285
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
286
     */
287
    public function processTwoFactorPhoneDisable(TwoFactorPhone $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...
288
    {
289
        $currentUser = $this->currentUser();
290
        $settings    = $currentUser->getTwoFactor();
291
292
        array_set($settings, 'phone.enabled', false);
293
294
        $this->users->update($currentUser->id, [
0 ignored issues
show
Bug introduced by
Accessing id on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
295
            'two_factor' => $settings,
296
        ]);
297
298
        return intend([
299
            'intended' => route('rinvex.fort.account.page'),
300
            'with'     => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.phone.disabled')],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 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...
301
        ]);
302
    }
303
304
    /**
305
     * Process the Two-Factor OTP backup.
306
     *
307
     * @param \Rinvex\Fort\Http\Requests\TwoFactorTotp $request
308
     *
309
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
310
     */
311
    public function processTwoFactorTotpBackup(TwoFactorTotp $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...
312
    {
313
        $currentUser = $this->currentUser();
314
        $settings    = $currentUser->getTwoFactor();
315
316
        array_set($settings, 'totp.backup', $this->generateTwoFactorTotpBackups());
317
        array_set($settings, 'totp.backup_at', (new Carbon())->toDateTimeString());
318
319
        $this->users->update($currentUser->id, [
0 ignored issues
show
Bug introduced by
Accessing id on the interface Rinvex\Fort\Contracts\AuthenticatableContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
320
            'two_factor' => $settings,
321
        ]);
322
323
        return intend([
324
            'back' => true,
325
            'with' => ['rinvex.fort.alert.success' => Lang::get('rinvex.fort::message.verification.twofactor.totp.rebackup')],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 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...
326
        ]);
327
    }
328
329
    /**
330
     * Generate Two-Factor OTP backup codes.
331
     *
332
     * @return array
333
     */
334
    protected function generateTwoFactorTotpBackups()
335
    {
336
        $backup = [];
337
338
        for ($x = 0; $x <= 9; $x++) {
339
            $backup[] = str_pad(random_int(0, 9999999999), 10, 0, STR_PAD_BOTH);
340
        }
341
342
        return $backup;
343
    }
344
345
    /**
346
     * Get current user.
347
     *
348
     * @return \Rinvex\Fort\Contracts\AuthenticatableContract
349
     */
350
    protected function currentUser()
351
    {
352
        return Auth::guard($this->getGuard())->user();
353
    }
354
}
355