Completed
Push — master ( 55ab89...0c5b4e )
by Song
06:39
created

MenuController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 16.13 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 20
loc 124
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 9 1
A edit() 10 10 1
A create() 10 10 1
A update() 0 4 1
A destroy() 0 6 2
A store() 0 4 1
A tree() 0 4 1
A form() 0 17 1
A callout() 0 6 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\Auth\Database\Menu;
6
use Encore\Admin\Auth\Database\Role;
7
use Encore\Admin\Facades\Admin;
8
use Encore\Admin\Form;
9
use Encore\Admin\Layout\Content;
10
use Encore\Admin\Tree;
11
use Encore\Admin\Widgets\Callout;
12
use Illuminate\Routing\Controller;
13
14
class MenuController extends Controller
15
{
16
    /**
17
     * Index interface.
18
     *
19
     * @return Content
20
     */
21
    public function index()
22
    {
23
        return Admin::content(function (Content $content) {
24
            $content->header(trans('admin::lang.menu'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.menu') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
25
            $content->description(trans('admin::lang.list'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.list') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
26
27
            $content->body($this->tree());
28
        });
29
    }
30
31
    /**
32
     * Edit interface.
33
     *
34
     * @param $id
35
     *
36
     * @return Content
37
     */
38 View Code Duplication
    public function edit($id)
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...
39
    {
40
        return Admin::content(function (Content $content) use ($id) {
41
            $content->header(trans('admin::lang.menu'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.menu') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
            $content->description(trans('admin::lang.edit'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.edit') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43
44
            $content->row($this->callout());
45
            $content->row($this->form()->edit($id));
46
        });
47
    }
48
49
    /**
50
     * Create interface.
51
     *
52
     * @return Content
53
     */
54 View Code Duplication
    public function create()
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...
55
    {
56
        return Admin::content(function (Content $content) {
57
            $content->header(trans('admin::lang.menu'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.menu') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
58
            $content->description(trans('admin::lang.create'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.create') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59
60
            $content->row($this->callout());
61
            $content->row($this->form());
62
        });
63
    }
64
65
    /**
66
     * @param $id
67
     *
68
     * @return $this|\Illuminate\Http\RedirectResponse
69
     */
70
    public function update($id)
71
    {
72
        return $this->form()->update($id);
73
    }
74
75
    /**
76
     * @param $id
77
     *
78
     * @return \Illuminate\Http\JsonResponse
79
     */
80
    public function destroy($id)
81
    {
82
        if ($this->form()->destroy($id)) {
83
            return response()->json(['msg' => 'delete success!']);
84
        }
85
    }
86
87
    /**
88
     * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
89
     */
90
    public function store()
91
    {
92
        return $this->form()->store();
93
    }
94
95
    /**
96
     * Make a tree builder.
97
     *
98
     * @return Tree
99
     */
100
    public function tree()
101
    {
102
        return Admin::tree(Menu::class);
103
    }
104
105
    /**
106
     * Make a form builder.
107
     *
108
     * @return Form
109
     */
110
    public function form()
111
    {
112
        return Admin::form(Menu::class, function (Form $form) {
113
            $form->display('id', 'ID');
114
115
            $options = [0 => 'Root'] + Menu::buildSelectOptions();
116
117
            $form->select('parent_id', trans('admin::lang.parent_id'))->options($options);
118
            $form->text('title', trans('admin::lang.title'))->rules('required');
119
            $form->text('icon', trans('admin::lang.icon'))->default('fa-bars')->rules('required');
120
            $form->text('uri', trans('admin::lang.uri'));
121
            $form->multipleSelect('roles', trans('admin::lang.roles'))->options(Role::all()->pluck('name', 'id'));
122
123
            $form->display('created_at', trans('admin::lang.created_at'));
124
            $form->display('updated_at', trans('admin::lang.updated_at'));
125
        });
126
    }
127
128
    /**
129
     * @return Callout
130
     */
131
    protected function callout()
132
    {
133
        $text = 'For icons see <a href="http://fontawesome.io/icons/" target="_blank">http://fontawesome.io/icons/</a>';
134
135
        return new Callout($text, 'Tips', 'info');
136
    }
137
}
138