Passed
Push — 5.0.0 ( c0ce95...10c169 )
by Fèvre
06:34
created

DiscussPostForm   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 106
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createUser() 0 9 1
A store() 0 26 3
B createPost() 0 25 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Forms;
6
7
use Illuminate\Support\Facades\Auth;
8
use Livewire\Attributes\Locked;
9
use Livewire\Attributes\Validate;
10
use Livewire\Form;
11
use Xetaio\Mentions\Parser\MentionParser;
12
use Xetaravel\Events\Discuss\ConversationWasLockedEvent;
13
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent;
14
use Xetaravel\Models\DiscussConversation;
15
use Xetaravel\Models\DiscussPost;
16
use Xetaravel\Models\DiscussUser;
17
18
class DiscussPostForm extends Form
19
{
20
    /**
21
     * The conversation id where the post belong to.
22
     *
23
     * @var int|null
24
     */
25
    #[Locked]
26
    public ?int $conversation_id = null;
27
28
    /**
29
     * The content of the post.
30
     *
31
     * @var string|null
32
     */
33
    #[Validate('required|min:10')]
34
    public ?string $content = null;
35
36
    /**
37
     * Whatever the conversation is pinned
38
     *
39
     * @var bool|null
40
     */
41
    #[Validate('boolean')]
42
    public ?bool $is_pinned = false;
43
44
    /**
45
     * Whatever the conversation is locked
46
     *
47
     * @var bool|null
48
     */
49
    #[Validate('boolean')]
50
    public ?bool $is_locked = false;
51
52
    private function createPost(array $properties): DiscussPost
53
    {
54
        $post = DiscussPost::create($this->only(['conversation_id', 'content']));
55
56
        $conversation = DiscussConversation::find($properties['conversation_id']);
57
58
        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

58
        if (Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo('pin discuss conversation')) {
Loading history...
59
            if ($conversation->is_pinned !== $properties['is_pinned'] && $properties['is_pinned'] === true) {
0 ignored issues
show
Bug introduced by
The property is_pinned does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
60
                event(new ConversationWasPinnedEvent($conversation));
0 ignored issues
show
Bug introduced by
It seems like $conversation can also be of type Illuminate\Database\Eloq...gHasThroughRelationship and 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

60
                event(new ConversationWasPinnedEvent(/** @scrutinizer ignore-type */ $conversation));
Loading history...
61
            }
62
            $conversation->is_pinned = $properties['is_pinned'];
63
        }
64
65
        if (Auth::user()->hasPermissionTo('lock discuss conversation')) {
66
            if ($conversation->is_locked !== $properties['is_locked'] && $properties['is_locked'] === true) {
0 ignored issues
show
Bug introduced by
The property is_locked does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
67
                event(new ConversationWasLockedEvent($conversation));
0 ignored issues
show
Bug introduced by
It seems like $conversation can also be of type Illuminate\Database\Eloq...gHasThroughRelationship and 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

67
                event(new ConversationWasLockedEvent(/** @scrutinizer ignore-type */ $conversation));
Loading history...
68
            }
69
            $conversation->is_locked = $properties['is_locked'];
70
        }
71
72
        $conversation->last_post_id = $post->getKey();
0 ignored issues
show
Bug introduced by
The property last_post_id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
73
74
        $conversation->save();
75
76
        return $post;
77
    }
78
79
    private function createUser(array $properties): void
80
    {
81
        DiscussUser::updateOrCreate(
82
            [
83
                'user_id' => Auth::id(),
84
                'conversation_id' => $properties['conversation_id']
85
            ],
86
            [
87
                'conversation_id' => $properties['conversation_id']
88
            ]
89
        );
90
    }
91
92
93
    /**
94
     * Function to store the model.
95
     *
96
     * @return DiscussPost
97
     */
98
    public function store(): DiscussPost
99
    {
100
        $properties = [
101
            'conversation_id',
102
            'content'
103
        ];
104
105
        if (Auth::user()->hasPermissionTo('pin discuss conversation')) {
106
            $properties[] = 'is_pinned';
107
        }
108
        if (Auth::user()->hasPermissionTo('lock discuss conversation')) {
109
            $properties[] = 'is_locked';
110
        }
111
112
        $post = $this->createPost($this->only($properties));
113
        $this->createUser($this->only($properties));
114
115
        $parser = new MentionParser($post, [
116
            'regex' => config('mentions.regex')
117
        ]);
118
        $content = $parser->parse($post->content);
119
120
        $post->content = $content;
121
        $post->save();
122
123
        return $post;
124
    }
125
}
126