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

UserController::detail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Controllers;
4
5
use Encore\Admin\Auth\Database\Administrator;
6
use Encore\Admin\Auth\Database\Permission;
7
use Encore\Admin\Auth\Database\Role;
8
use Encore\Admin\Form;
9
use Encore\Admin\Grid;
10
use Encore\Admin\Layout\Content;
11
use Encore\Admin\Show;
12
use Illuminate\Routing\Controller;
13
14
class UserController extends Controller
15
{
16
    use HasResourceActions;
17
18
    /**
19
     * Index interface.
20
     *
21
     * @return Content
22
     */
23
    public function index(Content $content)
24
    {
25
        return $content
26
            ->header(trans('admin.administrator'))
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.administrator'))
42
            ->description(trans('admin.detail'))
43
            ->body($this->detail($id));
44
    }
45
46
    /**
47
     * Edit interface.
48
     *
49
     * @param $id
50
     *
51
     * @return Content
52
     */
53 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...
54
    {
55
        return $content
56
            ->header(trans('admin.administrator'))
57
            ->description(trans('admin.edit'))
58
            ->body($this->form()->edit($id));
59
    }
60
61
    /**
62
     * Create interface.
63
     *
64
     * @return Content
65
     */
66
    public function create(Content $content)
67
    {
68
        return $content
69
            ->header(trans('admin.administrator'))
70
            ->description(trans('admin.create'))
71
            ->body($this->form());
72
    }
73
74
    /**
75
     * Make a grid builder.
76
     *
77
     * @return Grid
78
     */
79 View Code Duplication
    protected function grid()
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...
80
    {
81
        $grid = new Grid(new Administrator());
82
83
        $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...
84
        $grid->username(trans('admin.username'));
0 ignored issues
show
Documentation Bug introduced by
The method username 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...
85
        $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...
86
        $grid->roles(trans('admin.roles'))->pluck('name')->label();
0 ignored issues
show
Documentation Bug introduced by
The method roles 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->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...
88
        $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...
89
90
        $grid->actions(function (Grid\Displayers\Actions $actions) {
91
            if ($actions->getKey() == 1) {
92
                $actions->disableDelete();
93
            }
94
        });
95
96
        $grid->tools(function (Grid\Tools $tools) {
97
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
98
                $actions->disableDelete();
99
            });
100
        });
101
102
        return $grid;
103
    }
104
105
    /**
106
     * Make a show builder.
107
     *
108
     * @param mixed   $id
109
     * @return Show
110
     */
111
    protected function detail($id)
112
    {
113
        $show = new Show(Administrator::findOrFail($id));
114
115
        $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...
116
        $show->username(trans('admin.username'));
0 ignored issues
show
Documentation Bug introduced by
The method username 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...
117
        $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...
118
        $show->roles(trans('admin.roles'))->as(function ($roles) {
0 ignored issues
show
Documentation Bug introduced by
The method roles 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...
119
            return $roles->pluck('name');
120
        })->label();
121
        $show->permissions(trans('admin.permissions'))->as(function ($permission) {
0 ignored issues
show
Documentation Bug introduced by
The method permissions 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...
122
            return $permission->pluck('name');
123
        })->label();
124
        $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...
125
        $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...
126
127
        return $show;
128
    }
129
130
    /**
131
     * Make a form builder.
132
     *
133
     * @return Form
134
     */
135
    public function form()
136
    {
137
        $form = new Form(new Administrator());
138
139
        $form->display('id', 'ID');
140
141
        $form->text('username', trans('admin.username'))->rules('required');
142
        $form->text('name', trans('admin.name'))->rules('required');
143
        $form->image('avatar', trans('admin.avatar'));
144
        $form->password('password', trans('admin.password'))->rules('required|confirmed');
145
        $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
146
            ->default(function ($form) {
147
                return $form->model()->password;
148
            });
149
150
        $form->ignore(['password_confirmation']);
151
152
        $form->multipleSelect('roles', trans('admin.roles'))->options(Role::all()->pluck('name', 'id'));
153
        $form->multipleSelect('permissions', trans('admin.permissions'))->options(Permission::all()->pluck('name', 'id'));
154
155
        $form->display('created_at', trans('admin.created_at'));
156
        $form->display('updated_at', trans('admin.updated_at'));
157
158 View Code Duplication
        $form->saving(function (Form $form) {
0 ignored issues
show
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...
159
            if ($form->password && $form->model()->password != $form->password) {
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<Encore\Admin\Form>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
160
                $form->password = bcrypt($form->password);
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<Encore\Admin\Form>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property password does not exist on object<Encore\Admin\Form>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
161
            }
162
        });
163
164
        return $form;
165
    }
166
}
167