Completed
Push — master ( a97a29...ec6402 )
by Jonathan
09:06
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
11
class AccountController extends Controller
12
{
13
    /**
14
     * @inheritDoc
15
     */
16
    public function __construct()
17
    {
18
        parent::__construct();
19
20
        $this->module = ucmodule('user');
21
    }
22
23
    /**
24
     * Display user account page
25
     *
26
     * @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...
27
     */
28
    public function index(?Domain $domain, Module $module)
29
    {
30
        $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

30
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
31
32
        $this->viewName = 'account.main';
33
34
        $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

34
        $user = /** @scrutinizer ignore-call */ auth()->user();
Loading history...
35
36
        return $this->autoView(compact('user'));
37
    }
38
39
    /**
40
     * Update user profile
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function updateProfile(?Domain $domain, Module $module)
45
    {
46
        $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

46
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
47
48
        $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

48
        $user = /** @scrutinizer ignore-call */ auth()->user();
Loading history...
49
50
        $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

50
        $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...
51
            return isset($item->rules) ? explode('|', str_replace('%id%', $user->id, $item->rules)) : '';
52
        });
53
54
        $validator = Validator::make(request()->all(), [
55
            'username' => $rules['username'],
56
            'name' => $rules['name'],
57
            'email' => $rules['email'],
58
        ]);
59
60
        if ($validator->fails()) {
61
            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

61
            ucnotify(/** @scrutinizer ignore-type */ uctrans('notification.form.not_valid', $module), 'error');
Loading history...
62
63
            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

63
            return /** @scrutinizer ignore-call */ redirect(ucroute('uccello.user.account', $domain))
Loading history...
64
                        ->withErrors($validator)
65
                        ->withInput()
66
                        ->with('form_name', 'profile');
67
        }
68
69
        $user->username = request('username');
70
        $user->name = request('name');
71
        $user->email = request('email');
72
        $user->save();
73
74
        ucnotify(uctrans('success.profile_updated', ucmodule('user')), 'success');
75
76
        return redirect(ucroute('uccello.user.account', $domain));
77
    }
78
79
    /**
80
     * Update user avatar
81
     *
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function updateAvatar(?Domain $domain, Module $module)
85
    {
86
        $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

86
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
87
88
        $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

88
        $user = /** @scrutinizer ignore-call */ auth()->user();
Loading history...
89
90
        $avatarType = request('avatar_type');
91
        $avatar = [ 'type' => $avatarType ];
92
93
        if (request('avatar')) {
94
            $image = str_replace('data:image/png;base64,', '', request('avatar'));
95
            $image = str_replace(' ', '+', $image);
96
            $imageName = 'user-' . $user->id . '.png';
97
            $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

97
            $path = /** @scrutinizer ignore-call */ storage_path('app/public/avatar/');
Loading history...
98
            $filepath = $path . $imageName;
99
100
            if(!\File::isDirectory($path)){
101
                \File::makeDirectory($path, 0777, true, true);
102
            }
103
104
            \File::put($filepath, base64_decode($image));
105
106
            $avatar[ 'path' ] = "/storage/avatar/$imageName";
107
        }
108
109
        $user->avatar = $avatar;
110
        $user->save();
111
112
        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

112
        ucnotify(/** @scrutinizer ignore-type */ uctrans('success.avatar_updated', ucmodule('user')), 'success');
Loading history...
113
114
        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

114
        return /** @scrutinizer ignore-call */ redirect(ucroute('uccello.user.account', $domain))
Loading history...
115
            ->with('form_name', 'avatar');
116
    }
117
118
    /**
119
     * Update user password
120
     *
121
     * @return \Illuminate\Http\Response
122
     */
123
    public function updatePassword(?Domain $domain, Module $module)
124
    {
125
        $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

125
        $this->preProcess($domain, $module, /** @scrutinizer ignore-call */ request());
Loading history...
126
127
        $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

127
        $user = /** @scrutinizer ignore-call */ auth()->user();
Loading history...
128
129
        $field = Field::where('module_id', ucmodule('user')->id)->where('name', 'password')->first();
130
        $password_rules = isset($field->data->rules) ? explode('|', $field->data->rules) : '';
131
        $password_rules[] = 'confirmed';
132
133
        $validator = Validator::make(request()->all(), [
134
            'current_password' => function($attribute, $value, $fail) use ($user) {
135
                if (!\Hash::check($value, $user->password)) {
136
                    $fail(uctrans('error.current_password', ucmodule('user')));
137
                }
138
            },
139
            'password' => $password_rules,
140
        ]);
141
142
        if ($validator->fails()) {
143
            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

143
            ucnotify(/** @scrutinizer ignore-type */ uctrans('notification.form.not_valid', $module), 'error');
Loading history...
144
145
            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

145
            return /** @scrutinizer ignore-call */ redirect(ucroute('uccello.user.account', $domain))
Loading history...
146
                        ->withErrors($validator)
147
                        ->withInput()
148
                        ->with('form_name', 'password');
149
        }
150
151
        $user->password = \Hash::make(request('password'));
152
        $user->save();
153
154
        ucnotify(uctrans('success.password_updated', ucmodule('user')), 'success');
155
156
        return redirect(ucroute('uccello.user.account', $domain));
157
    }
158
}