Passed
Push — 5.0.0 ( f87479...b41ea8 )
by Fèvre
07:54
created

Permission::getRowsQueryProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Admin\Permission;
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 Illuminate\Support\Facades\Gate;
14
use Livewire\Attributes\Url;
15
use Livewire\Component;
16
use Livewire\WithPagination;
17
use Masmerise\Toaster\Toastable;
18
use Spatie\Permission\Models\Permission as ModelPermission;
19
use Xetaravel\Livewire\Traits\WithBulkActions;
20
use Xetaravel\Livewire\Traits\WithPerPagePagination;
21
use Xetaravel\Livewire\Traits\WithSorting;
22
23
class Permission extends Component
24
{
25
    use AuthorizesRequests;
26
    use Toastable;
27
    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\Permission\Permission: $selectedRowsQuery, $rows
Loading history...
28
    use WithPagination;
29
    use WithPerPagePagination;
30
    use WithSorting;
31
32
    /**
33
     * Bind the main model used in the component to be used in traits.
34
     *
35
     * @var string
36
     */
37
    public string $model = ModelPermission::class;
38
39
    /**
40
     * The field to sort by.
41
     *
42
     * @var string
43
     */
44
    #[Url(as: 'f', except: 'created_at')]
45
    public string $sortField = 'created_at';
46
47
    /**
48
     * The direction of the ordering.
49
     *
50
     * @var string
51
     */
52
    #[Url(as: 'd')]
53
    public string $sortDirection = 'desc';
54
55
    /**
56
     * The string to search.
57
     *
58
     * @var string
59
     */
60
    #[Url(as: 's', except: '')]
61
    public string $search = '';
62
63
    /**
64
     * The number of permission limited per page.
65
     *
66
     * @var int
67
     */
68
    public int $perPage = 15;
69
70
    /**
71
     * Array of allowed fields.
72
     *
73
     * @var array
74
     */
75
    public array $allowedFields = [
76
        'id',
77
        'name',
78
        'description',
79
        'created_at'
80
    ];
81
82
    public function render(): View|Application|Factory|\Illuminate\View\View
83
    {
84
        return view('livewire.admin.permission.permission', [
85
            'permissions' => $this->rows
0 ignored issues
show
Bug Best Practice introduced by
The property rows does not exist on Xetaravel\Livewire\Admin\Permission\Permission. Since you implemented __get, consider adding a @property annotation.
Loading history...
86
        ]);
87
    }
88
89
    /**
90
     * Create and return the query for the items.
91
     *
92
     * @return Builder
93
     */
94
    public function getRowsQueryProperty(): Builder
95
    {
96
        $query = ModelPermission::query();
97
98
        if (Gate::allows('search', ModelPermission::class)) {
99
            $query ->when($this->search, function ($query, $search) {
100
                return $query
101
                    ->where('name', 'LIKE', '%' . $search . '%')
102
                    ->orWhere('description', 'LIKE', '%' . $search . '%');
103
            });
104
        }
105
106
        return $this->applySorting($query);
107
    }
108
109
    /**
110
     * Build the query and paginate it.
111
     *
112
     * @return LengthAwarePaginator
113
     */
114
    public function getRowsProperty(): LengthAwarePaginator
115
    {
116
        return $this->applyPagination($this->rowsQuery);
0 ignored issues
show
Bug Best Practice introduced by
The property rowsQuery does not exist on Xetaravel\Livewire\Admin\Permission\Permission. Since you implemented __get, consider adding a @property annotation.
Loading history...
117
    }
118
}
119