Passed
Push — master ( f543e4...00966f )
by Darko
13:26
created

AdminUserRoleHistoryController::index()   D

Complexity

Conditions 15
Paths 256

Size

Total Lines 65
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 65
rs 4.3833
cc 15
nc 256
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use App\Models\User;
7
use App\Models\UserRoleHistory;
8
use Illuminate\Http\Request;
9
use Spatie\Permission\Models\Role;
10
11
class AdminUserRoleHistoryController extends BasePageController
12
{
13
    /**
14
     * Display user role history list
15
     */
16
    public function index(Request $request)
17
    {
18
        $this->setAdminPrefs();
19
20
        $meta_title = $title = 'User Role History';
21
22
        // Get all roles for filter
23
        $roles = Role::all()->pluck('name', 'id')->toArray();
24
25
        // Build query
26
        $query = UserRoleHistory::with(['user', 'oldRole', 'newRole', 'changedByUser'])
27
            ->orderBy('created_at', 'desc');
28
29
        // Apply filters
30
        if ($request->has('user_id') && !empty($request->input('user_id'))) {
31
            $query->where('user_id', $request->input('user_id'));
32
        }
33
34
        if ($request->has('username') && !empty($request->input('username'))) {
35
            $query->whereHas('user', function ($q) use ($request) {
36
                $q->where('username', 'like', '%' . $request->input('username') . '%');
37
            });
38
        }
39
40
        if ($request->has('role_id') && !empty($request->input('role_id'))) {
41
            $query->where(function ($q) use ($request) {
42
                $q->where('old_role_id', $request->input('role_id'))
43
                    ->orWhere('new_role_id', $request->input('role_id'));
44
            });
45
        }
46
47
        if ($request->has('change_reason') && !empty($request->input('change_reason'))) {
48
            $query->where('change_reason', 'like', '%' . $request->input('change_reason') . '%');
49
        }
50
51
        if ($request->has('date_from') && !empty($request->input('date_from'))) {
52
            $query->where('created_at', '>=', $request->input('date_from') . ' 00:00:00');
53
        }
54
55
        if ($request->has('date_to') && !empty($request->input('date_to'))) {
56
            $query->where('created_at', '<=', $request->input('date_to') . ' 23:59:59');
57
        }
58
59
        // Pagination
60
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
61
        $perPage = config('nntmux.items_per_page', 50);
62
63
        $results = $query->paginate($perPage, ['*'], 'page', $page);
64
65
        $this->viewData = array_merge($this->viewData, [
66
            'title' => $title,
67
            'meta_title' => $meta_title,
68
            'history' => $results,
69
            'roles' => $roles,
70
            'filters' => [
71
                'user_id' => $request->input('user_id', ''),
72
                'username' => $request->input('username', ''),
73
                'role_id' => $request->input('role_id', ''),
74
                'change_reason' => $request->input('change_reason', ''),
75
                'date_from' => $request->input('date_from', ''),
76
                'date_to' => $request->input('date_to', ''),
77
            ],
78
        ]);
79
80
        return view('admin.user-role-history.index', $this->viewData);
81
    }
82
83
    /**
84
     * Display role history for a specific user
85
     */
86
    public function show(Request $request, int $userId)
87
    {
88
        $this->setAdminPrefs();
89
90
        $user = User::findOrFail($userId);
91
        $meta_title = $title = 'Role History for ' . $user->username;
92
93
        $history = UserRoleHistory::with(['oldRole', 'newRole', 'changedByUser'])
94
            ->where('user_id', $userId)
95
            ->orderBy('created_at', 'desc')
96
            ->paginate(config('nntmux.items_per_page', 50));
97
98
        $this->viewData = array_merge($this->viewData, [
99
            'title' => $title,
100
            'meta_title' => $meta_title,
101
            'user' => $user,
102
            'history' => $history,
103
        ]);
104
105
        return view('admin.user-role-history.show', $this->viewData);
106
    }
107
}
108
109