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

DiscussConversationForm   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 98
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A store() 0 42 2
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 Livewire\Attributes\Validate;
10
use Livewire\Form;
11
use Xetaio\Mentions\Parser\MentionParser;
12
use Xetaravel\Models\BlogComment;
13
use Xetaravel\Models\DiscussConversation;
14
use Xetaravel\Models\DiscussPost;
15
use Xetaravel\Models\DiscussUser;
16
17
class DiscussConversationForm extends Form
18
{
19
    public ?DiscussConversation $discussConversation = null;
20
21
    /**
22
     * The category of the conversation
23
     *
24
     * @var int|null
25
     */
26
    #[Validate('required|integer|exists:discuss_categories,id')]
27
    public ?int $category_id = null;
28
29
    /**
30
     * The title of the conversation.
31
     *
32
     * @var string|null
33
     */
34
    #[Validate('required|min:5')]
35
    public ?string $title = null;
36
37
    /**
38
     * Whatever the conversation is pinned
39
     *
40
     * @var bool|null
41
     */
42
    #[Validate('boolean')]
43
    public ?bool $is_pinned = false;
44
45
    /**
46
     * Whatever the conversation is locked
47
     *
48
     * @var bool|null
49
     */
50
    #[Validate('boolean')]
51
    public ?bool $is_locked = false;
52
53
    /**
54
     * The categories used in choice.
55
     *
56
     * @var Collection|array
57
     */
58
    public Collection|array $categoriesSearchable = [];
59
60
    /**
61
     * The content of the post.
62
     *
63
     * @var string|null
64
     */
65
    #[Validate('required|min:10')]
66
    public ?string $content = null;
67
68
    /**
69
     * Function to store the model.
70
     *
71
     * @return DiscussConversation
72
     */
73
    public function store(): DiscussConversation
74
    {
75
        $properties = [
76
            'category_id',
77
            'title',
78
        ];
79
80
        if (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

80
        if (Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo('manage discuss conversation')) {
Loading history...
81
            $properties += [
82
                'is_locked',
83
                'is_pinned',
84
            ];
85
        }
86
87
        $discussConversation = DiscussConversation::create($this->only($properties));
88
89
        $post = DiscussPost::create([
90
            '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...
91
            'content' => $this->content
92
        ]);
93
94
        DiscussUser::create([
95
            'conversation_id' => $discussConversation->id,
96
            'is_read' => 1
97
        ]);
98
99
        $discussConversation->first_post_id = $post->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...
100
        $discussConversation->last_post_id = $post->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...
101
        $discussConversation->save();
102
103
        $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...
104
        $discussConversation->category->save();
105
106
        $parser = new MentionParser($post, [
0 ignored issues
show
Bug introduced by
It seems like $post 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

106
        $parser = new MentionParser(/** @scrutinizer ignore-type */ $post, [
Loading history...
107
            'regex' => config('mentions.regex')
108
        ]);
109
        $content = $parser->parse($post->content);
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
110
111
        $post->content = $content;
112
        $post->save();
113
114
        return $discussConversation;
115
    }
116
}
117