Passed
Push — 5.0.0 ( 7b1056...f0e712 )
by Fèvre
05:33
created

Comment::getRowsProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Blog;
6
7
use Illuminate\Auth\Access\AuthorizationException;
8
use Illuminate\Contracts\Database\Query\Builder;
9
use Illuminate\Contracts\View\View;
10
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
11
use Illuminate\Pagination\LengthAwarePaginator;
12
use Livewire\Component;
13
use Masmerise\Toaster\Toastable;
14
use Xetaravel\Livewire\Forms\CommentForm;
15
use Xetaravel\Livewire\Traits\WithCachedRows;
16
use Xetaravel\Livewire\Traits\WithPerPagePagination;
17
use Xetaravel\Livewire\Traits\WithSorting;
18
use Xetaravel\Models\BlogArticle;
19
use Xetaravel\Models\BlogComment;
20
21
class Comment extends Component
22
{
23
    use AuthorizesRequests;
24
    use Toastable;
25
    use WithCachedRows;
26
    use WithPerPagePagination;
27
    use WithSorting;
28
29
    public BlogArticle $article;
30
31
    /**
32
     * The number of comment for this article.
33
     *
34
     * @var int|null
35
     */
36
    public ?int $blogCommentCount = null;
37
38
    /**
39
     * The form used to create a comment.
40
     *
41
     * @var CommentForm
42
     */
43
    public CommentForm $form;
44
45
    /**
46
     * Number of rows displayed on a page.
47
     *
48
     * @var int
49
     */
50
    public int $perPage = 3;
51
52
    /**
53
     * The field to sort by.
54
     *
55
     * @var string
56
     */
57
    public string $sortField = 'created_at';
58
59
    /**
60
     * The direction of the ordering.
61
     *
62
     * @var string
63
     */
64
    public string $sortDirection = 'desc';
65
66
    /**
67
     * Array of allowed fields.
68
     *
69
     * @var array
70
     */
71
    public array $allowedFields = [
72
        'created_at'
73
    ];
74
75
    /**
76
     * The content of the comment.
77
     *
78
     * @var string
79
     */
80
    public string $content = '';
81
82
    public function mount(BlogArticle $article): void
83
    {
84
        $this->article = $article;
85
        $this->form->blog_article_id = $article->id;
86
        $this->blogCommentCount = $article->blog_comment_count;
87
    }
88
89
    public function render(): View
90
    {
91
        return view('livewire.blog.comment', [
92
            'comments' => $this->rows
0 ignored issues
show
Bug Best Practice introduced by
The property rows does not exist on Xetaravel\Livewire\Blog\Comment. Since you implemented __get, consider adding a @property annotation.
Loading history...
93
        ]);
94
    }
95
96
    /**
97
     * Create and return the query for the items.
98
     *
99
     * @return Builder
100
     */
101
    public function getRowsQueryProperty(): Builder
102
    {
103
        $query = BlogComment::query()
104
            ->with('user', 'user.account')
105
            ->where('blog_article_id', $this->article->id);
106
107
        return $this->applySorting($query);
108
    }
109
110
    /**
111
     * Build the query or get it from the cache and paginate it.
112
     *
113
     * @return LengthAwarePaginator
114
     */
115
    public function getRowsProperty(): LengthAwarePaginator
116
    {
117
        return $this->cache(function () {
118
            return $this->applyPagination($this->rowsQuery);
0 ignored issues
show
Bug Best Practice introduced by
The property rowsQuery does not exist on Xetaravel\Livewire\Blog\Comment. Since you implemented __get, consider adding a @property annotation.
Loading history...
119
        });
120
    }
121
122
    /**
123
     * Validate and create the model.
124
     *
125
     * @return void
126
     *
127
     * @throws AuthorizationException
128
     */
129
    public function create(): void
130
    {
131
        $this->authorize('create', BlogComment::class);
132
133
        $this->validate();
134
135
        $this->form->store();
136
137
        $this->form->content = "";
138
139
        $this->success('Your comment has been created !');
140
141
142
143
        $this->blogCommentCount++;
144
        //$this->js('window.editor.value = \'\'');
145
        //$this->js('alert(window.editor.value())');
146
        $this->dispatch('comment-created');
147
    }
148
}
149