Completed
Push — master ( 33156d...b9963c )
by Fèvre
21s queued 15s
created

UpdateBadge::updateBadge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 17
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Admin\Badge;
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 Xetaravel\Livewire\Forms\BadgeForm;
15
use Xetaravel\Models\Badge;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Xetaravel\Livewire\Admin\Badge\Badge. 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
17
class UpdateBadge extends Component
18
{
19
    use AuthorizesRequests;
20
    use Toastable;
21
22
    /**
23
     * The form used to create/update a model.
24
     *
25
     * @var BadgeForm
26
     */
27
    public BadgeForm $form;
28
29
    /**
30
     * Used to show the update modal.
31
     *
32
     * @var bool
33
     */
34
    public bool $showModal = false;
35
36
    /**
37
     * When a user click on 'Edit' open the modal and set the content.
38
     *
39
     * @param Badge $badge
40
     *
41
     * @return void
42
     */
43
    #[On('update-badge')]
44
    public function updateBadge(Badge $badge): void
45
    {
46
        $this->authorize('update', $badge);
47
48
        $this->form->reset();
49
        $this->form->fill([
50
            'badge' => $badge,
51
            'name' => $badge->name,
52
            'icon' => $badge->icon,
53
            'color' => $badge->color,
54
            'type' => $badge->type,
55
            'rule' => $badge->rule,
56
            'description' => $badge->description,
57
        ]);
58
59
        $this->showModal = true;
60
    }
61
62
    /**
63
     * Update the badge.
64
     *
65
     * @return void
66
     */
67
    public function update(): void
68
    {
69
        $this->authorize('update', $this->form->badge);
70
71
        $this->validate();
72
73
        $badge = $this->form->update();
74
75
        redirect()
76
            ->route('admin.badge.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.badge.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 badge $badge->name has been updated successfully !");
78
    }
79
80
    public function render(): View|Application|Factory|\Illuminate\View\View
81
    {
82
        return view('livewire.admin.badge.update-badge');
83
    }
84
}
85