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

src/Controllers/PermissionController.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Controllers;
4
5
use Encore\Admin\Form;
6
use Encore\Admin\Grid;
7
use Encore\Admin\Layout\Content;
8
use Encore\Admin\Show;
9
use Illuminate\Routing\Controller;
10
use Illuminate\Support\Str;
11
12
class PermissionController extends Controller
13
{
14
    use HasResourceActions;
15
16
    /**
17
     * Index interface.
18
     *
19
     * @param Content $content
20
     *
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
     *
37
     * @return Content
38
     */
39
    public function show($id, Content $content)
40
    {
41
        return $content
42
            ->header(trans('admin.permissions'))
43
            ->description(trans('admin.detail'))
44
            ->body($this->detail($id));
45
    }
46
47
    /**
48
     * Edit interface.
49
     *
50
     * @param $id
51
     * @param Content $content
52
     *
53
     * @return Content
54
     */
55 View Code Duplication
    public function edit($id, Content $content)
56
    {
57
        return $content
58
            ->header(trans('admin.permissions'))
59
            ->description(trans('admin.edit'))
60
            ->body($this->form()->edit($id));
61
    }
62
63
    /**
64
     * Create interface.
65
     *
66
     * @param Content $content
67
     *
68
     * @return Content
69
     */
70
    public function create(Content $content)
71
    {
72
        return $content
73
            ->header(trans('admin.permissions'))
74
            ->description(trans('admin.create'))
75
            ->body($this->form());
76
    }
77
78
    /**
79
     * Make a grid builder.
80
     *
81
     * @return Grid
82
     */
83
    protected function grid()
84
    {
85
        $permissionModel = config('admin.database.permissions_model');
86
87
        $grid = new Grid(new $permissionModel());
88
89
        $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...
90
        $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...
91
        $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...
92
93 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...
94
            return collect(explode("\r\n", $path))->map(function ($path) {
95
                $method = $this->http_method ?: ['ANY'];
96
97
                if (Str::contains($path, ':')) {
98
                    list($method, $path) = explode(':', $path);
99
                    $method = explode(',', $method);
100
                }
101
102
                $method = collect($method)->map(function ($name) {
103
                    return strtoupper($name);
104
                })->map(function ($name) {
105
                    return "<span class='label label-primary'>{$name}</span>";
106
                })->implode('&nbsp;');
107
108
                if (!empty(config('admin.route.prefix'))) {
109
                    $path = '/'.trim(config('admin.route.prefix'), '/').$path;
110
                }
111
112
                return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
113
            })->implode('');
114
        });
115
116
        $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...
117
        $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...
118
119
        $grid->tools(function (Grid\Tools $tools) {
120
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
121
                $actions->disableDelete();
122
            });
123
        });
124
125
        return $grid;
126
    }
127
128
    /**
129
     * Make a show builder.
130
     *
131
     * @param mixed $id
132
     *
133
     * @return Show
134
     */
135
    protected function detail($id)
136
    {
137
        $permissionModel = config('admin.database.permissions_model');
138
139
        $show = new Show($permissionModel::findOrFail($id));
140
141
        $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...
142
        $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...
143
        $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...
144
145 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...
146
            return collect(explode("\r\n", $path))->map(function ($path) {
147
                $method = $this->http_method ?: ['ANY'];
148
149
                if (Str::contains($path, ':')) {
150
                    list($method, $path) = explode(':', $path);
151
                    $method = explode(',', $method);
152
                }
153
154
                $method = collect($method)->map(function ($name) {
155
                    return strtoupper($name);
156
                })->map(function ($name) {
157
                    return "<span class='label label-primary'>{$name}</span>";
158
                })->implode('&nbsp;');
159
160
                if (!empty(config('admin.route.prefix'))) {
161
                    $path = '/'.trim(config('admin.route.prefix'), '/').$path;
162
                }
163
164
                return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
165
            })->implode('');
166
        });
167
168
        $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...
169
        $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...
170
171
        return $show;
172
    }
173
174
    /**
175
     * Make a form builder.
176
     *
177
     * @return Form
178
     */
179
    public function form()
180
    {
181
        $permissionModel = config('admin.database.permissions_model');
182
183
        $form = new Form(new $permissionModel());
184
185
        $form->display('id', 'ID');
186
187
        $form->text('slug', trans('admin.slug'))->rules('required');
188
        $form->text('name', trans('admin.name'))->rules('required');
189
190
        $form->multipleSelect('http_method', trans('admin.http.method'))
191
            ->options($this->getHttpMethodsOptions())
192
            ->help(trans('admin.all_methods_if_empty'));
193
        $form->textarea('http_path', trans('admin.http.path'));
194
195
        $form->display('created_at', trans('admin.created_at'));
196
        $form->display('updated_at', trans('admin.updated_at'));
197
198
        return $form;
199
    }
200
201
    /**
202
     * Get options of HTTP methods select field.
203
     *
204
     * @return array
205
     */
206
    protected function getHttpMethodsOptions()
207
    {
208
        $permissionModel = config('admin.database.permissions_model');
209
210
        return array_combine($permissionModel::$httpMethods, $permissionModel::$httpMethods);
211
    }
212
}
213