Completed
Push — master ( a2dd34...253c8c )
by Song
03:46
created

PermissionController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Controllers;
4
5
use Encore\Admin\Auth\Database\Permission;
6
use Encore\Admin\Form;
7
use Encore\Admin\Grid;
8
use Encore\Admin\Layout\Content;
9
use Encore\Admin\Show;
10
use Illuminate\Routing\Controller;
11
use Illuminate\Support\Str;
12
13
class PermissionController extends Controller
14
{
15
    use HasResourceActions;
16
17
    /**
18
     * Index interface.
19
     *
20
     * @param Content $content
21
     * @return Content
22
     */
23
    public function index(Content $content)
24
    {
25
        return $content
26
            ->header(trans('admin.permissions'))
27
            ->description(trans('admin.list'))
28
            ->body($this->grid()->render());
29
    }
30
31
    /**
32
     * Show interface.
33
     *
34
     * @param mixed   $id
35
     * @param Content $content
36
     * @return Content
37
     */
38
    public function show($id, Content $content)
39
    {
40
        return $content
41
            ->header(trans('admin.permissions'))
42
            ->description(trans('admin.detail'))
43
            ->body($this->detail($id));
44
    }
45
46
    /**
47
     * Edit interface.
48
     *
49
     * @param $id
50
     *
51
     * @param Content $content
52
     * @return Content
53
     */
54 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...
55
    {
56
        return $content
57
            ->header(trans('admin.permissions'))
58
            ->description(trans('admin.edit'))
59
            ->body($this->form()->edit($id));
60
    }
61
62
    /**
63
     * Create interface.
64
     *
65
     * @param Content $content
66
     * @return Content
67
     */
68
    public function create(Content $content)
69
    {
70
        return $content
71
            ->header(trans('admin.permissions'))
72
            ->description(trans('admin.create'))
73
            ->body($this->form());
74
    }
75
76
    /**
77
     * Make a grid builder.
78
     *
79
     * @return Grid
80
     */
81
    protected function grid()
82
    {
83
        $grid = new Grid(new Permission());
84
85
        $grid->id('ID')->sortable();
0 ignored issues
show
Documentation Bug introduced by
The method id does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
        $grid->slug(trans('admin.slug'));
0 ignored issues
show
Documentation Bug introduced by
The method slug does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
87
        $grid->name(trans('admin.name'));
0 ignored issues
show
Documentation Bug introduced by
The method name does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
88
89 View Code Duplication
        $grid->http_path(trans('admin.route'))->display(function ($path) {
0 ignored issues
show
Documentation Bug introduced by
The method http_path does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
90
            return collect(explode("\r\n", $path))->map(function ($path) {
91
                $method = $this->http_method ?: ['ANY'];
0 ignored issues
show
Bug introduced by
The property http_method does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
92
93
                if (Str::contains($path, ':')) {
94
                    list($method, $path) = explode(':', $path);
95
                    $method = explode(',', $method);
96
                }
97
98
                $method = collect($method)->map(function ($name) {
99
                    return strtoupper($name);
100
                })->map(function ($name) {
101
                    return "<span class='label label-primary'>{$name}</span>";
102
                })->implode('&nbsp;');
103
104
                $path = '/'.trim(config('admin.route.prefix'), '/').$path;
105
106
                return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
107
            })->implode('');
108
        });
109
110
        $grid->created_at(trans('admin.created_at'));
0 ignored issues
show
Documentation Bug introduced by
The method created_at does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
111
        $grid->updated_at(trans('admin.updated_at'));
0 ignored issues
show
Documentation Bug introduced by
The method updated_at does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
112
113
        $grid->tools(function (Grid\Tools $tools) {
114
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
115
                $actions->disableDelete();
116
            });
117
        });
118
119
        return $grid;
120
    }
121
122
    /**
123
     * Make a show builder.
124
     *
125
     * @param mixed   $id
126
     * @return Show
127
     */
128
    protected function detail($id)
129
    {
130
        $show = new Show(Permission::findOrFail($id));
131
132
        $show->id('ID');
0 ignored issues
show
Documentation Bug introduced by
The method id does not exist on object<Encore\Admin\Show>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
133
        $show->slug(trans('admin.slug'));
0 ignored issues
show
Documentation Bug introduced by
The method slug does not exist on object<Encore\Admin\Show>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
134
        $show->name(trans('admin.name'));
0 ignored issues
show
Documentation Bug introduced by
The method name does not exist on object<Encore\Admin\Show>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
135
136 View Code Duplication
        $show->http_path(trans('admin.route'))->as(function ($path) {
0 ignored issues
show
Documentation Bug introduced by
The method http_path does not exist on object<Encore\Admin\Show>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
137
            return collect(explode("\r\n", $path))->map(function ($path) {
138
                $method = $this->http_method ?: ['ANY'];
139
140
                if (Str::contains($path, ':')) {
141
                    list($method, $path) = explode(':', $path);
142
                    $method = explode(',', $method);
143
                }
144
145
                $method = collect($method)->map(function ($name) {
146
                    return strtoupper($name);
147
                })->map(function ($name) {
148
                    return "<span class='label label-primary'>{$name}</span>";
149
                })->implode('&nbsp;');
150
151
                $path = '/'.trim(config('admin.route.prefix'), '/').$path;
152
153
                return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
154
            })->implode('');
155
        });
156
157
        $show->created_at(trans('admin.created_at'));
0 ignored issues
show
Documentation Bug introduced by
The method created_at does not exist on object<Encore\Admin\Show>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
158
        $show->updated_at(trans('admin.updated_at'));
0 ignored issues
show
Documentation Bug introduced by
The method updated_at does not exist on object<Encore\Admin\Show>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
159
160
        return $show;
161
    }
162
163
    /**
164
     * Make a form builder.
165
     *
166
     * @return Form
167
     */
168
    public function form()
169
    {
170
        $form = new Form(new Permission());
171
172
        $form->display('id', 'ID');
173
174
        $form->text('slug', trans('admin.slug'))->rules('required');
175
        $form->text('name', trans('admin.name'))->rules('required');
176
177
        $form->multipleSelect('http_method', trans('admin.http.method'))
178
            ->options($this->getHttpMethodsOptions())
179
            ->help(trans('admin.all_methods_if_empty'));
180
        $form->textarea('http_path', trans('admin.http.path'));
181
182
        $form->display('created_at', trans('admin.created_at'));
183
        $form->display('updated_at', trans('admin.updated_at'));
184
185
        return $form;
186
    }
187
188
    /**
189
     * Get options of HTTP methods select field.
190
     *
191
     * @return array
192
     */
193
    protected function getHttpMethodsOptions()
194
    {
195
        return array_combine(Permission::$httpMethods, Permission::$httpMethods);
196
    }
197
}
198