Passed
Push — 5.0.0 ( 78f010...4a0fb0 )
by Fèvre
05:30
created

User   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 107
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 4 1
A getRowsProperty() 0 3 1
A mount() 0 3 1
A getRowsQueryProperty() 0 19 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Admin\User;
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 Xetaravel\Livewire\Traits\WithBulkActions;
19
use Xetaravel\Livewire\Traits\WithPerPagePagination;
20
use Xetaravel\Livewire\Traits\WithSorting;
21
use Xetaravel\Models\User as UserModel;
22
23
class User 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\User\User: $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 = UserModel::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 article 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
        'username',
78
        'email',
79
        'last_login_date',
80
        'created_at',
81
        'deleted_at'
82
    ];
83
84
    public function mount(): void
85
    {
86
        $this->perPage = config('xetaravel.pagination.user.user_per_page', $this->perPage);
87
    }
88
89
    public function render(): View|Application|Factory|\Illuminate\View\View
90
    {
91
        return view('livewire.admin.user.user', [
92
            'users' => $this->rows
0 ignored issues
show
Bug Best Practice introduced by
The property rows does not exist on Xetaravel\Livewire\Admin\User\User. 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 = UserModel::query()
104
            ->with('account', 'roles');
105
106
        if (Gate::allows('search', UserModel::class)) {
107
            $query->when($this->search, function ($query, $search) {
108
                return $query
109
                    ->where('username', 'LIKE', '%' . $search . '%')
110
                    ->orWhere('email', 'LIKE', '%' . $search . '%')
111
                    ->orWhere('register_ip', 'LIKE', '%' . $search . '%')
112
                    ->orWhere('last_login_ip', 'LIKE', '%' . $search . '%');
113
            });
114
        }
115
116
        // Get also trashed users.
117
        $query->withTrashed();
118
119
        return $this->applySorting($query);
120
    }
121
122
    /**
123
     * Build the query and paginate it.
124
     *
125
     * @return LengthAwarePaginator
126
     */
127
    public function getRowsProperty(): LengthAwarePaginator
128
    {
129
        return $this->applyPagination($this->rowsQuery);
0 ignored issues
show
Bug Best Practice introduced by
The property rowsQuery does not exist on Xetaravel\Livewire\Admin\User\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
130
    }
131
}
132