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