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

UserSettingsController::update()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 8
nop 1
dl 0
loc 31
rs 6.7272
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 Illuminate\Http\Request;
19
use Rinvex\Fort\Contracts\UserRepositoryContract;
20
use Rinvex\Fort\Http\Controllers\AuthenticatedController;
21
use Rinvex\Fort\Http\Requests\Frontend\UserSettingsUpdateRequest;
22
23
class UserSettingsController extends AuthenticatedController
24
{
25
    /**
26
     * The user repository instance.
27
     *
28
     * @var \Rinvex\Fort\Contracts\UserRepositoryContract
29
     */
30
    protected $userRepository;
31
32
    /**
33
     * Create a new profile update controller instance.
34
     *
35
     * @param \Rinvex\Fort\Contracts\UserRepositoryContract $userRepository
36
     */
37
    public function __construct(UserRepositoryContract $userRepository)
38
    {
39
        parent::__construct();
40
41
        $this->userRepository = $userRepository;
42
    }
43
44
    /**
45
     * Show the account update form.
46
     *
47
     * @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...
48
     */
49
    public function edit(Request $request)
50
    {
51
        $countries = array_map(function ($country) {
52
            return $country->getName();
53
        }, countries());
54
        $twoFactor = $request->user($this->getGuard())->getTwoFactor();
55
56
        return view('rinvex/fort::frontend/user.settings', compact('twoFactor', 'countries'));
57
    }
58
59
    /**
60
     * Process the account update form.
61
     *
62
     * @param \Rinvex\Fort\Http\Requests\Frontend\UserSettingsUpdateRequest $request
63
     *
64
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
65
     */
66
    public function update(UserSettingsUpdateRequest $request)
67
    {
68
        $currentUser = $request->user($this->getGuard());
69
        $data        = $request->except(['_token', 'id']);
70
        $twoFactor   = $currentUser->getTwoFactor();
71
72
        $emailVerification = $data['email'] != $currentUser->email ? [
73
            'email_verified'    => false,
74
            'email_verified_at' => null,
75
        ] : [];
76
77
        $phoneVerification = $data['phone'] != $currentUser->phone ? [
78
            'phone_verified'    => false,
79
            'phone_verified_at' => null,
80
        ] : [];
81
82
        $countryVerification = $data['country'] !== $currentUser->country;
83
84
        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...
85
            array_set($twoFactor, 'phone.enabled', false);
86
        }
87
88
        $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...
89
90
        return intend([
91
            'back' => true,
92
            'with' => [
93
                          '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...
94
                      ] + ($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...
95
        ]);
96
    }
97
}
98