Update   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 32 6
1
<?php
2
3
namespace App\Http\Controllers\Users;
4
5
use App\Http\Requests\ValidateUserRequest;
6
use App\Models\User;
7
use Illuminate\Auth\Events\PasswordReset;
8
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
9
use Illuminate\Routing\Controller;
10
use Illuminate\Support\Collection;
11
use Illuminate\Support\Facades\Event;
12
13
class Update extends Controller
14
{
15
    use AuthorizesRequests;
16
17
    public function __invoke(ValidateUserRequest $request, User $user)
18
    {
19
        $role = \Auth::user()->role_id;
0 ignored issues
show
Bug introduced by
Accessing role_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
20
        // $user_id = \Auth::user()->id;
21
        if (in_array($role, [1, 2])) {
22
            $this->authorize('handle', $user);
23
24
            if ($request->filled('password')) {
25
                $this->authorize('change-password', $user);
26
                $user->password = bcrypt($request->get('password'));
27
            }
28
29
            $user->fill($request->validated());
30
31
            if ($user->isDirty('group_id')) {
32
                $this->authorize('change-group', $user);
33
            }
34
35
            if ($user->isDirty('role_id')) {
36
                $this->authorize('change-role', $user);
37
            }
38
39
            $user->save();
40
41
            if ((new Collection($user->getChanges()))->keys()->contains('password')) {
0 ignored issues
show
Bug introduced by
$user->getChanges() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

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

41
            if ((new Collection(/** @scrutinizer ignore-type */ $user->getChanges()))->keys()->contains('password')) {
Loading history...
42
                Event::dispatch(new PasswordReset($user));
43
            }
44
45
            return ['message' => __('The user was successfully updated')];
46
        }
47
48
        return ['error' => __('The user was unsuccessfully updated')];
49
    }
50
}
51