AccountController::updateAvatar()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 3
nop 2
dl 0
loc 32
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\User;
4
5
use Uccello\Core\Http\Controllers\Core\Controller;
6
use Uccello\Core\Models\Domain;
7
use Uccello\Core\Models\Module;
8
use Uccello\Core\Models\Field;
9
use Illuminate\Support\Facades\Validator;
10
use Uccello\Core\Models\UserSettings;
11
12
class AccountController extends Controller
13
{
14
    /**
15
     * @inheritDoc
16
     */
17
    public function __construct()
18
    {
19
        parent::__construct();
20
21
        $this->module = ucmodule('user');
22
    }
23
24
    /**
25
     * Display user account page
26
     *
27
     * @return \Illuminate\Http\Response
28
     */
29
    public function index(?Domain $domain, Module $module)
30
    {
31
        $this->preProcess($domain, $module, request());
32
33
        $this->viewName = 'account.main';
34
35
        $user = auth()->user();
36
37
        return $this->autoView(compact('user'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->autoView(compact('user')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
38
    }
39
40
    /**
41
     * Update user profile
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45
    public function updateProfile(?Domain $domain, Module $module)
46
    {
47
        $this->preProcess($domain, $module, request());
48
49
        $user = auth()->user();
50
51
        $rules = Field::where('module_id', ucmodule('user')->id)->pluck('data', 'name')->map(function($item, $key) use($user) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
        $rules = Field::where('module_id', ucmodule('user')->id)->pluck('data', 'name')->map(function($item, /** @scrutinizer ignore-unused */ $key) use($user) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
            return isset($item->rules) ? explode('|', str_replace('%id%', $user->id, $item->rules)) : '';
53
        });
54
55
        $validator = Validator::make(request()->all(), [
56
            'username' => $rules['username'],
57
            'name' => $rules['name'],
58
            'email' => $rules['email'],
59
        ]);
60
61
        if ($validator->fails()) {
62
            ucnotify(uctrans('notification.form.not_valid', $module), 'error');
63
64
            return redirect(ucroute('uccello.user.account', $domain))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...'form_name', 'profile') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
65
                        ->withErrors($validator)
66
                        ->withInput()
67
                        ->with('form_name', 'profile');
68
        }
69
70
        $user->username = request('username');
71
        $user->name = request('name');
72
        $user->email = request('email');
73
        $user->save();
74
75
        ucnotify(uctrans('success.profile_updated', ucmodule('user')), 'success');
76
77
        return redirect(ucroute('uccello.user.account', $domain));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...ser.account', $domain)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
78
    }
79
80
    /**
81
     * Update user avatar
82
     *
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function updateAvatar(?Domain $domain, Module $module)
86
    {
87
        $this->preProcess($domain, $module, request());
88
89
        $user = auth()->user();
90
91
        $avatarType = request('avatar_type');
92
        $avatar = [ 'type' => $avatarType ];
93
94
        if (request('avatar')) {
95
            $image = str_replace('data:image/png;base64,', '', request('avatar'));
96
            $image = str_replace(' ', '+', $image);
97
            $imageName = 'user-' . $user->id . '.png';
98
            $path = storage_path('app/public/avatar/');
99
            $filepath = $path . $imageName;
100
101
            if(!\File::isDirectory($path)){
102
                \File::makeDirectory($path, 0777, true, true);
103
            }
104
105
            \File::put($filepath, base64_decode($image));
106
107
            $avatar[ 'path' ] = "/storage/avatar/$imageName";
108
        }
109
110
        $user->avatar = $avatar;
111
        $user->save();
112
113
        ucnotify(uctrans('success.avatar_updated', ucmodule('user')), 'success');
114
115
        return redirect(ucroute('uccello.user.account', $domain))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...('form_name', 'avatar') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
116
            ->with('form_name', 'avatar');
117
    }
118
119
    /**
120
     * Update user password
121
     *
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function updatePassword(?Domain $domain, Module $module)
125
    {
126
        $this->preProcess($domain, $module, request());
127
128
        $user = auth()->user();
129
130
        $field = Field::where('module_id', ucmodule('user')->id)->where('name', 'password')->first();
131
        $password_rules = isset($field->data->rules) ? explode('|', $field->data->rules) : '';
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Field. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
132
        $password_rules[] = 'confirmed';
133
134
        $validator = Validator::make(request()->all(), [
135
            'current_password' => function($attribute, $value, $fail) use ($user) {
136
                if (!\Hash::check($value, $user->password)) {
137
                    $fail(uctrans('error.current_password', ucmodule('user')));
138
                }
139
            },
140
            'password' => $password_rules,
141
        ]);
142
143
        if ($validator->fails()) {
144
            ucnotify(uctrans('notification.form.not_valid', $module), 'error');
145
146
            return redirect(ucroute('uccello.user.account', $domain))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...form_name', 'password') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
147
                        ->withErrors($validator)
148
                        ->withInput()
149
                        ->with('form_name', 'password');
150
        }
151
152
        $user->password = \Hash::make(request('password'));
0 ignored issues
show
Bug introduced by
It seems like request('password') can also be of type array; however, parameter $value of Illuminate\Support\Facades\Hash::make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
        $user->password = \Hash::make(/** @scrutinizer ignore-type */ request('password'));
Loading history...
153
        $user->save();
154
155
        ucnotify(uctrans('success.password_updated', ucmodule('user')), 'success');
156
157
        return redirect(ucroute('uccello.user.account', $domain));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...ser.account', $domain)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
158
    }
159
160
    /**
161
     * Update user settings
162
     *
163
     * @return \Illuminate\Http\Response
164
     */
165
    public function updateSettings(?Domain $domain, Module $module)
166
    {
167
        $this->preProcess($domain, $module, request());
168
169
        $userSettings = UserSettings::firstOrNew([
170
            'user_id' => auth()->id()
171
        ]);
172
173
        $data = $userSettings->data ?? new \stdClass;
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\UserSettings. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
174
175
        foreach ((array) request('settings') as $key => $value) {
176
            if ($value === 'true') {
177
                $value = true;
178
            } elseif ($value === 'false') {
179
                $value = false;
180
            }
181
182
            $data->{$key} = $value;
183
        }
184
185
        $userSettings->data = $data;
186
        $userSettings->save();
187
188
        return response()->json($userSettings->data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($userSettings->data) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
189
    }
190
}