Passed
Push — 5.0.0 ( 9d1037...fbd7bb )
by Fèvre
05:16
created

UpdateConversation::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 22
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
namespace Xetaravel\Livewire\Discuss;
4
5
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6
use Illuminate\Validation\Rule;
7
use Livewire\Attributes\On;
8
use Livewire\Component;
9
use Masmerise\Toaster\Toastable;
10
use Xetaravel\Livewire\Forms\DiscussConversationForm;
11
use Xetaravel\Models\DiscussCategory;
12
use Xetaravel\Models\DiscussConversation;
13
14
class UpdateConversation extends Component
15
{
16
    use AuthorizesRequests;
17
    use Toastable;
18
19
    /**
20
     * The form used to create/update a model.
21
     *
22
     * @var DiscussConversationForm
23
     */
24
    public DiscussConversationForm $form;
25
26
    /**
27
     * Used to show the Edit/Create modal.
28
     *
29
     * @var bool
30
     */
31
    public bool $showModal = false;
32
33
    public function mount(DiscussConversation $discussConversation): void
34
    {
35
        $this->form->discussConversation = $discussConversation;
36
    }
37
38
    public function render()
39
    {
40
        return view('livewire.discuss.update-conversation');
41
    }
42
43
    /**
44
     * When a user click on 'Edit' open the modal.
45
     *
46
     * @return void
47
     */
48
    #[On('update-conversation')]
49
    public function updateConversation(): void
50
    {
51
        $this->authorize('update', $this->form->discussConversation);
52
53
        $this->form->title = $this->form->discussConversation->title;
54
        $this->form->category_id = $this->form->discussConversation->category_id;
55
        $this->form->is_pinned = $this->form->discussConversation->is_pinned;
56
        $this->form->is_locked = $this->form->discussConversation->is_locked;
57
58
        $this->form->searchCategories();
59
60
        $this->showModal = true;
61
    }
62
63
    /**
64
     * Update the conversation.
65
     *
66
     * @return void
67
     */
68
    public function update(): void
69
    {
70
        $this->authorize('update', $this->form->discussConversation);
71
72
        $categories = DiscussCategory::pluckLocked('id');
73
74
        $this->validate([
75
            'form.title' => 'required|min:5',
76
            'form.category_id' => [
77
                'required',
78
                'integer',
79
                Rule::in($categories->toArray())
80
            ],
81
            'form.is_pinned' => 'boolean',
82
            'form.is_locked' => 'boolean'
83
        ]);
84
85
        $discussConversation = $this->form->update();
86
87
        redirect()
88
            ->route('discuss.conversation.show', ['slug' => $discussConversation->slug, 'id' => $discussConversation->getKey()])
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

88
            ->/** @scrutinizer ignore-call */ route('discuss.conversation.show', ['slug' => $discussConversation->slug, 'id' => $discussConversation->getKey()])

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...
89
            ->success('Your discussion has been updated successfully !');
90
    }
91
92
    /**
93
     * We must use a function in the component.
94
     *
95
     * @param string $value
96
     *
97
     * @return void
98
     */
99
    public function searchCategories(string $value = ''): void
100
    {
101
        $this->form->searchCategories($value);
102
    }
103
}
104