UserTable   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 37
c 3
b 1
f 0
dl 0
loc 59
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updatedRole() 0 4 1
A render() 0 6 1
A updatedSearch() 0 4 1
A getUsers() 0 28 5
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Livewire\Admin\User;
4
5
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\Cache;
7
use Livewire\Component;
8
use Livewire\WithPagination;
9
use Pratiksh\Adminetic\Models\Admin\Role;
10
11
class UserTable extends Component
12
{
13
    use WithPagination;
14
15
    protected $paginationTheme = 'bootstrap';
16
17
    public $filter = 1;
18
    public $information;
19
    public $role;
20
    public $search;
21
22
    public function updatedSearch()
23
    {
24
        $this->filter = 2;
25
        $this->resetPage();
26
    }
27
28
    public function updatedRole()
29
    {
30
        $this->filter = 3;
31
        $this->resetPage();
32
    }
33
34
    public function render()
35
    {
36
        $users = $this->getUsers();
37
        $roles = Cache::get('roles', Role::latest()->get());
38
39
        return view('adminetic::livewire.admin.user.user-table', compact('users', 'roles'));
40
    }
41
42
    protected function getUsers()
43
    {
44
        $filter = $this->filter;
45
        $default = User::with('profile', 'roles');
46
        switch ($filter) {
47
            case 1:
48
                $data = $default->latest();
49
                break;
50
            case 2:
51
                $this->resetPage();
52
                $search = $this->search ?? null;
53
                $data = $default->where('name', 'like', '%'.$search.'%')
54
                    ->orWhere('email', 'like', '%'.$search.'%');
55
                $this->information = 'Showing search results for "'.$search.'"';
56
                break;
57
            case 3:
58
                $this->resetPage();
59
                $role_id = $this->role ?? null;
60
                $data = $role_id == '' ? $default : $default->whereHas('roles', function ($query) use ($role_id) {
61
                    $query->where('role_id', $role_id);
62
                });
63
                break;
64
            default:
65
                $data = $default->latest();
66
                break;
67
        }
68
69
        return $data->paginate(10);
70
    }
71
}
72