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

DiscussConversationForm::store()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 25
nc 4
nop 0
dl 0
loc 42
rs 9.52
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Forms;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\DB;
10
use Livewire\Form;
11
use Xetaio\Mentions\Parser\MentionParser;
12
use Xetaravel\Events\Discuss\CategoryWasChangedEvent;
13
use Xetaravel\Events\Discuss\ConversationWasCreatedEvent;
14
use Xetaravel\Events\Discuss\ConversationWasLockedEvent;
15
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent;
16
use Xetaravel\Events\Discuss\TitleWasChangedEvent;
17
use Xetaravel\Models\DiscussCategory;
18
use Xetaravel\Models\DiscussConversation;
19
use Xetaravel\Models\DiscussPost;
20
use Xetaravel\Models\DiscussUser;
21
use Throwable;
22
23
class DiscussConversationForm extends Form
24
{
25
    /**
26
     * The conversation to update.
27
     *
28
     * @var DiscussConversation|null
29
     */
30
    public ?DiscussConversation $discussConversation = null;
31
32
    /**
33
     * The category of the conversation
34
     *
35
     * @var int|null
36
     */
37
    public ?int $category_id = null;
38
39
    /**
40
     * The title of the conversation.
41
     *
42
     * @var string|null
43
     */
44
    public ?string $title = null;
45
46
    /**
47
     * Whatever the conversation is pinned
48
     *
49
     * @var bool|null
50
     */
51
    public ?bool $is_pinned = false;
52
53
    /**
54
     * Whatever the conversation is locked.
55
     *
56
     * @var bool|null
57
     */
58
    public ?bool $is_locked = false;
59
60
    /**
61
     * The content of the post, only when creating.
62
     *
63
     * @var string|null
64
     */
65
    public ?string $content = null;
66
67
    /**
68
     * The categories used in choice.
69
     *
70
     * @var Collection|array
71
     */
72
    public Collection|array $categoriesSearchable = [];
73
74
    /**
75
     * Function to store the model.
76
     *
77
     * @return DiscussConversation
78
     *
79
     * @throws Throwable
80
     */
81
    public function create(): DiscussConversation
82
    {
83
        $properties = [
84
            'category_id',
85
            'title',
86
        ];
87
88
        if (Auth::user()->hasPermissionTo('pin 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
        if (Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo('pin discuss conversation')) {
Loading history...
89
            $properties[] = 'is_pinned';
90
        }
91
        if (Auth::user()->hasPermissionTo('lock discuss conversation')) {
92
            $properties[] = 'is_locked';
93
        }
94
95
        return DB::transaction(function () use ($properties) {
96
            $discussConversation = DiscussConversation::create($this->only($properties));
97
98
            $discussPost = DiscussPost::create([
99
                'conversation_id' => $discussConversation->id,
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
100
                'content' => $this->content
101
            ]);
102
103
            DiscussUser::create([
104
                'conversation_id' => $discussConversation->id,
105
                'is_read' => 1
106
            ]);
107
108
            $discussConversation->first_post_id = $discussPost->id;
0 ignored issues
show
Bug introduced by
The property first_post_id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
109
            $discussConversation->last_post_id = $discussPost->id;
0 ignored issues
show
Bug introduced by
The property last_post_id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
110
            $discussConversation->save();
111
112
            $discussConversation->category->last_conversation_id = $discussConversation->getKey();
0 ignored issues
show
Bug introduced by
The property category does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
113
            $discussConversation->category->save();
114
115
            $parser = new MentionParser($discussPost, [
0 ignored issues
show
Bug introduced by
It seems like $discussPost can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $model of Xetaio\Mentions\Parser\M...onParser::__construct() does only seem to accept Illuminate\Database\Eloquent\Model, 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

115
            $parser = new MentionParser(/** @scrutinizer ignore-type */ $discussPost, [
Loading history...
116
                'regex' => config('mentions.regex')
117
            ]);
118
            $content = $parser->parse($discussPost->content);
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
119
120
            $discussPost->content = $content;
121
            $discussPost->save();
122
123
            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

123
            event(new ConversationWasCreatedEvent(/** @scrutinizer ignore-type */ Auth::user(), $discussConversation));
Loading history...
Bug introduced by
It seems like $discussConversation can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $discussConversation of Xetaravel\Events\Discuss...tedEvent::__construct() does only seem to accept Xetaravel\Models\DiscussConversation, 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

123
            event(new ConversationWasCreatedEvent(Auth::user(), /** @scrutinizer ignore-type */ $discussConversation));
Loading history...
124
125
            return $discussConversation;
126
        });
127
    }
128
129
    /**
130
     * Function to update the conversation.
131
     *
132
     * @return DiscussConversation
133
     */
134
    public function update(): DiscussConversation
135
    {
136
        // The title has changed
137
        if ($this->discussConversation->title !== $this->title) {
138
            event(new TitleWasChangedEvent($this->discussConversation, $this->title, $this->discussConversation->title));
0 ignored issues
show
Bug introduced by
It seems like $this->discussConversation can also be of type null; however, parameter $discussConversation of Xetaravel\Events\Discuss...gedEvent::__construct() does only seem to accept Xetaravel\Models\DiscussConversation, 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

138
            event(new TitleWasChangedEvent(/** @scrutinizer ignore-type */ $this->discussConversation, $this->title, $this->discussConversation->title));
Loading history...
Bug introduced by
It seems like $this->title can also be of type null; however, parameter $title of Xetaravel\Events\Discuss...gedEvent::__construct() does only seem to accept string, 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

138
            event(new TitleWasChangedEvent($this->discussConversation, /** @scrutinizer ignore-type */ $this->title, $this->discussConversation->title));
Loading history...
139
140
            $this->discussConversation->title = $this->title;
141
        }
142
143
        // The category has changed
144
        if ($this->discussConversation->category_id !== $this->category_id) {
145
            event(new CategoryWasChangedEvent($this->discussConversation, $this->category_id, $this->discussConversation->category_id));
0 ignored issues
show
Bug introduced by
It seems like $this->discussConversation can also be of type null; however, parameter $discussConversation of Xetaravel\Events\Discuss...gedEvent::__construct() does only seem to accept Xetaravel\Models\DiscussConversation, 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

145
            event(new CategoryWasChangedEvent(/** @scrutinizer ignore-type */ $this->discussConversation, $this->category_id, $this->discussConversation->category_id));
Loading history...
Bug introduced by
It seems like $this->category_id can also be of type null; however, parameter $category of Xetaravel\Events\Discuss...gedEvent::__construct() does only seem to accept integer, 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

145
            event(new CategoryWasChangedEvent($this->discussConversation, /** @scrutinizer ignore-type */ $this->category_id, $this->discussConversation->category_id));
Loading history...
146
147
            $this->discussConversation->category_id = $this->category_id;
148
        }
149
150
        // The pinned status has changed
151
        if (Auth::user()->hasPermissionTo('pin discuss conversation')) {
152
            if ($this->discussConversation->is_pinned !== $this->is_pinned && $this->is_pinned === true) {
153
                event(new ConversationWasPinnedEvent($this->discussConversation));
0 ignored issues
show
Bug introduced by
It seems like $this->discussConversation can also be of type null; however, parameter $discussConversation of Xetaravel\Events\Discuss...nedEvent::__construct() does only seem to accept Xetaravel\Models\DiscussConversation, 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

153
                event(new ConversationWasPinnedEvent(/** @scrutinizer ignore-type */ $this->discussConversation));
Loading history...
154
            }
155
            $this->discussConversation->is_pinned = $this->is_pinned;
156
        }
157
158
        // The locked status has changed
159
        if (Auth::user()->hasPermissionTo('lock discuss conversation')) {
160
            if ($this->discussConversation->is_locked !== $this->is_locked && $this->is_locked === true) {
161
                event(new ConversationWasLockedEvent($this->discussConversation));
0 ignored issues
show
Bug introduced by
It seems like $this->discussConversation can also be of type null; however, parameter $discussConversation of Xetaravel\Events\Discuss...kedEvent::__construct() does only seem to accept Xetaravel\Models\DiscussConversation, 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

161
                event(new ConversationWasLockedEvent(/** @scrutinizer ignore-type */ $this->discussConversation));
Loading history...
162
            }
163
            $this->discussConversation->is_locked = $this->is_locked;
164
        }
165
166
        $this->discussConversation->save();
0 ignored issues
show
Bug introduced by
The method save() does not exist on null. ( Ignorable by Annotation )

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

166
        $this->discussConversation->/** @scrutinizer ignore-call */ 
167
                                    save();

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...
167
168
        return $this->discussConversation;
169
    }
170
171
    /**
172
     * Function to search categories.
173
     *
174
     * @param string $value
175
     *
176
     * @return void
177
     */
178
    public function searchCategories(string $value = ''): void
179
    {
180
        $selectedOption = DiscussCategory::where('id', $this->category_id)->get();
181
182
        $categories = DiscussCategory::query()
183
            ->where('title', 'like', "%$value%");
184
185
        $this->categoriesSearchable = $categories->take(10)
186
            ->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

186
            ->orderBy(/** @scrutinizer ignore-type */ 'title')
Loading history...
187
            ->get()
188
            ->merge($selectedOption);
189
    }
190
}
191