| Conditions | 9 |
| Paths | 2 |
| Total Lines | 130 |
| Code Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 36 | public function setupListOperation() |
||
| 37 | { |
||
| 38 | // Required for order logic. |
||
| 39 | $locale = 'en'; |
||
| 40 | if (null !== $this->request->input('locale')) { |
||
| 41 | $locale = $this->request->input('locale'); |
||
| 42 | } |
||
| 43 | App::setLocale($locale); |
||
| 44 | |||
| 45 | // Add the custom blade buttons found in resources/views/vendor/backpack/crud/buttons/. |
||
| 46 | $this->crud->addButtonFromView('line', 'job_admin_edit', 'job_admin_edit', 'end'); |
||
| 47 | $this->crud->addButtonFromView('line', 'spb_link', 'spb_link', 'end'); |
||
| 48 | $this->crud->addButtonFromView('line', 'jpb_link', 'jpb_link', 'end'); |
||
| 49 | $this->crud->addButtonFromView('line', 'job_poster_link', 'job_poster_link', 'end'); |
||
| 50 | $this->crud->addButtonFromView('line', 'applicants_download', 'applicants_download', 'end'); |
||
| 51 | |||
| 52 | $this->crud->addColumn([ |
||
| 53 | 'name' => 'id', |
||
| 54 | 'type' => 'number', |
||
| 55 | 'label' => 'ID' |
||
| 56 | ]); |
||
| 57 | $this->crud->addColumn([ |
||
| 58 | 'name' => 'title', |
||
| 59 | 'type' => 'text', |
||
| 60 | 'label' => 'Title', |
||
| 61 | 'searchLogic' => function ($query, $column, $searchTerm) use ($locale): void { |
||
| 62 | $query->orWhere('title->' . $locale, 'ilike', "%$searchTerm%"); |
||
| 63 | }, |
||
| 64 | 'orderLogic' => function ($query, $column, $columnDirection) use ($locale) { |
||
| 65 | return $query->orderBy('title->' . $locale, $columnDirection)->select('*'); |
||
| 66 | } |
||
| 67 | ]); |
||
| 68 | $this->crud->addColumn([ |
||
| 69 | 'name' => 'job_poster_status.key', |
||
| 70 | 'label' => 'Status', |
||
| 71 | 'type' => 'text', |
||
| 72 | 'orderable' => true, |
||
| 73 | 'orderLogic' => function ($query, $column, $columnDirection) { |
||
| 74 | return $query->leftJoin('job_poster_status', 'job_poster_status.id', '=', 'job_posters.job_poster_status_id') |
||
| 75 | ->orderBy('job_poster_status.key', $columnDirection)->select('job_posters.*'); |
||
| 76 | } |
||
| 77 | ]); |
||
| 78 | $this->crud->addColumn([ |
||
| 79 | 'name' => 'isOpen', |
||
| 80 | 'label' => 'Open', |
||
| 81 | 'type' => 'closure', |
||
| 82 | 'orderable' => false, |
||
| 83 | 'function' => function ($entry) { |
||
| 84 | return $entry->isOpen() ? |
||
| 85 | '<span><i class="fa fa-check-circle"></i></span>' : |
||
| 86 | '<span><i class="fa fa-circle"></i></span>'; |
||
| 87 | } |
||
| 88 | ]); |
||
| 89 | $this->crud->addColumn([ |
||
| 90 | 'name' => 'isClosed', |
||
| 91 | 'label' => 'Closed', |
||
| 92 | 'type' => 'closure', |
||
| 93 | 'orderable' => false, |
||
| 94 | 'function' => function ($entry) { |
||
| 95 | return $entry->isClosed() ? |
||
| 96 | '<span><i class="fa fa-check-circle"></i></span>' : |
||
| 97 | '<span><i class="fa fa-circle"></i></span>'; |
||
| 98 | } |
||
| 99 | ]); |
||
| 100 | |||
| 101 | $this->crud->addColumn([ |
||
| 102 | 'name' => 'internal_only', |
||
| 103 | 'label' => 'Internal Only', |
||
| 104 | 'type' => 'check', |
||
| 105 | ]); |
||
| 106 | |||
| 107 | $this->crud->addColumn([ |
||
| 108 | 'name' => 'manager_user_name', |
||
| 109 | 'type' => 'closure', |
||
| 110 | 'label' => 'Manager', |
||
| 111 | 'orderable' => false, |
||
| 112 | 'function' => function ($entry) { |
||
| 113 | return '<a href="' . route('manager.profile.edit', $entry->manager->id) . '" target="_blank">' . $entry->manager->user->full_name . '</a>'; |
||
|
|
|||
| 114 | } |
||
| 115 | ]); |
||
| 116 | $this->crud->addColumn([ |
||
| 117 | 'name' => 'department.name', |
||
| 118 | 'label' => 'Department', |
||
| 119 | 'type' => 'text' |
||
| 120 | ]); |
||
| 121 | $this->crud->addColumn([ |
||
| 122 | 'name' => 'submitted_applications_count', |
||
| 123 | 'label' => 'Applications', |
||
| 124 | 'type' => 'closure', |
||
| 125 | 'function' => |
||
| 126 | function ($entry) { |
||
| 127 | return $entry->submitted_applications_count() > 0 ? |
||
| 128 | '<a target="_blank" href="' . route('manager.jobs.applications', $entry->id) . '">' . $entry->submitted_applications_count() . ' (View <i class="fa fa-external-link"></i>)</a>' : |
||
| 129 | $entry->submitted_applications_count(); |
||
| 130 | } |
||
| 131 | ]); |
||
| 132 | |||
| 133 | // Filters. |
||
| 134 | $this->crud->addFilter([ |
||
| 135 | 'name' => 'departments', |
||
| 136 | 'type' => 'select2_multiple', |
||
| 137 | 'label' => 'Filter by department' |
||
| 138 | ], function () { |
||
| 139 | return Department::all()->pluck('name', 'id')->toArray(); |
||
| 140 | }, function ($values) { |
||
| 141 | $this->crud->addClause('WhereHas', 'department', function ($query) use ($values) { |
||
| 142 | foreach (json_decode($values) as $key => $value) { |
||
| 143 | if ($key === 0) { |
||
| 144 | $query->where('id', $value); |
||
| 145 | } else { |
||
| 146 | $query->orWhere('id', $value); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | }); |
||
| 150 | }); |
||
| 151 | |||
| 152 | $this->crud->addFilter([ |
||
| 153 | 'name' => 'statuses', |
||
| 154 | 'type' => 'select2_multiple', |
||
| 155 | 'label' => 'Filter by status' |
||
| 156 | ], function () { |
||
| 157 | // Using key because some of the job status names are the same. |
||
| 158 | return JobPosterStatus::all()->pluck('key', 'id')->toArray(); |
||
| 159 | }, function ($values) { |
||
| 160 | $this->crud->addClause('WhereHas', 'job_poster_status', function ($query) use ($values) { |
||
| 161 | foreach (json_decode($values) as $key => $value) { |
||
| 162 | if ($key === 0) { |
||
| 163 | $query->where('id', $value); |
||
| 164 | } else { |
||
| 165 | $query->orWhere('id', $value); |
||
| 166 | } |
||
| 345 |