Passed
Push — 5.0.0 ( 8a9c00...cfa35f )
by Fèvre
06:04
created

UpdateRole::updateRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 16
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Admin\Permission;
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\Permission\Models\Role;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Xetaravel\Livewire\Admin\Permission\Role. 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...
15
use Spatie\Permission\Models\Permission;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Xetaravel\Livewire\Admin\Permission\Permission. 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...
16
use Xetaravel\Livewire\Forms\RoleForm;
17
18
class UpdateRole extends Component
19
{
20
    use AuthorizesRequests;
21
    use Toastable;
22
23
    /**
24
     * The form used to create/update a model.
25
     *
26
     * @var RoleForm
27
     */
28
    public RoleForm $form;
29
30
    /**
31
     * Used to show the update modal.
32
     *
33
     * @var bool
34
     */
35
    public bool $showModal = false;
36
37
    /**
38
     * When a user click on 'Edit' open the modal and set the content.
39
     *
40
     * @param Role $role
41
     *
42
     * @return void
43
     */
44
    #[On('update-role')]
45
    public function updateRole(Role $role): void
46
    {
47
        $this->authorize('update', $role);
48
49
        $this->form->reset();
50
        $this->form->fill([
51
            'role' => $role,
52
            'name' => $role->name,
53
            'color' => $role->color,
0 ignored issues
show
Bug introduced by
The property color does not seem to exist on Spatie\Permission\Models\Role.
Loading history...
54
            'level' => $role->level,
0 ignored issues
show
Bug introduced by
The property level does not seem to exist on Spatie\Permission\Models\Role.
Loading history...
55
            'description' => $role->description,
0 ignored issues
show
Bug introduced by
The property description does not seem to exist on Spatie\Permission\Models\Role.
Loading history...
56
            'permissions' => $role->permissions()->pluck('name')->toArray(),
57
        ]);
58
59
        $this->showModal = true;
60
    }
61
62
    /**
63
     * Update the role.
64
     *
65
     * @return void
66
     */
67
    public function update(): void
68
    {
69
        $this->authorize('update', $this->form->role);
70
71
        $this->validate();
72
73
        $role = $this->form->update();
74
75
        redirect()
76
            ->route('admin.role.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

76
            ->/** @scrutinizer ignore-call */ route('admin.role.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...
77
            ->success("The role $role->name has been updated successfully !");
78
    }
79
80
    public function render(): View|Application|Factory|\Illuminate\View\View
81
    {
82
        // Select all permissions except `bypass login` which is handled by a checkbox.
83
        $permissions = Permission::where('name', '<>', 'bypass login')
84
            ->select(['name', 'description'])
85
            ->orderBy('name')
86
            ->get()
87
            ->toArray();
88
89
        return view('livewire.admin.permission.update-role', [
90
            'permissions' => $permissions,
91
        ]);
92
    }
93
}
94