Completed
Pull Request — master (#4037)
by Muhlis
02:38
created

MultipleImageController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 23.81 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 20
loc 84
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Tests\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use Encore\Admin\Controllers\ModelForm;
7
use Encore\Admin\Facades\Admin;
8
use Encore\Admin\Form;
9
use Encore\Admin\Grid;
10
use Encore\Admin\Layout\Content;
11
use Tests\Models\MultipleImage;
12
13
class MultipleImageController extends Controller
14
{
15
    use ModelForm;
16
17
    /**
18
     * Index interface.
19
     *
20
     * @return Content
21
     */
22
    public function index()
23
    {
24
        return Admin::content(function (Content $content) {
25
            $content->header('header');
26
            $content->description('description');
27
28
            $content->body($this->grid());
29
        });
30
    }
31
32
    /**
33
     * Edit interface.
34
     *
35
     * @param $id
36
     *
37
     * @return Content
38
     */
39
    public function edit($id)
40
    {
41
        return Admin::content(function (Content $content) use ($id) {
42
            $content->header('header');
43
            $content->description('description');
44
45
            $content->body($this->form()->edit($id));
46
        });
47
    }
48
49
    /**
50
     * Create interface.
51
     *
52
     * @return Content
53
     */
54
    public function create()
55
    {
56
        return Admin::content(function (Content $content) {
57
            $content->header('Upload image');
58
59
            $content->body($this->form());
60
        });
61
    }
62
63
    /**
64
     * Make a grid builder.
65
     *
66
     * @return Grid
67
     */
68
    protected function grid()
69
    {
70
        return Admin::grid(Image::class, function (Grid $grid) {
71
            $grid->id('ID')->sortable();
72
73
            $grid->created_at();
74
            $grid->updated_at();
75
76
            $grid->disableFilter();
77
        });
78
    }
79
80
    /**
81
     * Make a form builder.
82
     *
83
     * @return Form
84
     */
85
    protected function form()
86
    {
87
        return Admin::form(MultipleImage::class, function (Form $form) {
88
            $form->display('id', 'ID');
89
90
            $form->multipleImage('pictures');
91
92
            $form->display('created_at', 'Created At');
93
            $form->display('updated_at', 'Updated At');
94
        });
95
    }
96
}
97