Passed
Push — master ( 6bbf15...aead9d )
by Jonathan
19:29
created

AccountController::updateAvatar()   A

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;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Validator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
     */
29
    public function index(?Domain $domain, Module $module)
30
    {
31
        $this->preProcess($domain, $module, request());
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

31
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
32
33
        $this->viewName = 'account.main';
34
35
        $user = auth()->user();
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

35
        $user = /** @scrutinizer ignore-call */ auth()->user();
Loading history...
36
37
        return $this->autoView(compact('user'));
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());
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

47
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
48
49
        $user = auth()->user();
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

49
        $user = /** @scrutinizer ignore-call */ auth()->user();
Loading history...
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');
0 ignored issues
show
Bug introduced by
It seems like uctrans('notification.form.not_valid', $module) can also be of type array; however, parameter $message of ucnotify() 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

62
            ucnotify(/** @scrutinizer ignore-type */ uctrans('notification.form.not_valid', $module), 'error');
Loading history...
63
64
            return redirect(ucroute('uccello.user.account', $domain))
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

64
            return /** @scrutinizer ignore-call */ redirect(ucroute('uccello.user.account', $domain))
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));
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());
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

87
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
88
89
        $user = auth()->user();
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

89
        $user = /** @scrutinizer ignore-call */ auth()->user();
Loading history...
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/');
0 ignored issues
show
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

98
            $path = /** @scrutinizer ignore-call */ storage_path('app/public/avatar/');
Loading history...
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');
0 ignored issues
show
Bug introduced by
It seems like uctrans('success.avatar_...ted', ucmodule('user')) can also be of type array; however, parameter $message of ucnotify() 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

113
        ucnotify(/** @scrutinizer ignore-type */ uctrans('success.avatar_updated', ucmodule('user')), 'success');
Loading history...
114
115
        return redirect(ucroute('uccello.user.account', $domain))
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

115
        return /** @scrutinizer ignore-call */ redirect(ucroute('uccello.user.account', $domain))
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());
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

126
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
127
128
        $user = auth()->user();
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

128
        $user = /** @scrutinizer ignore-call */ auth()->user();
Loading history...
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) : '';
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');
0 ignored issues
show
Bug introduced by
It seems like uctrans('notification.form.not_valid', $module) can also be of type array; however, parameter $message of ucnotify() 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

144
            ucnotify(/** @scrutinizer ignore-type */ uctrans('notification.form.not_valid', $module), 'error');
Loading history...
145
146
            return redirect(ucroute('uccello.user.account', $domain))
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

146
            return /** @scrutinizer ignore-call */ redirect(ucroute('uccello.user.account', $domain))
Loading history...
147
                        ->withErrors($validator)
148
                        ->withInput()
149
                        ->with('form_name', 'password');
150
        }
151
152
        $user->password = \Hash::make(request('password'));
153
        $user->save();
154
155
        ucnotify(uctrans('success.password_updated', ucmodule('user')), 'success');
156
157
        return redirect(ucroute('uccello.user.account', $domain));
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());
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

167
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
168
169
        $userSettings = UserSettings::firstOrNew([
170
            'user_id' => auth()->id()
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

170
            'user_id' => /** @scrutinizer ignore-call */ auth()->id()
Loading history...
171
        ]);
172
173
        $data = $userSettings->data ?? new \stdClass;
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 introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

188
        return /** @scrutinizer ignore-call */ response()->json($userSettings->data);
Loading history...
189
    }
190
}