Completed
Pull Request — master (#2161)
by
unknown
03:01
created

UserController::update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 1
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\Facades\Admin;
9
use Encore\Admin\Form;
10
use Encore\Admin\Grid;
11
use Encore\Admin\Layout\Content;
12
use Illuminate\Routing\Controller;
13
14
class UserController extends Controller
15
{
16
    use ModelForm;
17
18
    /**
19
     * Index interface.
20
     *
21
     * @return Content
22
     */
23 View Code Duplication
    public function index()
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...
24
    {
25
        return Admin::content(function (Content $content) {
26
            $content->header(trans('admin.administrator'));
27
            $content->description(trans('admin.list'));
28
            $content->body($this->grid()->render());
29
        });
30
    }
31
32
    /**
33
     * Edit interface.
34
     *
35
     * @param $id
36
     *
37
     * @return Content
38
     */
39
    public function edit($id)
40
    {
41
        return Admin::content(function (Content $content) use ($id) {
42
            $content->header(trans('admin.administrator'));
43
            $content->description(trans('admin.edit'));
44
            $content->body($this->form()->edit($id));
45
        });
46
    }
47
48
    /**
49
     * Create interface.
50
     *
51
     * @return Content
52
     */
53 View Code Duplication
    public function create()
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 Admin::content(function (Content $content) {
56
            $content->header(trans('admin.administrator'));
57
            $content->description(trans('admin.create'));
58
            $content->body($this->form());
59
        });
60
    }
61
62
    /**
63
     * Make a grid builder.
64
     *
65
     * @return Grid
66
     */
67 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...
68
    {
69
        return Administrator::grid(function (Grid $grid) {
70
            $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...
71
            $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...
72
            $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...
73
            $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...
74
            $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...
75
            $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...
76
77
            $grid->actions(function (Grid\Displayers\Actions $actions) {
78
                if ($actions->getKey() == 1) {
79
                    $actions->disableDelete();
80
                }
81
            });
82
83
            $grid->tools(function (Grid\Tools $tools) {
84
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
85
                    $actions->disableDelete();
86
                });
87
            });
88
        });
89
    }
90
91
    /**
92
     * Make a form builder.
93
     *
94
     * @return Form
95
     */
96
    public function form()
97
    {
98
        return Administrator::form(function (Form $form) {
99
            $form->display('id', 'ID');
100
101
            $form->text('username', trans('admin.username'))->rules('required');
102
            $form->text('name', trans('admin.name'))->rules('required');
103
            $form->image('avatar', trans('admin.avatar'));
104
            $form->password('password', trans('admin.password'))->rules('required|confirmed');
105
            $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
106
                ->default(function ($form) {
107
                    return $form->model()->password;
108
                });
109
110
            $form->ignore(['password_confirmation']);
111
112
            $form->multipleSelect('roles', trans('admin.roles'))->options(Role::all()->pluck('name', 'id'));
113
            $form->multipleSelect('permissions', trans('admin.permissions'))->options(Permission::all()->pluck('name', 'id'));
114
115
            $form->display('created_at', trans('admin.created_at'));
116
            $form->display('updated_at', trans('admin.updated_at'));
117
118 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...
119
                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...
120
                    $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...
121
                }
122
            });
123
        });
124
    }
125
126
    /**
127
     * Update the specified resource in storage.
128
     *
129
     * @param int $id
130
     *
131
     * @return \Illuminate\Http\Response
132
     */
133
    public function update($id)
134
    {
135
        if ($id == 1 && Admin::user()->id != 1) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
136
            return back();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return back(); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by Encore\Admin\Controllers\UserController::update of type Illuminate\Http\Response.

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...
137
        }
138
139
        return $this->form()->update($id);
140
    }
141
142
    /**
143
     * Remove the specified resource from storage.
144
     *
145
     * @param int $id
146
     *
147
     * @return \Illuminate\Http\Response
148
     */
149
    public function destroy($id)
150
    {
151
        $ids = explode(',', $id);
152
        if (in_array(1, $ids)) {
153
            return response()->json([
154
                'status'  => false,
155
                'message' => trans('admin.delete_failed'),
156
            ]);
157
        }
158
159 View Code Duplication
        if ($this->form()->destroy($id)) {
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...
160
            return response()->json([
161
                'status'  => true,
162
                'message' => trans('admin.delete_succeeded'),
163
            ]);
164
        } else {
165
            return response()->json([
166
                'status'  => false,
167
                'message' => trans('admin.delete_failed'),
168
            ]);
169
        }
170
    }
171
}
172