Total Complexity | 125 |
Total Lines | 678 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ModifierTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ModifierTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | trait ModifierTrait |
||
24 | { |
||
25 | /** |
||
26 | * Insert Data |
||
27 | * |
||
28 | * Method to input data as well as equipping the data in accordance with the fields |
||
29 | * in the destination database table. |
||
30 | * |
||
31 | * @access public |
||
32 | * |
||
33 | * @param array $sets Array of Input Data |
||
34 | * |
||
35 | * @return mixed |
||
36 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
37 | */ |
||
38 | protected function insert(array $sets) |
||
63 | } |
||
64 | |||
65 | // ------------------------------------------------------------------------ |
||
66 | |||
67 | protected function insertOrUpdate(array $sets) |
||
77 | } |
||
78 | |||
79 | // ------------------------------------------------------------------------ |
||
80 | |||
81 | protected function insertMany(array $sets) |
||
82 | { |
||
83 | if (method_exists($this, 'insertRecordSets')) { |
||
84 | foreach ($sets as $set) { |
||
85 | $this->insertRecordSets($set); |
||
86 | if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
||
87 | $set[ 'record_ordering' ] = $this->getRecordOrdering($this->table); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | if (method_exists($this, 'beforeInsertMany')) { |
||
93 | $this->beforeInsertMany($sets); |
||
94 | } |
||
95 | |||
96 | if ($this->qb->table($this->table)->insertBatch($sets)) { |
||
97 | if (method_exists($this, 'afterInsertMany')) { |
||
98 | return $this->afterInsertMany(); |
||
99 | } |
||
100 | |||
101 | return true; |
||
102 | } |
||
103 | |||
104 | return false; |
||
105 | } |
||
106 | |||
107 | // ------------------------------------------------------------------------ |
||
108 | |||
109 | /** |
||
110 | * Update Data |
||
111 | * |
||
112 | * Method to update data as well as equipping the data in accordance with the fields |
||
113 | * in the destination database table. |
||
114 | * |
||
115 | * @access public |
||
116 | * |
||
117 | * @param array $sets Array of Update Data |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | protected function update(array $sets, $where = []) |
||
122 | { |
||
123 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
124 | |||
125 | if (empty($where)) { |
||
126 | if (empty($this->primaryKeys)) { |
||
127 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
128 | } else { |
||
129 | foreach ($this->primaryKeys as $primaryKey) { |
||
130 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
135 | // Reset Primary Keys |
||
136 | $this->primaryKey = 'id'; |
||
137 | $this->primaryKeys = []; |
||
138 | |||
139 | if (method_exists($this, 'updateRecordSets')) { |
||
140 | $this->updateRecordSets($sets); |
||
141 | } |
||
142 | |||
143 | if (method_exists($this, 'beforeUpdate')) { |
||
144 | $sets = $this->beforeUpdate($sets); |
||
145 | } |
||
146 | |||
147 | if (method_exists($this, 'getRecordOrdering')) { |
||
148 | if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
||
149 | $sets[ 'record_ordering' ] = $this->getRecordOrdering($this->table); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | if ($this->qb->table($this->table)->update($sets, $where)) { |
||
154 | |||
155 | if (method_exists($this, 'afterUpdate')) { |
||
156 | return $this->afterUpdate(); |
||
157 | } |
||
158 | |||
159 | return true; |
||
160 | } |
||
161 | |||
162 | return false; |
||
163 | } |
||
164 | |||
165 | // ------------------------------------------------------------------------ |
||
166 | |||
167 | /** |
||
168 | * Find Or Insert |
||
169 | * |
||
170 | * To insert if there is no record before. |
||
171 | * This is very important in insert to pivot table and avoid redundan |
||
172 | * |
||
173 | * @access public |
||
174 | * |
||
175 | * @param array $sets Array of Input Data |
||
176 | * @param array $sets Array of Reference |
||
177 | * |
||
178 | * @return int |
||
179 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
180 | */ |
||
181 | protected function findOrInsert(array $sets, array $reference = null) |
||
182 | { |
||
183 | if ($reference != null) { |
||
184 | // Disini where nya berdasarkan hasil define. |
||
185 | $result = $this->qb->from($this->table)->getWhere($reference); |
||
186 | } else { |
||
187 | $result = $this->qb->from($this->table)->getWhere($sets); |
||
188 | } |
||
189 | |||
190 | if ($result->count() == 0) { |
||
191 | $this->insert($sets); |
||
192 | |||
193 | return $this->db->getLastInsertId(); |
||
194 | } |
||
195 | |||
196 | return $result[ 0 ]->id; |
||
197 | } |
||
198 | |||
199 | // ------------------------------------------------------------------------ |
||
200 | |||
201 | protected function updateOrInsert(array $sets, array $where = []) |
||
227 | } |
||
228 | |||
229 | // ------------------------------------------------------------------------ |
||
230 | |||
231 | protected function updateMany(array $sets) |
||
232 | { |
||
233 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
234 | |||
235 | if (method_exists($this, 'updateRecordSets')) { |
||
236 | foreach ($sets as $set) { |
||
237 | $this->updateRecordSets($set); |
||
238 | |||
239 | if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
||
240 | $set[ 'record_ordering' ] = $this->getRecordOrdering($this->table); |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | if (method_exists($this, 'beforeUpdateMany')) { |
||
246 | $this->beforeUpdateMany($sets); |
||
247 | } |
||
248 | |||
249 | if ($this->qb->table($this->table)->updateBatch($sets, $primaryKey)) { |
||
250 | if (method_exists($this, 'afterUpdateMany')) { |
||
251 | return $this->afterUpdateMany(); |
||
252 | } |
||
253 | |||
254 | return true; |
||
255 | } |
||
256 | |||
257 | return false; |
||
258 | } |
||
259 | |||
260 | // ------------------------------------------------------------------------ |
||
261 | |||
262 | /** |
||
263 | * softDelete many rows from the database table based on sets of ids. |
||
264 | * |
||
265 | * @param array $ids |
||
266 | * |
||
267 | * @return mixed |
||
268 | */ |
||
269 | protected function softDeleteMany(array $ids) |
||
270 | { |
||
271 | $affectedRows = []; |
||
272 | |||
273 | foreach ($ids as $id) { |
||
274 | $affectedRows[ $id ] = $this->softDelete($id); |
||
275 | } |
||
276 | |||
277 | return $affectedRows; |
||
278 | } |
||
279 | |||
280 | // ------------------------------------------------------------------------ |
||
281 | |||
282 | /** |
||
283 | * softDelete |
||
284 | * |
||
285 | * @param $id |
||
286 | * |
||
287 | * @return array|bool |
||
288 | */ |
||
289 | protected function softDelete($id) |
||
290 | { |
||
291 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
292 | |||
293 | $sets = []; |
||
294 | $where = []; |
||
295 | |||
296 | if (empty($this->primaryKeys)) { |
||
297 | $where[ $primaryKey ] = $id; |
||
298 | $sets[ $primaryKey ] = $id; |
||
299 | } elseif (is_array($id)) { |
||
300 | foreach ($this->primaryKeys as $primaryKey) { |
||
301 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
302 | $sets[ $primaryKey ] = $id[ $primaryKey ]; |
||
303 | } |
||
304 | } else { |
||
305 | foreach ($this->primaryKeys as $primaryKey) { |
||
306 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
307 | } |
||
308 | |||
309 | $sets[ reset($this->primaryKeys) ] = $id; |
||
310 | } |
||
311 | |||
312 | // Reset Primary Keys |
||
313 | $this->primaryKey = 'id'; |
||
314 | $this->primaryKeys = []; |
||
315 | |||
316 | if (method_exists($this, 'updateRecordSets')) { |
||
317 | $this->setRecordStatus('DELETED'); |
||
318 | $this->updateRecordSets($sets); |
||
319 | } |
||
320 | |||
321 | if (method_exists($this, 'beforesoftDelete')) { |
||
322 | $this->beforesoftDelete($sets); |
||
323 | } |
||
324 | |||
325 | if ($this->qb->table($this->table)->update($sets, $where)) { |
||
326 | if (method_exists($this, 'aftersoftDelete')) { |
||
327 | return $this->aftersoftDelete(); |
||
328 | } |
||
329 | |||
330 | return true; |
||
331 | } |
||
332 | |||
333 | return false; |
||
334 | } |
||
335 | |||
336 | // ------------------------------------------------------------------------ |
||
337 | |||
338 | protected function softDeleteManyBy(array $ids, $where = []) |
||
339 | { |
||
340 | $affectedRows = []; |
||
341 | |||
342 | foreach ($ids as $id) { |
||
343 | $affectedRows[ $id ] = $this->softDeleteBy($id, $where); |
||
344 | } |
||
345 | |||
346 | return $affectedRows; |
||
347 | } |
||
348 | |||
349 | // ------------------------------------------------------------------------ |
||
350 | |||
351 | protected function softDeleteBy($id, array $where = []) |
||
352 | { |
||
353 | $this->qb->where($where); |
||
354 | |||
355 | return $this->softDelete($id); |
||
356 | } |
||
357 | |||
358 | // ------------------------------------------------------------------------ |
||
359 | |||
360 | protected function deleteMany(array $ids, $force = false) |
||
361 | { |
||
362 | $affectedRows = []; |
||
363 | |||
364 | foreach ($ids as $id) { |
||
365 | $affectedRows[ $id ] = $this->delete($id, $force); |
||
366 | } |
||
367 | |||
368 | return $affectedRows; |
||
369 | } |
||
370 | |||
371 | // ------------------------------------------------------------------------ |
||
372 | |||
373 | protected function delete($id) |
||
405 | } |
||
406 | |||
407 | // ------------------------------------------------------------------------ |
||
408 | |||
409 | protected function deleteManyBy(array $ids, $where = [], $force = false) |
||
410 | { |
||
411 | $affectedRows = []; |
||
412 | |||
413 | foreach ($ids as $id) { |
||
414 | $affectedRows[ $id ] = $this->deleteBy($id, $where, $force); |
||
415 | } |
||
416 | |||
417 | return $affectedRows; |
||
418 | } |
||
419 | |||
420 | protected function deleteBy($id, $where = [], $force = false) |
||
421 | { |
||
422 | $this->qb->where($where); |
||
423 | |||
424 | return $this->delete($id, $force); |
||
425 | } |
||
426 | |||
427 | // ------------------------------------------------------------------------ |
||
428 | |||
429 | protected function restore($id) |
||
430 | { |
||
431 | return $this->publish($id); |
||
432 | } |
||
433 | |||
434 | // ------------------------------------------------------------------------ |
||
435 | |||
436 | protected function publish($id) |
||
437 | { |
||
438 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
439 | |||
440 | $sets = []; |
||
441 | $where = []; |
||
442 | |||
443 | if (empty($this->primaryKeys)) { |
||
444 | $where[ $primaryKey ] = $id; |
||
445 | $sets[ $primaryKey ] = $id; |
||
446 | } elseif (is_array($id)) { |
||
447 | foreach ($this->primaryKeys as $primaryKey) { |
||
448 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
449 | $sets[ $primaryKey ] = $id[ $primaryKey ]; |
||
450 | } |
||
451 | } else { |
||
452 | foreach ($this->primaryKeys as $primaryKey) { |
||
453 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
454 | } |
||
455 | |||
456 | $sets[ reset($this->primaryKeys) ] = $id; |
||
457 | } |
||
458 | |||
459 | // Reset Primary Keys |
||
460 | $this->primaryKey = 'id'; |
||
461 | $this->primaryKeys = []; |
||
462 | |||
463 | if (method_exists($this, 'updateRecordSets')) { |
||
464 | $this->setRecordStatus('PUBLISH'); |
||
465 | $this->updateRecordSets($sets); |
||
466 | } |
||
467 | |||
468 | if (method_exists($this, 'beforePublish')) { |
||
469 | $this->beforePublish($sets); |
||
470 | } |
||
471 | |||
472 | if ($this->qb->table($this->table)->update($sets, $where)) { |
||
473 | if (method_exists($this, 'afterPublish')) { |
||
474 | return $this->afterPublish(); |
||
475 | } |
||
476 | |||
477 | return true; |
||
478 | } |
||
479 | |||
480 | return false; |
||
481 | } |
||
482 | |||
483 | // ------------------------------------------------------------------------ |
||
484 | |||
485 | protected function restoreBy($id, array $where = []) |
||
486 | { |
||
487 | return $this->publishBy($id, $where); |
||
488 | } |
||
489 | |||
490 | // ------------------------------------------------------------------------ |
||
491 | |||
492 | protected function publishBy($id, array $where = []) |
||
493 | { |
||
494 | $this->qb->where($where); |
||
495 | |||
496 | return $this->publish($id); |
||
497 | } |
||
498 | |||
499 | // ------------------------------------------------------------------------ |
||
500 | |||
501 | protected function restoreMany(array $ids) |
||
502 | { |
||
503 | return $this->publishMany($ids); |
||
504 | } |
||
505 | |||
506 | // ------------------------------------------------------------------------ |
||
507 | |||
508 | protected function publishMany(array $ids) |
||
509 | { |
||
510 | $affectedRows = []; |
||
511 | |||
512 | foreach ($ids as $id) { |
||
513 | $affectedRows[ $id ] = $this->publish($id); |
||
514 | } |
||
515 | |||
516 | return $affectedRows; |
||
517 | } |
||
518 | |||
519 | // ------------------------------------------------------------------------ |
||
520 | |||
521 | protected function restoreManyBy(array $ids, $where = []) |
||
522 | { |
||
523 | return $this->publishManyBy($ids, $where); |
||
524 | } |
||
525 | |||
526 | // ------------------------------------------------------------------------ |
||
527 | |||
528 | protected function publishManyBy(array $ids, $where = []) |
||
529 | { |
||
530 | $affectedRows = []; |
||
531 | |||
532 | foreach ($ids as $id) { |
||
533 | $affectedRows[ $id ] = $this->publishBy($id, $where); |
||
534 | } |
||
535 | |||
536 | return $affectedRows; |
||
537 | } |
||
538 | |||
539 | protected function unpublish($id) |
||
540 | { |
||
541 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
542 | |||
543 | $sets = []; |
||
544 | $where = []; |
||
545 | |||
546 | if (empty($this->primaryKeys)) { |
||
547 | $where[ $primaryKey ] = $id; |
||
548 | $sets[ $primaryKey ] = $id; |
||
549 | } elseif (is_array($id)) { |
||
550 | foreach ($this->primaryKeys as $primaryKey) { |
||
551 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
552 | $sets[ $primaryKey ] = $id[ $primaryKey ]; |
||
553 | } |
||
554 | } else { |
||
555 | foreach ($this->primaryKeys as $primaryKey) { |
||
556 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
557 | } |
||
558 | |||
559 | $sets[ reset($this->primaryKeys) ] = $id; |
||
560 | } |
||
561 | |||
562 | // Reset Primary Keys |
||
563 | $this->primaryKey = 'id'; |
||
564 | $this->primaryKeys = []; |
||
565 | |||
566 | if (method_exists($this, 'updateRecordSets')) { |
||
567 | $this->setRecordStatus('UNPUBLISH'); |
||
568 | $this->updateRecordSets($sets); |
||
569 | } |
||
570 | |||
571 | if (method_exists($this, 'beforeUnpublish')) { |
||
572 | $this->beforePublish($sets); |
||
573 | } |
||
574 | |||
575 | if ($this->qb->table($this->table)->update($sets, $where)) { |
||
576 | if (method_exists($this, 'afterUnpublish')) { |
||
577 | return $this->afterPublish(); |
||
578 | } |
||
579 | |||
580 | return true; |
||
581 | } |
||
582 | |||
583 | return false; |
||
584 | } |
||
585 | |||
586 | // ------------------------------------------------------------------------ |
||
587 | |||
588 | protected function unpublishBy($id, array $where = []) |
||
589 | { |
||
590 | $this->qb->where($where); |
||
591 | |||
592 | return $this->publish($id); |
||
593 | } |
||
594 | |||
595 | // ------------------------------------------------------------------------ |
||
596 | |||
597 | protected function unpublishMany(array $ids) |
||
598 | { |
||
599 | $affectedRows = []; |
||
600 | |||
601 | foreach ($ids as $id) { |
||
602 | $affectedRows[ $id ] = $this->publish($id); |
||
603 | } |
||
604 | |||
605 | return $affectedRows; |
||
606 | } |
||
607 | |||
608 | // ------------------------------------------------------------------------ |
||
609 | |||
610 | protected function unpublishManyBy(array $ids, $where = []) |
||
619 | } |
||
620 | |||
621 | protected function archive($id) |
||
622 | { |
||
623 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
624 | |||
625 | $sets = []; |
||
626 | $where = []; |
||
627 | |||
628 | if (empty($this->primaryKeys)) { |
||
629 | $where[ $primaryKey ] = $id; |
||
630 | $sets[ $primaryKey ] = $id; |
||
666 | } |
||
667 | |||
668 | // ------------------------------------------------------------------------ |
||
669 | |||
670 | protected function archiveBy($id, array $where = []) |
||
671 | { |
||
672 | $this->qb->where($where); |
||
673 | |||
674 | return $this->publish($id); |
||
675 | } |
||
676 | |||
677 | // ------------------------------------------------------------------------ |
||
678 | |||
679 | protected function archiveMany(array $ids) |
||
680 | { |
||
681 | $affectedRows = []; |
||
682 | |||
683 | foreach ($ids as $id) { |
||
684 | $affectedRows[ $id ] = $this->publish($id); |
||
685 | } |
||
686 | |||
687 | return $affectedRows; |
||
688 | } |
||
689 | |||
690 | // ------------------------------------------------------------------------ |
||
691 | |||
692 | protected function archiveManyBy(array $ids, $where = []) |
||
701 | } |
||
702 | } |