UpdateUser::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Admin\User;
6
7
use Illuminate\Contracts\View\Factory;
8
use Illuminate\Contracts\View\View;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
11
use Livewire\Attributes\On;
12
use Livewire\Component;
13
use Masmerise\Toaster\Toastable;
14
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist;
15
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileIsTooBig;
16
use Spatie\Permission\Models\Permission;
17
use Throwable;
18
use Xetaravel\Livewire\Forms\UserForm;
19
use Xetaravel\Models\Role;
20
use Xetaravel\Models\User;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Xetaravel\Livewire\Admin\User\User. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
22
class UpdateUser extends Component
23
{
24
    use AuthorizesRequests;
25
    use Toastable;
26
27
    /**
28
     * The form used to create/update a model.
29
     *
30
     * @var UserForm
31
     */
32
    public UserForm $form;
33
34
    /**
35
     * Used to show the update modal.
36
     *
37
     * @var bool
38
     */
39
    public bool $showModal = false;
40
41
    public function render(): View|Application|Factory|\Illuminate\View\View
42
    {
43
        // Select only the roles attached to this site or the roles without assigned site_id.
44
        $roles = Role::where('level', '<=', auth()->user()->level)
45
            ->select(['name', 'color'])
46
            ->orderBy('name')
47
            ->get()
48
            ->toArray();
49
50
        // Select all permissions except `bypass login` who is assigned to the `site_id` 0.
51
        $permissions = Permission::where('name', '<>', 'bypass login')
52
            ->select(['name', 'description'])
53
            ->orderBy('name')
54
            ->get()
55
            ->toArray();
56
57
        return view('livewire.admin.user.update-user', [
58
            'roles' => $roles,
59
            'permissions' => $permissions
60
        ]);
61
    }
62
63
    /**
64
     * When a user click on 'Edit' open the modal and set the content.
65
     *
66
     * @param int $id
67
     *
68
     * @return void
69
     */
70
    #[On('update-user')]
71
    public function updateUser(int $id): void
72
    {
73
        $user = User::withTrashed()
74
            ->with('account')
75
            ->find($id);
76
        $this->authorize('update', $user);
77
78
        $this->form->reset();
79
        $this->form->fill([
80
            'user' => $user,
81
            'username' => $user->username,
82
            'email' => $user->email,
83
            'first_name' => $user->account?->first_name,
84
            'last_name' => $user->account?->last_name,
85
            'facebook' => $user->account?->facebook,
86
            'twitter' => $user->account?->twitter,
87
            'biography' => $user->account?->biography,
88
            'signature' => $user->account?->signature,
89
            'roles' => $user->roles()->pluck('name')->toArray(),
90
            'permissions' => $user->permissions()->pluck('name')->toArray(),
91
            'can_bypass' => $user->hasPermissionTo('bypass login'),
92
        ]);
93
94
        $this->showModal = true;
95
    }
96
97
    /**
98
     * Update the user.
99
     *
100
     * @return void
101
     *
102
     * @throws Throwable
103
     */
104
    public function update(): void
105
    {
106
        $this->authorize('update', $this->form->user);
107
108
        $this->validate();
109
110
        $this->form->update();
111
112
        redirect()
113
            ->route('admin.user.index')
0 ignored issues
show
Bug introduced by
The method route() does not exist on Illuminate\Routing\Redirector. ( Ignorable by Annotation )

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

113
            ->/** @scrutinizer ignore-call */ route('admin.user.index')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
            ->success('This user has been updated successfully !');
115
    }
116
117
    /**
118
     * Restore a user.
119
     *
120
     * @return void
121
     */
122
    public function restore(): void
123
    {
124
        $this->authorize('restore', $this->form->user);
125
126
        $this->form->user->restore();
0 ignored issues
show
Bug introduced by
The method restore() does not exist on null. ( 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->form->user->/** @scrutinizer ignore-call */ 
127
                           restore();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
128
        redirect()
129
            ->route('admin.user.index')
130
            ->success('This user has been restored successfully !');
131
    }
132
133
    /**
134
     * Delete the avatar for the specified user.
135
     *
136
     * @param int $id The id of the user.
137
     *
138
     * @return void
139
     *
140
     * @throws FileDoesNotExist
141
     * @throws FileIsTooBig
142
     */
143
    public function deleteAvatar(int $id): void
144
    {
145
        $user = User::findOrFail($id);
146
147
        $user->clearMediaCollection('avatar');
148
        $user->addMedia(public_path('images/avatar.png'))
149
            ->preservingOriginal()
150
            ->setName(mb_substr(md5($user->username), 0, 10))
151
            ->setFileName(mb_substr(md5($user->username), 0, 10) . '.png')
152
            ->toMediaCollection('avatar');
153
154
        redirect()
155
            ->route('admin.user.index')
156
            ->success('The avatar has been deleted successfully !');
157
    }
158
}
159