Completed
Push — master ( f9866e...fa91dc )
by Song
02:51
created

MenuController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 138
Duplicated Lines 5.07 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 15
dl 7
loc 138
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 4 1
A index() 0 30 2
A treeView() 0 24 3
A edit() 7 7 1
A form() 0 24 2
A iconHelp() 0 4 1

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 Encore\Admin\Controllers;
4
5
use Encore\Admin\Form;
6
use Encore\Admin\Layout\Column;
7
use Encore\Admin\Layout\Content;
8
use Encore\Admin\Layout\Row;
9
use Encore\Admin\Tree;
10
use Encore\Admin\Widgets\Box;
11
use Illuminate\Routing\Controller;
12
13
class MenuController extends Controller
14
{
15
    use HasResourceActions;
16
17
    /**
18
     * Index interface.
19
     *
20
     * @param Content $content
21
     *
22
     * @return Content
23
     */
24
    public function index(Content $content)
25
    {
26
        return $content
27
            ->header(trans('admin.menu'))
28
            ->description(trans('admin.list'))
29
            ->row(function (Row $row) {
30
                $row->column(6, $this->treeView()->render());
31
32
                $row->column(6, function (Column $column) {
33
                    $form = new \Encore\Admin\Widgets\Form();
34
                    $form->action(admin_base_path('auth/menu'));
35
36
                    $menuModel = config('admin.database.menu_model');
37
                    $permissionModel = config('admin.database.permissions_model');
38
                    $roleModel = config('admin.database.roles_model');
39
40
                    $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());
41
                    $form->text('title', trans('admin.title'))->rules('required');
42
                    $form->icon('icon', trans('admin.icon'))->default('fa-bars')->rules('required')->help($this->iconHelp());
43
                    $form->text('uri', trans('admin.uri'));
44
                    $form->multipleSelect('roles', trans('admin.roles'))->options($roleModel::all()->pluck('name', 'id'));
45
                    if ((new $menuModel())->withPermission()) {
46
                        $form->select('permission', trans('admin.permission'))->options($permissionModel::pluck('name', 'slug'));
47
                    }
48
                    $form->hidden('_token')->default(csrf_token());
49
50
                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
51
                });
52
            });
53
    }
54
55
    /**
56
     * Redirect to edit page.
57
     *
58
     * @param int $id
59
     *
60
     * @return \Illuminate\Http\RedirectResponse
61
     */
62
    public function show($id)
63
    {
64
        return redirect()->route('menu.edit', ['id' => $id]);
65
    }
66
67
    /**
68
     * @return \Encore\Admin\Tree
69
     */
70
    protected function treeView()
71
    {
72
        $menuModel = config('admin.database.menu_model');
73
74
        return $menuModel::tree(function (Tree $tree) {
75
            $tree->disableCreate();
76
77
            $tree->branch(function ($branch) {
78
                $payload = "<i class='fa {$branch['icon']}'></i>&nbsp;<strong>{$branch['title']}</strong>";
79
80
                if (!isset($branch['children'])) {
81
                    if (url()->isValidUrl($branch['uri'])) {
82
                        $uri = $branch['uri'];
83
                    } else {
84
                        $uri = admin_base_path($branch['uri']);
85
                    }
86
87
                    $payload .= "&nbsp;&nbsp;&nbsp;<a href=\"$uri\" class=\"dd-nodrag\">$uri</a>";
88
                }
89
90
                return $payload;
91
            });
92
        });
93
    }
94
95
    /**
96
     * Edit interface.
97
     *
98
     * @param string  $id
99
     * @param Content $content
100
     *
101
     * @return Content
102
     */
103 View Code Duplication
    public function edit($id, Content $content)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        return $content
106
            ->header(trans('admin.menu'))
107
            ->description(trans('admin.edit'))
108
            ->row($this->form()->edit($id));
109
    }
110
111
    /**
112
     * Make a form builder.
113
     *
114
     * @return Form
115
     */
116
    public function form()
117
    {
118
        $menuModel = config('admin.database.menu_model');
119
        $permissionModel = config('admin.database.permissions_model');
120
        $roleModel = config('admin.database.roles_model');
121
122
        $form = new Form(new $menuModel());
123
124
        $form->display('id', 'ID');
125
126
        $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());
127
        $form->text('title', trans('admin.title'))->rules('required');
128
        $form->icon('icon', trans('admin.icon'))->default('fa-bars')->rules('required')->help($this->iconHelp());
129
        $form->text('uri', trans('admin.uri'));
130
        $form->multipleSelect('roles', trans('admin.roles'))->options($roleModel::all()->pluck('name', 'id'));
131
        if ($form->model()->withPermission()) {
132
            $form->select('permission', trans('admin.permission'))->options($permissionModel::pluck('name', 'slug'));
133
        }
134
135
        $form->display('created_at', trans('admin.created_at'));
136
        $form->display('updated_at', trans('admin.updated_at'));
137
138
        return $form;
139
    }
140
141
    /**
142
     * Help message for icon field.
143
     *
144
     * @return string
145
     */
146
    protected function iconHelp()
147
    {
148
        return 'For more icons please see <a href="http://fontawesome.io/icons/" target="_blank">http://fontawesome.io/icons/</a>';
149
    }
150
}
151