Category   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRowsQueryProperty() 0 10 1
A getRowsProperty() 0 3 1
A render() 0 4 1
A mount() 0 3 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\BlogCategory;
21
22
class Category 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\Category: $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 = BlogCategory::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 category 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
        'title',
77
        'description',
78
        'blog_article_count',
79
        'created_at'
80
    ];
81
82
    public function mount(): void
83
    {
84
        $this->perPage = config('xetaravel.pagination.blog.article_per_page', $this->perPage);
85
    }
86
87
    public function render(): View|Application|Factory|\Illuminate\View\View
88
    {
89
        return view('livewire.admin.blog.category', [
90
            'categories' => $this->rows
0 ignored issues
show
Bug Best Practice introduced by
The property rows does not exist on Xetaravel\Livewire\Admin\Blog\Category. Since you implemented __get, consider adding a @property annotation.
Loading history...
91
        ]);
92
    }
93
94
    /**
95
     * Create and return the query for the items.
96
     *
97
     * @return Builder
98
     */
99
    public function getRowsQueryProperty(): Builder
100
    {
101
        $query = BlogCategory::query()
102
            ->when($this->search, function ($query, $search) {
103
                return $query
104
                    ->where('title', 'LIKE', '%' . $search . '%')
105
                    ->orWhere('description', 'LIKE', '%' . $search . '%');
106
            });
107
108
        return $this->applySorting($query);
0 ignored issues
show
Bug introduced by
$query of type Xetaravel\Models\BlogCategory is incompatible with the type Illuminate\Contracts\Database\Query\Builder expected by parameter $query of Xetaravel\Livewire\Admin...ategory::applySorting(). ( Ignorable by Annotation )

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

108
        return $this->applySorting(/** @scrutinizer ignore-type */ $query);
Loading history...
109
    }
110
111
    /**
112
     * Build the query and paginate it.
113
     *
114
     * @return LengthAwarePaginator
115
     */
116
    public function getRowsProperty(): LengthAwarePaginator
117
    {
118
        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\Category. Since you implemented __get, consider adding a @property annotation.
Loading history...
119
    }
120
}
121