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

CreateConversation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 90
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 3 1
A create() 0 31 3
A createConversation() 0 9 1
A searchCategories() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Discuss;
6
7
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Validation\Rule;
10
use Livewire\Attributes\On;
11
use Livewire\Component;
12
use Masmerise\Toaster\Toastable;
13
use Xetaravel\Livewire\Forms\DiscussConversationForm;
14
use Xetaravel\Models\DiscussCategory;
15
use Xetaravel\Models\DiscussConversation;
16
use Throwable;
17
18
class CreateConversation extends Component
19
{
20
    use AuthorizesRequests;
21
    use Toastable;
22
23
    /**
24
     * The form used to create/update a model.
25
     *
26
     * @var DiscussConversationForm
27
     */
28
    public DiscussConversationForm $form;
29
30
    /**
31
     * Used to show the Edit/Create modal.
32
     *
33
     * @var bool
34
     */
35
    public bool $showModal = false;
36
37
    public function render()
38
    {
39
        return view('livewire.discuss.create-conversation');
40
    }
41
42
    /**
43
     * Create a blank model and assign it to the model. (Used in create modal)
44
     *
45
     * @return void
46
     */
47
    #[On('create-conversation')]
48
    public function createConversation(): void
49
    {
50
        $this->authorize('create', DiscussConversation::class);
51
52
        $this->form->reset();
53
        $this->form->searchCategories();
54
55
        $this->showModal = true;
56
    }
57
58
    /**
59
     * Validate and save the model.
60
     *
61
     * @return void
62
     *
63
     * @throws Throwable
64
     */
65
    public function create(): void
66
    {
67
        $this->authorize('create', DiscussConversation::class);
68
69
        $categories = DiscussCategory::pluckLocked('id');
70
71
        $this->validate([
72
            'form.title' => 'required|min:5',
73
            'form.category_id' => [
74
                'required',
75
                'integer',
76
                Rule::in($categories->toArray())
77
            ],
78
            'form.is_pinned' => 'boolean',
79
            'form.is_locked' => 'boolean',
80
            'form.content' => 'required|min:10',
81
        ]);
82
83
        if (DiscussConversation::isFlooding('xetaravel.flood.discuss.conversation') &&
84
            !Auth::user()->hasPermissionTo('manage discuss conversation')) {
0 ignored issues
show
Bug introduced by
The method hasPermissionTo() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

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

84
            !Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo('manage discuss conversation')) {
Loading history...
85
            $this->error('Wow, keep calm bro, and try to not flood !');
86
87
            return;
88
        }
89
        $discussConversation = $this->form->create();
90
91
        $this->showModal = false;
92
93
        redirect()
94
            ->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

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