Passed
Push — 5.0.0 ( 233e11...ab4a40 )
by Fèvre
05:22
created

Category::getRowsQueryProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Admin\Discuss;
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\DiscussCategory;
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\Discuss\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 = DiscussCategory::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
        'title',
77
        'description',
78
        'color',
79
        'icon',
80
        'level',
81
        'conversation_count',
82
        'is_locked',
83
        'created_at'
84
    ];
85
86
    public function mount(): void
87
    {
88
        $this->perPage = config('xetaravel.pagination.discuss.category_per_page', $this->perPage);
89
    }
90
91
    public function render(): View|Application|Factory|\Illuminate\View\View
92
    {
93
        return view('livewire.admin.discuss.category', [
94
            'categories' => $this->rows
0 ignored issues
show
Bug Best Practice introduced by
The property rows does not exist on Xetaravel\Livewire\Admin\Discuss\Category. Since you implemented __get, consider adding a @property annotation.
Loading history...
95
        ]);
96
    }
97
98
    /**
99
     * Create and return the query for the items.
100
     *
101
     * @return Builder
102
     */
103
    public function getRowsQueryProperty(): Builder
104
    {
105
        $query = DiscussCategory::query()
106
            ->when($this->search, function ($query, $search) {
107
                return $query
108
                    ->where('title', 'LIKE', '%' . $search . '%')
109
                    ->orWhere('description', 'LIKE', '%' . $search . '%');
110
            });
111
112
        return $this->applySorting($query);
0 ignored issues
show
Bug introduced by
$query of type Xetaravel\Models\DiscussCategory 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

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