Completed
Push — master ( c8a423...a23ea0 )
by Song
02:21
created

src/Controllers/PermissionController.php (1 issue)

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\Show;
8
use Illuminate\Support\Str;
9
10
class PermissionController extends AdminController
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    protected function title()
16
    {
17
        return trans('admin.permissions');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return trans('admin.permissions'); (Illuminate\Contracts\Tra...lator|string|array|null) is incompatible with the return type of the parent method Encore\Admin\Controllers\AdminController::title of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
18
    }
19
20
    /**
21
     * Make a grid builder.
22
     *
23
     * @return Grid
24
     */
25
    protected function grid()
26
    {
27
        $permissionModel = config('admin.database.permissions_model');
28
29
        $grid = new Grid(new $permissionModel());
30
31
        $grid->column('id', 'ID')->sortable();
32
        $grid->column('slug', trans('admin.slug'));
33
        $grid->column('name', trans('admin.name'));
34
35 View Code Duplication
        $grid->column('http_path', trans('admin.route'))->display(function ($path) {
36
            return collect(explode("\n", $path))->map(function ($path) {
37
                $method = $this->http_method ?: ['ANY'];
38
39
                if (Str::contains($path, ':')) {
40
                    list($method, $path) = explode(':', $path);
41
                    $method = explode(',', $method);
42
                }
43
44
                $method = collect($method)->map(function ($name) {
45
                    return strtoupper($name);
46
                })->map(function ($name) {
47
                    return "<span class='label label-primary'>{$name}</span>";
48
                })->implode('&nbsp;');
49
50
                if (!empty(config('admin.route.prefix'))) {
51
                    $path = '/'.trim(config('admin.route.prefix'), '/').$path;
52
                }
53
54
                return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
55
            })->implode('');
56
        });
57
58
        $grid->column('created_at', trans('admin.created_at'));
59
        $grid->column('updated_at', trans('admin.updated_at'));
60
61
        $grid->tools(function (Grid\Tools $tools) {
62
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
63
                $actions->disableDelete();
64
            });
65
        });
66
67
        return $grid;
68
    }
69
70
    /**
71
     * Make a show builder.
72
     *
73
     * @param mixed $id
74
     *
75
     * @return Show
76
     */
77
    protected function detail($id)
78
    {
79
        $permissionModel = config('admin.database.permissions_model');
80
81
        $show = new Show($permissionModel::findOrFail($id));
82
83
        $show->field('id', 'ID');
84
        $show->field('slug', trans('admin.slug'));
85
        $show->field('name', trans('admin.name'));
86
87 View Code Duplication
        $show->field('http_path', trans('admin.route'))->unescape()->as(function ($path) {
88
            return collect(explode("\r\n", $path))->map(function ($path) {
89
                $method = $this->http_method ?: ['ANY'];
90
91
                if (Str::contains($path, ':')) {
92
                    list($method, $path) = explode(':', $path);
93
                    $method = explode(',', $method);
94
                }
95
96
                $method = collect($method)->map(function ($name) {
97
                    return strtoupper($name);
98
                })->map(function ($name) {
99
                    return "<span class='label label-primary'>{$name}</span>";
100
                })->implode('&nbsp;');
101
102
                if (!empty(config('admin.route.prefix'))) {
103
                    $path = '/'.trim(config('admin.route.prefix'), '/').$path;
104
                }
105
106
                return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
107
            })->implode('');
108
        });
109
110
        $show->field('created_at', trans('admin.created_at'));
111
        $show->field('updated_at', trans('admin.updated_at'));
112
113
        return $show;
114
    }
115
116
    /**
117
     * Make a form builder.
118
     *
119
     * @return Form
120
     */
121
    public function form()
122
    {
123
        $permissionModel = config('admin.database.permissions_model');
124
125
        $form = new Form(new $permissionModel());
126
127
        $form->display('id', 'ID');
128
129
        $form->text('slug', trans('admin.slug'))->rules('required');
130
        $form->text('name', trans('admin.name'))->rules('required');
131
132
        $form->multipleSelect('http_method', trans('admin.http.method'))
133
            ->options($this->getHttpMethodsOptions())
134
            ->help(trans('admin.all_methods_if_empty'));
135
        $form->textarea('http_path', trans('admin.http.path'));
136
137
        $form->display('created_at', trans('admin.created_at'));
138
        $form->display('updated_at', trans('admin.updated_at'));
139
140
        return $form;
141
    }
142
143
    /**
144
     * Get options of HTTP methods select field.
145
     *
146
     * @return array
147
     */
148
    protected function getHttpMethodsOptions()
149
    {
150
        $model = config('admin.database.permissions_model');
151
152
        return array_combine($model::$httpMethods, $model::$httpMethods);
153
    }
154
}
155