Completed
Push — master ( 03b333...573f2e )
by Abdelrahman
02:45
created

ProfileUpdateController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 11
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A showProfileUpdate() 0 7 1
C processProfileUpdate() 0 35 8
A currentUser() 0 4 1
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\Models\Country;
19
use Illuminate\Support\Facades\Auth;
20
use Illuminate\Support\Facades\Lang;
21
use Rinvex\Fort\Http\Requests\ProfileUpdate;
22
use Rinvex\Fort\Contracts\UserRepositoryContract;
23
use Rinvex\Fort\Http\Controllers\AbstractController;
24
25
class ProfileUpdateController extends AbstractController
26
{
27
    /**
28
     * The users repository instance.
29
     *
30
     * @var \Rinvex\Fort\Contracts\UserRepositoryContract
31
     */
32
    protected $users;
33
34
    /**
35
     * Create a new account controller instance.
36
     *
37
     * @param \Rinvex\Fort\Contracts\UserRepositoryContract $users
38
     *
39
     * @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...
40
     */
41
    public function __construct(UserRepositoryContract $users)
42
    {
43
        $this->users = $users;
44
45
        $this->middleware($this->getAuthMiddleware(), ['except' => $this->middlewareWhitelist]);
46
    }
47
48
    /**
49
     * Show the account update form.
50
     *
51
     * @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...
52
     */
53
    public function showProfileUpdate(Country $country)
54
    {
55
        $twoFactor = $this->currentUser()->getTwoFactor();
56
        $countries = $country->findAll()->pluck('name.common', 'iso_3166_1_alpha2');
57
58
        return view('rinvex.fort::profile.page', compact('twoFactor', 'countries'));
59
    }
60
61
    /**
62
     * Process the account update form.
63
     *
64
     * @param \Rinvex\Fort\Http\Requests\ProfileUpdate $request
65
     *
66
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
67
     */
68
    public function processProfileUpdate(ProfileUpdate $request)
69
    {
70
        $currentUser = $this->currentUser();
71
        $data        = $request->except(['_token', 'id']);
72
        $twoFactor   = $currentUser->getTwoFactor();
73
74
        if (isset($data['password'])) {
75
            $data['password'] = bcrypt($data['password']);
76
        }
77
78
        $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...
79
            'email_verified'    => false,
80
            'email_verified_at' => null,
81
        ] : [];
82
83
        $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...
84
            'phone_verified'    => false,
85
            'phone_verified_at' => null,
86
        ] : [];
87
88
        $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...
89
90
        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...
91
            array_set($twoFactor, 'phone.enabled', false);
92
        }
93
94
        $this->users->update($request->get('id'), $data + $emailVerification + $phoneVerification + $twoFactor);
95
96
        return intend([
97
            'back' => true,
98
            'with' => [
99
                          '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...
100
                      ] + ($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...
101
        ]);
102
    }
103
104
    /**
105
     * Get current user.
106
     *
107
     * @return \Rinvex\Fort\Contracts\AuthenticatableContract
108
     */
109
    protected function currentUser()
110
    {
111
        return Auth::guard($this->getGuard())->user();
112
    }
113
}
114