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

UserSettingsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
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 Rinvex\Country\Loader;
19
use Illuminate\Support\Facades\Auth;
20
use Rinvex\Fort\Contracts\UserRepositoryContract;
21
use Rinvex\Fort\Http\Requests\Frontend\ProfileUpdate;
22
use Rinvex\Fort\Http\Controllers\AuthorizedController;
23
24
class UserSettingsController extends AuthorizedController
25
{
26
    /**
27
     * The user repository instance.
28
     *
29
     * @var \Rinvex\Fort\Contracts\UserRepositoryContract
30
     */
31
    protected $userRepository;
32
33
    /**
34
     * Create a new profile update controller instance.
35
     *
36
     * @param \Rinvex\Fort\Contracts\UserRepositoryContract $userRepository
37
     *
38
     * @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...
39
     */
40
    public function __construct(UserRepositoryContract $userRepository)
41
    {
42
        parent::__construct();
43
44
        $this->userRepository = $userRepository;
45
    }
46
47
    /**
48
     * Show the account update form.
49
     *
50
     * @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...
51
     */
52
    public function edit()
53
    {
54
        $countries = Loader::countries();
55
        $twoFactor = $this->currentUser()->getTwoFactor();
56
57
        return view('rinvex.fort::frontend.user.settings', compact('twoFactor', 'countries'));
58
    }
59
60
    /**
61
     * Process the account update form.
62
     *
63
     * @param \Rinvex\Fort\Http\Requests\Frontend\ProfileUpdate $request
64
     *
65
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
66
     */
67
    public function update(ProfileUpdate $request)
68
    {
69
        $currentUser = $this->currentUser();
70
        $data        = $request->except(['_token', 'id']);
71
        $twoFactor   = $currentUser->getTwoFactor();
72
73
        if (isset($data['password'])) {
74
            $data['password'] = bcrypt($data['password']);
75
        }
76
77
        $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...
78
            'email_verified'    => false,
79
            'email_verified_at' => null,
80
        ] : [];
81
82
        $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...
83
            'phone_verified'    => false,
84
            'phone_verified_at' => null,
85
        ] : [];
86
87
        $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...
88
89
        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...
90
            array_set($twoFactor, 'phone.enabled', false);
91
        }
92
93
        $this->userRepository->update($request->get('id'), $data + $emailVerification + $phoneVerification + $twoFactor);
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...
94
95
        return intend([
96
            'back' => true,
97
            'with' => [
98
                          'rinvex.fort.alert.success' => trans('rinvex.fort::frontend/messages.account.'.(! empty($emailVerification) ? 'reverify' : 'updated')),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 161 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...
99
                      ] + ($twoFactor !== $currentUser->getTwoFactor() ? ['rinvex.fort.alert.warning' => trans('rinvex.fort::frontend/messages.verification.twofactor.phone.auto_disabled')] : []),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 195 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
    }
102
103
    /**
104
     * Get current user.
105
     *
106
     * @return \Rinvex\Fort\Contracts\AuthenticatableContract
107
     */
108
    protected function currentUser()
109
    {
110
        return Auth::guard($this->getGuard())->user();
111
    }
112
}
113