Passed
Push — 5.0.0 ( 7f8e42...2b6cb3 )
by Fèvre
05:43
created

CommentForm::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Forms;
6
7
use Illuminate\Support\Facades\DB;
8
use Livewire\Attributes\Locked;
9
use Livewire\Attributes\Validate;
10
use Livewire\Form;
11
use Xetaio\Mentions\Parser\MentionParser;
12
use Xetaravel\Models\BlogComment;
13
use Throwable;
14
15
class CommentForm extends Form
16
{
17
    /**
18
     * The article id where the comment belong to.
19
     *
20
     * @var int|null
21
     */
22
    #[Locked]
23
    public ?int $blog_article_id = null;
24
25
    /**
26
     * The content of the comment.
27
     *
28
     * @var string|null
29
     */
30
    #[Validate('required|min:10')]
31
    public ?string $content = null;
32
33
    /**
34
     * Function to store the model.
35
     *
36
     * @return BlogComment
37
     *
38
     * @throws Throwable
39
     */
40
    public function create(): BlogComment
41
    {
42
        return DB::transaction(function () {
43
            $comment = BlogComment::create($this->only([
44
                'blog_article_id',
45
                'content'
46
            ]));
47
48
            $parser = new MentionParser($comment);
0 ignored issues
show
Bug introduced by
It seems like $comment 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

48
            $parser = new MentionParser(/** @scrutinizer ignore-type */ $comment);
Loading history...
49
            $content = $parser->parse($this->content);
50
            $comment->content = $content;
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
51
            $comment->save();
52
53
            return $comment;
54
        });
55
    }
56
}
57