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

src/Controllers/RoleController.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
9
class RoleController extends AdminController
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected function title()
15
    {
16
        return trans('admin.roles');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return trans('admin.roles'); (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...
17
    }
18
19
    /**
20
     * Make a grid builder.
21
     *
22
     * @return Grid
23
     */
24
    protected function grid()
25
    {
26
        $roleModel = config('admin.database.roles_model');
27
28
        $grid = new Grid(new $roleModel());
29
30
        $grid->column('id', 'ID')->sortable();
31
        $grid->column('slug', trans('admin.slug'));
32
        $grid->column('name', trans('admin.name'));
33
34
        $grid->column('permissions', trans('admin.permission'))->pluck('name')->label();
35
36
        $grid->column('created_at', trans('admin.created_at'));
37
        $grid->column('updated_at', trans('admin.updated_at'));
38
39
        $grid->actions(function (Grid\Displayers\Actions $actions) {
40
            if ($actions->row->slug == 'administrator') {
41
                $actions->disableDelete();
42
            }
43
        });
44
45
        $grid->tools(function (Grid\Tools $tools) {
46
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
47
                $actions->disableDelete();
48
            });
49
        });
50
51
        return $grid;
52
    }
53
54
    /**
55
     * Make a show builder.
56
     *
57
     * @param mixed $id
58
     *
59
     * @return Show
60
     */
61
    protected function detail($id)
62
    {
63
        $roleModel = config('admin.database.roles_model');
64
65
        $show = new Show($roleModel::findOrFail($id));
66
67
        $show->field('id', 'ID');
68
        $show->field('slug', trans('admin.slug'));
69
        $show->field('name', trans('admin.name'));
70
        $show->field('permissions', trans('admin.permissions'))->as(function ($permission) {
71
            return $permission->pluck('name');
72
        })->label();
73
        $show->field('created_at', trans('admin.created_at'));
74
        $show->field('updated_at', trans('admin.updated_at'));
75
76
        return $show;
77
    }
78
79
    /**
80
     * Make a form builder.
81
     *
82
     * @return Form
83
     */
84
    public function form()
85
    {
86
        $permissionModel = config('admin.database.permissions_model');
87
        $roleModel = config('admin.database.roles_model');
88
89
        $form = new Form(new $roleModel());
90
91
        $form->display('id', 'ID');
92
93
        $form->text('slug', trans('admin.slug'))->rules('required');
94
        $form->text('name', trans('admin.name'))->rules('required');
95
        $form->listbox('permissions', trans('admin.permissions'))->options($permissionModel::all()->pluck('name', 'id'));
96
97
        $form->display('created_at', trans('admin.created_at'));
98
        $form->display('updated_at', trans('admin.updated_at'));
99
100
        return $form;
101
    }
102
}
103