| Conditions | 15 |
| Paths | 256 |
| Total Lines | 65 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 109 |