Passed
Push — 5.0.0 ( 580cb8...19859a )
by Fèvre
05:24
created

Article   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 4 1
A mount() 0 3 1
A getRowsProperty() 0 3 1
A getRowsQueryProperty() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Admin\Blog;
6
7
use Illuminate\Contracts\Database\Query\Builder;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use Illuminate\Contracts\View\Factory;
10
use Illuminate\Contracts\View\View;
11
use Illuminate\Foundation\Application;
12
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
13
use Livewire\Attributes\Url;
14
use Livewire\Component;
15
use Livewire\WithPagination;
16
use Masmerise\Toaster\Toastable;
17
use Xetaravel\Livewire\Traits\WithBulkActions;
18
use Xetaravel\Livewire\Traits\WithPerPagePagination;
19
use Xetaravel\Livewire\Traits\WithSorting;
20
use Xetaravel\Models\BlogArticle;
21
22
class Article extends Component
23
{
24
    use AuthorizesRequests;
25
    use Toastable;
26
    use WithBulkActions;
0 ignored issues
show
introduced by
The trait Xetaravel\Livewire\Traits\WithBulkActions requires some properties which are not provided by Xetaravel\Livewire\Admin\Blog\Article: $selectedRowsQuery, $rows
Loading history...
27
    use WithPagination;
28
    use WithPerPagePagination;
29
    use WithSorting;
30
31
    /**
32
     * Bind the main model used in the component to be used in traits.
33
     *
34
     * @var string
35
     */
36
    public string $model = BlogArticle::class;
37
38
    /**
39
     * The field to sort by.
40
     *
41
     * @var string
42
     */
43
    #[Url(as: 'f', except: 'created_at')]
44
    public string $sortField = 'created_at';
45
46
    /**
47
     * The direction of the ordering.
48
     *
49
     * @var string
50
     */
51
    #[Url(as: 'd')]
52
    public string $sortDirection = 'desc';
53
54
    /**
55
     * The string to search.
56
     *
57
     * @var string
58
     */
59
    #[Url(as: 's', except: '')]
60
    public string $search = '';
61
62
    /**
63
     * The number of article limited per page.
64
     *
65
     * @var int
66
     */
67
    public int $perPage = 15;
68
69
    /**
70
     * Array of allowed fields.
71
     *
72
     * @var array
73
     */
74
    public array $allowedFields = [
75
        'id',
76
        'user_id',
77
        'title',
78
        'blog_category_id',
79
        'blog_comment_count',
80
        'published_at',
81
        'created_at'
82
    ];
83
84
    public function mount(): void
85
    {
86
        $this->perPage = config('xetaravel.pagination.blog.article_per_page', $this->perPage);
87
    }
88
89
    public function render(): View|Application|Factory|\Illuminate\View\View
90
    {
91
        return view('livewire.admin.blog.article', [
92
            'articles' => $this->rows
0 ignored issues
show
Bug Best Practice introduced by
The property rows does not exist on Xetaravel\Livewire\Admin\Blog\Article. 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 = BlogArticle::query()
104
            ->with(['category', 'user.account'])
105
            ->when($this->search, function ($query, $search) {
106
                return $query
107
                    ->where('title', 'LIKE', '%' . $search . '%')
108
                    ->orWhere('content', 'LIKE', '%' . $search . '%');
109
            });
110
111
        return $this->applySorting($query);
0 ignored issues
show
Bug introduced by
$query of type Xetaravel\Models\BlogArticle is incompatible with the type Illuminate\Contracts\Database\Query\Builder expected by parameter $query of Xetaravel\Livewire\Admin...Article::applySorting(). ( Ignorable by Annotation )

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

111
        return $this->applySorting(/** @scrutinizer ignore-type */ $query);
Loading history...
112
    }
113
114
    /**
115
     * Build the query and paginate it.
116
     *
117
     * @return LengthAwarePaginator
118
     */
119
    public function getRowsProperty(): LengthAwarePaginator
120
    {
121
        return $this->applyPagination($this->rowsQuery);
0 ignored issues
show
Bug Best Practice introduced by
The property rowsQuery does not exist on Xetaravel\Livewire\Admin\Blog\Article. Since you implemented __get, consider adding a @property annotation.
Loading history...
122
    }
123
}
124