setupCreateOperation()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 192
Code Lines 122

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 122
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 192
rs 8

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Requests\ExternalCourseRequest as StoreRequest;
6
use App\Http\Requests\ExternalCourseRequest as UpdateRequest;
7
use App\Models\Course;
8
use App\Models\Level;
9
use App\Models\Partner;
10
use App\Models\Period;
11
use App\Models\Rhythm;
12
use App\Models\Room;
13
use App\Models\SchedulePreset;
14
use App\Models\Teacher;
15
use Backpack\CRUD\app\Http\Controllers\CrudController;
16
use Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
17
use Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
18
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
19
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
20
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
21
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
22
23
/**
24
 * Class ExternalCourseCrudController.
25
 * @property-read CrudPanel $crud
26
 */
27
class ExternalCourseCrudController extends CrudController
28
{
29
    use ListOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...perations\ListOperation requires some properties which are not provided by App\Http\Controllers\Adm...nalCourseCrudController: $model, $query, $entity_name_plural
Loading history...
30
    use CreateOperation { store as traitStore; }
0 ignored issues
show
Bug introduced by
The trait Backpack\CRUD\app\Http\C...rations\CreateOperation requires the property $entity_name which is not provided by App\Http\Controllers\Adm...nalCourseCrudController.
Loading history...
31
    use UpdateOperation { update as traitUpdate; }
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...rations\UpdateOperation requires some properties which are not provided by App\Http\Controllers\Adm...nalCourseCrudController: $entity_name, $model
Loading history...
32
    use DeleteOperation;
33
34
    public function __construct()
35
    {
36
        parent::__construct();
37
        $this->middleware('permission:courses.edit');
38
    }
39
40
    public function setup()
41
    {
42
        /*
43
        |--------------------------------------------------------------------------
44
        | CrudPanel Basic Information
45
        |--------------------------------------------------------------------------
46
        */
47
        CRUD::setModel(Course::class);
48
        CRUD::setRoute(config('backpack.base.route_prefix').'/externalcourse');
49
        CRUD::setEntityNameStrings(__('External Course'), __('External Courses'));
50
        CRUD::addClause('external');
51
52
        CRUD::enableExportButtons();
53
    }
54
55
    /*
56
    |--------------------------------------------------------------------------
57
    | CrudPanel Configuration
58
    |--------------------------------------------------------------------------
59
    */
60
61
    protected function setupListOperation()
62
    {
63
        CRUD::setColumns([
64
            [
65
                // RYTHM
66
                'label' => __('Partnership'),
67
                'type' => 'select',
68
                'name' => 'partner_id',
69
                'entity' => 'partner',
70
                'attribute' => 'name',
71
                'model' => Partner::class,
72
            ],
73
74
            [
75
                // RYTHM
76
                'label' => __('Rhythm'),
77
                'type' => 'select',
78
                'name' => 'rhythm_id',
79
                'entity' => 'rhythm',
80
                'attribute' => 'name',
81
                'model' => Rhythm::class,
82
            ],
83
84
            [
85
                // LEVEL
86
                'label' => __('Level'),
87
                'type' => 'select',
88
                'name' => 'level_id',
89
                'entity' => 'level',
90
                'attribute' => 'name',
91
                'model' => Level::class,
92
            ],
93
94
            [
95
                'name' => 'name',
96
                'label' => __('Name'),
97
            ],
98
99
            [
100
                'name' => 'volume',
101
                'label' => __('Volume'),
102
                'suffix' => 'h',
103
                'type' => 'number',
104
            ],
105
106
            [
107
                'name' => 'hourly_price',
108
                'label' => __('Hourly Price'),
109
                'prefix' => '$',
110
                'type' => 'number',
111
            ],
112
113
            [
114
                // TEACHER
115
                'label' => __('Teacher'),
116
                'type' => 'select',
117
                'name' => 'teacher_id',
118
                'entity' => 'teacher',
119
                'attribute' => 'name',
120
                'model' => Teacher::class,
121
                'searchLogic' => false,
122
            ],
123
124
            [
125
                // ROOM
126
                'label' => __('Room'),
127
                'type' => 'select',
128
                'name' => 'room_id',
129
                'entity' => 'room',
130
                'attribute' => 'name',
131
                'model' => Room::class,
132
            ],
133
134
            // COURSE SCHEDULED TIMES
135
            [
136
                'name' => 'times',
137
                'label' => __('Schedule'),
138
                'type' => 'model_function',
139
                'function_name' => 'getCourseTimesAttribute',
140
                'limit' => 150,
141
            ],
142
143
            // HEAD COUNT
144
            [
145
                'name' => 'head_count',
146
                'label' => __('Students'),
147
            ],
148
149
            // HEAD COUNT
150
            [
151
                'name' => 'new_students',
152
                'label' => __('Year Students'),
153
            ],
154
155
            [
156
                'name' => 'start_date',
157
                'label' => __('Start Date'),
158
                'type' => 'date',
159
            ],
160
161
            [
162
                'name' => 'end_date',
163
                'label' => __('End Date'),
164
                'type' => 'date',
165
            ],
166
167
        ]);
168
169
        CRUD::addFilter(
170
            [ // select2 filter
171
                'name' => 'rhythm_id',
172
                'type' => 'select2',
173
                'label' => __('Rhythm'),
174
            ],
175
            fn () => Rhythm::all()->pluck('name', 'id')->toArray(),
176
            function ($value) {
177
                CRUD::addClause('where', 'rhythm_id', $value);
178
            },
179
            function () { // if the filter is NOT active (the GET parameter "checkbox" does not exit)
180
            }
181
        );
182
183
        CRUD::addFilter(
184
            [ // select2 filter
185
                'name' => 'teacher_id',
186
                'type' => 'select2',
187
                'label' => __('Teacher'),
188
            ],
189
            fn () => Teacher::all()->pluck('name', 'id')->toArray(),
190
            function ($value) {
191
                CRUD::addClause('where', 'teacher_id', $value);
192
            },
193
            function () { // if the filter is NOT active (the GET parameter "checkbox" does not exit)
194
            }
195
        );
196
197
        CRUD::addFilter(
198
            [ // select2 filter
199
                'name' => 'level_id',
200
                'type' => 'select2',
201
                'label' => __('Level'),
202
            ],
203
            fn () => Level::all()->pluck('name', 'id')->toArray(),
204
            function ($value) {
205
                CRUD::addClause('where', 'level_id', $value);
206
            },
207
            function () { // if the filter is NOT active (the GET parameter "checkbox" does not exit)
208
            }
209
        );
210
211
        CRUD::addFilter(
212
            [ // select2 filter
213
                'name' => 'period_id',
214
                'type' => 'select2',
215
                'label' => __('Period'),
216
            ],
217
            fn () => Period::all()->pluck('name', 'id')->toArray(),
218
            function ($value) {
219
                CRUD::addClause('where', 'period_id', $value);
220
            },
221
            function () { // if the filter is NOT active (the GET parameter "checkbox" does not exit)
222
                $period = Period::get_default_period()->id;
223
                CRUD::addClause('where', 'period_id', $period);
224
                $this->crud->getRequest()->request->add(['period_id' => $period]); // to make the filter look active
225
            }
226
        );
227
    }
228
229
    protected function setupCreateOperation()
230
    {
231
        CRUD::setValidation(StoreRequest::class);
232
233
        CRUD::addFields([
234
            [
235
                // RYTHM
236
                'label' => __('Partnership'),
237
                'type' => 'select',
238
                'name' => 'partner_id',
239
                'entity' => 'partner',
240
                'attribute' => 'name',
241
                'model' => Partner::class,
242
                'tab' => __('Course info'),
243
            ],
244
245
            [
246
                // RYTHM
247
                'label' => __('Rhythm'),
248
                'type' => 'select',
249
                'name' => 'rhythm_id',
250
                'entity' => 'rhythm',
251
                'attribute' => 'name',
252
                'model' => Rhythm::class,
253
                'tab' => __('Course info'),
254
            ],
255
256
            [
257
                // LEVEL
258
                'label' => __('Level'),
259
                'type' => 'select',
260
                'name' => 'level_id',
261
                'entity' => 'level',
262
                'attribute' => 'name',
263
                'model' => Level::class,
264
                'tab' => __('Course info'),
265
            ],
266
267
            [
268
                'name' => 'name',
269
                'label' => __('Name'),
270
                'tab' => __('Course info'),
271
            ],
272
273
            [
274
                'name' => 'volume',
275
                'label' => __('Volume'),
276
                'suffix' => 'h',
277
                'tab' => __('Course info'),
278
            ],
279
280
            [
281
                'name' => 'hourly_price',
282
                'label' => __('Hourly Price'),
283
                'prefix' => '$',
284
                'tab' => __('Course info'),
285
            ],
286
287
            [
288
                // TEACHER
289
                'label' => __('Teacher'),
290
                'type' => 'select',
291
                'name' => 'teacher_id',
292
                'entity' => 'teacher',
293
                'attribute' => 'name',
294
                'model' => Teacher::class,
295
                'tab' => __('Course info'),
296
            ],
297
298
            [
299
                // ROOM
300
                'label' => __('Room'),
301
                'type' => 'select',
302
                'name' => 'room_id',
303
                'entity' => 'room',
304
                'attribute' => 'name',
305
                'model' => Room::class,
306
                'tab' => __('Course info'),
307
            ],
308
309
            [
310
                // RYTHM
311
                'label' => __('Campus'),
312
                'type' => 'hidden',
313
                'name' => 'campus_id',
314
                'value' => 2,
315
                'tab' => __('Course info'),
316
            ],
317
318
            [
319
                'name' => 'price',
320
                'type' => 'hidden',
321
                'value' => 0,
322
                'tab' => __('Course info'),
323
            ],
324
325
            [
326
                // PERIOD
327
                'label' => __('Period'),
328
                'type' => 'select',
329
                'name' => 'period_id',
330
                'entity' => 'period',
331
                'attribute' => 'name',
332
                'model' => Period::class,
333
                'tab' => __('Schedule'),
334
                'default' => Period::get_enrollments_period()->id,
335
            ],
336
337
            [
338
                'name' => 'start_date',
339
                'label' => __('Start Date'),
340
                'type' => 'date',
341
                'tab' => __('Schedule'),
342
                'default' => Period::get_enrollments_period()->start,
343
            ],
344
345
            [
346
                'name' => 'end_date',
347
                'label' => __('End Date'),
348
                'type' => 'date',
349
                'tab' => __('Schedule'),
350
                'default' => Period::get_enrollments_period()->end,
351
            ],
352
353
            [
354
                'name' => 'head_count',
355
                'label' => __('Head Count'),
356
                'type' => 'number',
357
                'tab' => __('Course info'),
358
            ],
359
360
            [
361
                'name' => 'new_students',
362
                'label' => __('Students to count in year total'),
363
                'type' => 'number',
364
                'tab' => __('Course info'),
365
            ],
366
367
            [   // repeatable
368
                'name' => 'times',
369
                'label' => __('Course Schedule'),
370
                'type' => 'repeatable',
371
                'fields' => [
372
                    [
373
                        'name' => 'day',
374
                        'label' => __('Day'),
375
                        'type' => 'select_from_array',
376
                        'options' => [
377
                            0 => __('Sunday'),
378
                            1 => __('Monday'),
379
                            2 => __('Tuesday'),
380
                            3 => __('Wednesday'),
381
                            4 => __('Thursday'),
382
                            5 => __('Friday'),
383
                            6 => __('Saturday'),
384
                        ],
385
                        'allows_null' => false,
386
                        'default' => 1,
387
                        'wrapper' => ['class' => 'form-group col-md-4'],
388
                    ],
389
                    [
390
                        'name' => 'start',
391
                        'type' => 'time',
392
                        'label' => __('Start'),
393
                        'wrapper' => ['class' => 'form-group col-md-4'],
394
                    ],
395
                    [
396
                        'name' => 'end',
397
                        'type' => 'time',
398
                        'label' => __('End'),
399
                        'wrapper' => ['class' => 'form-group col-md-4'],
400
                    ],
401
                ],
402
                'init_rows' => 0,
403
                'tab' => __('Schedule'),
404
            ],
405
406
            [   // view
407
                'name' => 'custom-ajax-button',
408
                'type' => 'view',
409
                'view' => 'courses/schedule-preset-alert',
410
                'tab' => __('Schedule'),
411
            ],
412
        ]);
413
414
        CRUD::addField([
415
            'name' => 'schedulepreset',
416
            'label' => __('Schedule Preset'),
417
            'type' => 'select_from_array',
418
            'options' => array_column(SchedulePreset::all()->toArray(), 'name', 'presets'),
419
            'allows_null' => true,
420
            'tab' => __('Schedule'),
421
        ]);
422
    }
423
424
    protected function setupUpdateOperation()
425
    {
426
        CRUD::addFields([
427
            [
428
                // RYTHM
429
                'label' => __('Partnership'),
430
                'type' => 'select',
431
                'name' => 'partner_id',
432
                'entity' => 'partner',
433
                'attribute' => 'name',
434
                'model' => Partner::class,
435
                'tab' => __('Course info'),
436
            ],
437
438
            [
439
                // RYTHM
440
                'label' => __('Rhythm'),
441
                'type' => 'select',
442
                'name' => 'rhythm_id',
443
                'entity' => 'rhythm',
444
                'attribute' => 'name',
445
                'model' => Rhythm::class,
446
                'tab' => __('Course info'),
447
            ],
448
449
            [
450
                // LEVEL
451
                'label' => __('Level'),
452
                'type' => 'select',
453
                'name' => 'level_id',
454
                'entity' => 'level',
455
                'attribute' => 'name',
456
                'model' => Level::class,
457
                'tab' => __('Course info'),
458
            ],
459
460
            [
461
                'name' => 'name',
462
                'label' => __('Name'),
463
                'tab' => __('Course info'),
464
            ],
465
466
            [
467
                'name' => 'volume',
468
                'label' => __('Volume'),
469
                'suffix' => 'h',
470
                'tab' => __('Course info'),
471
            ],
472
473
            [
474
                'name' => 'hourly_price',
475
                'label' => __('Hourly Price'),
476
                'prefix' => '$',
477
                'tab' => __('Course info'),
478
            ],
479
480
            [
481
                // TEACHER
482
                'label' => __('Teacher'),
483
                'type' => 'select',
484
                'name' => 'teacher_id',
485
                'entity' => 'teacher',
486
                'attribute' => 'name',
487
                'model' => Teacher::class,
488
                'tab' => __('Course info'),
489
            ],
490
491
            [
492
                // ROOM
493
                'label' => __('Room'),
494
                'type' => 'select',
495
                'name' => 'room_id',
496
                'entity' => 'room',
497
                'attribute' => 'name',
498
                'model' => Room::class,
499
                'tab' => __('Course info'),
500
            ],
501
502
            [
503
                // RYTHM
504
                'label' => __('Campus'),
505
                'type' => 'hidden',
506
                'name' => 'campus_id',
507
                'value' => 2,
508
                'tab' => __('Course info'),
509
            ],
510
511
            [
512
                'name' => 'price',
513
                'type' => 'hidden',
514
                'value' => 0,
515
                'tab' => __('Course info'),
516
            ],
517
518
            [
519
                // PERIOD
520
                'label' => __('Period'),
521
                'type' => 'select',
522
                'name' => 'period_id',
523
                'entity' => 'period',
524
                'attribute' => 'name',
525
                'model' => Period::class,
526
                'tab' => __('Schedule'),
527
                'default' => Period::get_enrollments_period()->id,
528
            ],
529
530
            [
531
                'name' => 'start_date',
532
                'label' => __('Start Date'),
533
                'type' => 'date',
534
                'tab' => __('Schedule'),
535
                'default' => Period::get_enrollments_period()->start,
536
            ],
537
538
            [
539
                'name' => 'end_date',
540
                'label' => __('End Date'),
541
                'type' => 'date',
542
                'tab' => __('Schedule'),
543
                'default' => Period::get_enrollments_period()->end,
544
            ],
545
546
            [
547
                'name' => 'head_count',
548
                'label' => __('Head Count'),
549
                'type' => 'number',
550
                'tab' => __('Course info'),
551
            ],
552
553
            [
554
                'name' => 'new_students',
555
                'label' => __('Students to count in year total'),
556
                'type' => 'number',
557
                'tab' => __('Course info'),
558
            ],
559
560
            [   // repeatable
561
                'name' => 'times',
562
                'label' => __('Course Schedule'),
563
                'type' => 'repeatable',
564
                'fields' => [
565
                    [
566
                        'name' => 'day',
567
                        'label' => __('Day'),
568
                        'type' => 'select_from_array',
569
                        'options' => [
570
                            0 => __('Sunday'),
571
                            1 => __('Monday'),
572
                            2 => __('Tuesday'),
573
                            3 => __('Wednesday'),
574
                            4 => __('Thursday'),
575
                            5 => __('Friday'),
576
                            6 => __('Saturday'),
577
                        ],
578
                        'allows_null' => false,
579
                        'default' => 1,
580
                        'wrapper' => ['class' => 'form-group col-md-4'],
581
                    ],
582
                    [
583
                        'name' => 'start',
584
                        'type' => 'time',
585
                        'label' => __('Start'),
586
                        'wrapper' => ['class' => 'form-group col-md-4'],
587
                    ],
588
                    [
589
                        'name' => 'end',
590
                        'type' => 'time',
591
                        'label' => __('End'),
592
                        'wrapper' => ['class' => 'form-group col-md-4'],
593
                    ],
594
                ],
595
                'init_rows' => 0,
596
                'tab' => __('Schedule'),
597
            ],
598
        ]);
599
600
        CRUD::setValidation(UpdateRequest::class);
601
    }
602
603
    public function update()
604
    {
605
        $course = $this->crud->getCurrentEntry();
606
        $newCourseTimes = collect(json_decode($this->crud->getRequest()->input('times'), null, 512, JSON_THROW_ON_ERROR));
607
        $course->saveCourseTimes($newCourseTimes);
608
609
        // update model
610
        return $this->traitUpdate();
611
    }
612
613
    public function store()
614
    {
615
        // if a schedule preset was applied, use it
616
        if ($this->crud->getRequest()->input('schedulepreset') !== null) {
617
            $courseTimes = collect(json_decode($this->crud->getRequest()->input('schedulepreset'), null, 512, JSON_THROW_ON_ERROR));
618
        } else {
619
            // otherwise, use any user-defined course times
620
            $courseTimes = collect(json_decode($this->crud->getRequest()->input('times'), null, 512, JSON_THROW_ON_ERROR));
621
        }
622
623
        $response = $this->traitStore();
624
        $course = $this->crud->getCurrentEntry();
625
626
        // apply course times to the parent.
627
        $course->saveCourseTimes($courseTimes);
628
629
        return $response;
630
    }
631
}
632