Passed
Push — 5.0.0 ( 746f48...fc989a )
by Fèvre
06:29
created

CreateConversation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 103
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 3 1
A mount() 0 6 2
A save() 0 21 3
A create() 0 8 1
A searchCategories() 0 12 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\Http\RedirectResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Auth;
11
use Livewire\Attributes\Url;
12
use Livewire\Component;
13
use Masmerise\Toaster\Toastable;
14
use Xetaio\Mentions\Parser\MentionParser;
15
use Xetaravel\Events\Discuss\ConversationWasCreatedEvent;
16
use Xetaravel\Livewire\Forms\DiscussConversationForm;
17
use Xetaravel\Models\DiscussCategory;
18
use Xetaravel\Models\DiscussConversation;
19
20
class CreateConversation extends Component
21
{
22
    use AuthorizesRequests;
23
    use Toastable;
24
25
    /**
26
     * The form used to create/update a model.
27
     *
28
     * @var DiscussConversationForm
29
     */
30
    public DiscussConversationForm $form;
31
32
    /**
33
     * Used to show the Edit/Create modal.
34
     *
35
     * @var bool
36
     */
37
    public bool $showModal = false;
38
39
    /**
40
     * Whatever the creating url param is set or not.
41
     *
42
     * @var bool
43
     */
44
    #[Url(except: false)]
45
    public bool $creating = false;
46
47
    public function mount(Request $request): void
48
    {
49
        if ($request->boolean('creating') === true) {
50
            $this->create();
51
52
            $this->creating = false;
53
        }
54
    }
55
56
    public function render()
57
    {
58
        return view('livewire.discuss.create-conversation');
59
    }
60
61
    /**
62
     * Create a blank model and assign it to the model. (Used in create modal)
63
     *
64
     * @return void
65
     */
66
    public function create(): void
67
    {
68
        $this->authorize('create', DiscussConversation::class);
69
70
        $this->form->reset();
71
        $this->searchCategories();
72
73
        $this->showModal = true;
74
    }
75
76
    /**
77
     * Validate and save the model.
78
     *
79
     * @return void
80
     */
81
    public function save(): void
82
    {
83
        $this->authorize('create', DiscussConversation::class);
84
85
        $this->validate();
86
87
        if (DiscussConversation::isFlooding('xetaravel.flood.discuss.conversation') &&
88
            !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

88
            !Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo('manage discuss conversation')) {
Loading history...
89
            $this->error('Wow, keep calm bro, and try to not flood !');
90
91
            return;
92
        }
93
        $discussConversation = $this->form->store();
94
95
        event(new ConversationWasCreatedEvent(Auth::user(), $discussConversation));
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facades\Auth::user() can also be of type null; however, parameter $user of Xetaravel\Events\Discuss...tedEvent::__construct() does only seem to accept Xetaravel\Models\User, maybe add an additional type check? ( Ignorable by Annotation )

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

95
        event(new ConversationWasCreatedEvent(/** @scrutinizer ignore-type */ Auth::user(), $discussConversation));
Loading history...
96
97
        $this->showModal = false;
98
99
        redirect()
100
            ->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

100
            ->/** @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...
101
            ->success('Your discussion has been created successfully !');
102
    }
103
104
    /**
105
     * Function to search suppliers in form.
106
     *
107
     * @param string $value
108
     *
109
     * @return void
110
     */
111
    public function searchCategories(string $value = ''): void
112
    {
113
        $selectedOption = DiscussCategory::where('id', $this->form->category_id)->get();
114
115
116
        $categories = DiscussCategory::query()
117
            ->where('title', 'like', "%$value%");
118
119
        $this->form->categoriesSearchable = $categories->take(10)
120
            ->orderBy('title')
0 ignored issues
show
Bug introduced by
'title' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

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

120
            ->orderBy(/** @scrutinizer ignore-type */ 'title')
Loading history...
121
            ->get()
122
            ->merge($selectedOption);
123
    }
124
}
125