Conditions | 7 |
Paths | 16 |
Total Lines | 166 |
Code Lines | 98 |
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 |
||
93 | public function setupListOperation() |
||
94 | { |
||
95 | if (config('app.currency_position') === 'before') { |
||
96 | $currency = ['prefix' => config('app.currency_symbol')]; |
||
97 | } else { |
||
98 | $currency = ['suffix' => config('app.currency_symbol')]; |
||
99 | } |
||
100 | |||
101 | CRUD::addColumns([ |
||
102 | |||
103 | [ |
||
104 | 'name' => 'id', |
||
105 | 'label' => 'ID', |
||
106 | ], |
||
107 | |||
108 | [ |
||
109 | 'label' => __('ID number'), |
||
110 | 'type' => 'text', |
||
111 | 'name' => 'student.idnumber', |
||
112 | ], |
||
113 | |||
114 | [ |
||
115 | 'name' => 'user', |
||
116 | 'key' => 'user_lastname', |
||
117 | 'attribute' => 'lastname', |
||
118 | 'label' => __('Last Name'), |
||
119 | 'type' => 'relationship', |
||
120 | 'searchLogic' => function ($query, $column, $searchTerm) { |
||
121 | $query->orWhereHas('student', function ($q) use ($searchTerm) { |
||
122 | $q->whereHas('user', function ($q) use ($searchTerm) { |
||
123 | $q->where('lastname', 'like', '%'.$searchTerm.'%'); |
||
124 | }); |
||
125 | }); |
||
126 | }, |
||
127 | ], |
||
128 | |||
129 | [ |
||
130 | 'name' => 'user', |
||
131 | 'key' => 'user_firstname', |
||
132 | 'attribute' => 'firstname', |
||
133 | 'label' => __('First Name'), |
||
134 | 'type' => 'relationship', |
||
135 | 'searchLogic' => function ($query, $column, $searchTerm) { |
||
136 | $query->orWhereHas('student', function ($q) use ($searchTerm) { |
||
137 | $q->whereHas('user', function ($q) use ($searchTerm) { |
||
138 | $q->where('firstname', 'like', '%'.$searchTerm.'%'); |
||
139 | }); |
||
140 | }); |
||
141 | }, |
||
142 | ], |
||
143 | |||
144 | [ |
||
145 | 'label' => __('Age'), |
||
146 | 'name' => 'student_age', |
||
147 | ], |
||
148 | |||
149 | [ |
||
150 | 'label' => __('Birthdate'), |
||
151 | 'name' => 'student_birthdate', |
||
152 | ], |
||
153 | ]); |
||
154 | |||
155 | if ($this->mode === 'global') { |
||
156 | CRUD::addColumns([[// COURSE NAME |
||
157 | 'label' => __('Course'), // Table column heading |
||
158 | 'type' => 'select', 'name' => 'course_id', // the column that contains the ID of that connected entity; |
||
159 | 'entity' => 'course', // the method that defines the relationship in your Model |
||
160 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
161 | 'model' => Course::class, // foreign key model |
||
162 | ], |
||
163 | |||
164 | ['type' => 'relationship', 'name' => 'course.period', 'label' => __('Period'), 'attribute' => 'name'], ]); |
||
165 | } |
||
166 | |||
167 | CRUD::addColumns([ |
||
168 | [ |
||
169 | // STATUS |
||
170 | 'label' => __('Status'), // Table column heading |
||
171 | 'type' => 'select', |
||
172 | 'name' => 'status_id', // the column that contains the ID of that connected entity; |
||
173 | 'entity' => 'enrollmentStatus', // the method that defines the relationship in your Model |
||
174 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
175 | 'model' => EnrollmentStatusType::class, // foreign key model |
||
176 | ], |
||
177 | |||
178 | array_merge([ |
||
179 | 'name' => 'balance', |
||
180 | 'label' => __('Balance'), |
||
181 | 'type' => 'number', |
||
182 | ], |
||
183 | $currency), |
||
184 | ]); |
||
185 | |||
186 | if (config('invoicing.allow_scheduled_payments')) { |
||
187 | CRUD::addColumn([ |
||
188 | 'name' => 'scheduledPayments', // name of relationship method in the model |
||
189 | 'type' => 'relationship', 'label' => __('Scheduled Payments'), // OPTIONAL |
||
190 | 'attribute' => 'date', 'model' => ScheduledPayment::class, // foreign key model |
||
191 | ]); |
||
192 | } |
||
193 | |||
194 | CRUD::addColumns([ |
||
195 | |||
196 | [ |
||
197 | // any type of relationship |
||
198 | 'name' => 'scholarships', // name of relationship method in the model |
||
199 | 'type' => 'relationship', |
||
200 | 'label' => __('Scholarship'), |
||
201 | // OPTIONAL |
||
202 | // 'entity' => 'tags', // the method that defines the relationship in your Model |
||
203 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
204 | 'model' => Scholarship::class, // foreign key model |
||
205 | ], |
||
206 | |||
207 | [ |
||
208 | 'label' => __('Email'), // Table column heading |
||
209 | 'name' => 'user', |
||
210 | 'attribute' => 'email', |
||
211 | 'type' => 'relationship', |
||
212 | ], |
||
213 | |||
214 | [ |
||
215 | 'label' => __('Phone number'), // Table column heading |
||
216 | 'type' => 'select_multiple', |
||
217 | 'name' => 'student.phone', // the method that defines the relationship in your Model |
||
218 | 'attribute' => 'phone_number', // foreign key attribute that is shown to user |
||
219 | 'model' => PhoneNumber::class, // foreign key model |
||
220 | ], |
||
221 | |||
222 | ]); |
||
223 | |||
224 | if ($this->mode === 'global') { |
||
225 | CRUD::addFilter([ |
||
226 | 'name' => 'status_id', |
||
227 | 'type' => 'select2_multiple', |
||
228 | 'label'=> __('Status'), |
||
229 | ], fn () => EnrollmentStatusType::all()->pluck('name', 'id')->toArray(), |
||
230 | function ($values) { |
||
231 | // if the filter is active |
||
232 | foreach (json_decode($values, null, 512, JSON_THROW_ON_ERROR) as $value) { |
||
233 | CRUD::addClause('orWhere', 'status_id', $value); |
||
234 | } |
||
235 | }); |
||
236 | |||
237 | CRUD::addFilter([ |
||
238 | 'name' => 'period_id', |
||
239 | 'type' => 'select2', |
||
240 | 'label'=> __('Period'), |
||
241 | ], fn () => Period::all()->pluck('name', 'id')->toArray(), function ($value) { |
||
242 | // if the filter is active |
||
243 | CRUD::addClause('period', $value); |
||
244 | }); |
||
245 | |||
246 | CRUD::addFilter( |
||
247 | [ |
||
248 | 'name' => 'scholarship', |
||
249 | 'type' => 'select2', |
||
250 | 'label'=> __('Scholarship'), |
||
251 | ], |
||
252 | fn () => Scholarship::all()->pluck('name', 'id')->toArray(), |
||
253 | function ($value) { // if the filter is active |
||
254 | if ($value == 'all') { |
||
255 | CRUD::addClause('whereHas', 'scholarships'); |
||
256 | } else { |
||
257 | CRUD::addClause('whereHas', 'scholarships', function ($q) use ($value) { |
||
258 | $q->where('scholarships.id', $value); |
||
259 | }); |
||
355 |