UserController::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Modules\User\Http\Controllers\Admin;
2
3
use Modules\Core\Contracts\Authentication;
4
use Modules\User\Http\Requests\CreateUserRequest;
5
use Modules\User\Http\Requests\UpdateUserRequest;
6
use Modules\User\Permissions\PermissionManager;
7
use Modules\User\Repositories\RoleRepository;
8
use Modules\User\Repositories\UserRepository;
9
10
class UserController extends BaseUserModuleController
11
{
12
    /**
13
     * @var UserRepository
14
     */
15
    private $user;
16
    /**
17
     * @var RoleRepository
18
     */
19
    private $role;
20
    /**
21
     * @var Authentication
22
     */
23
    private $auth;
24
25
    /**
26
     * @param PermissionManager $permissions
27
     * @param UserRepository    $user
28
     * @param RoleRepository    $role
29
     * @param Authentication    $auth
30
     */
31
    public function __construct(
32
        PermissionManager $permissions,
33
        UserRepository $user,
34
        RoleRepository $role,
35
        Authentication $auth
36
    ) {
37
        parent::__construct();
38
39
        $this->permissions = $permissions;
40
        $this->user = $user;
41
        $this->role = $role;
42
        $this->auth = $auth;
43
    }
44
45
    /**
46
     * Display a listing of the resource.
47
     *
48
     * @return Response
49
     */
50
    public function index()
51
    {
52
        $users = $this->user->all();
53
54
        $currentUser = $this->auth->check();
55
56
        return view('user::admin.users.index', compact('users', 'currentUser'));
57
    }
58
59
    /**
60
     * Show the form for creating a new resource.
61
     *
62
     * @return Response
63
     */
64
    public function create()
65
    {
66
        $roles = $this->role->all();
67
68
        return view('user::admin.users.create', compact('roles'));
69
    }
70
71
    /**
72
     * Store a newly created resource in storage.
73
     *
74
     * @param  CreateUserRequest $request
75
     * @return Response
76
     */
77
    public function store(CreateUserRequest $request)
78
    {
79
        $data = $this->mergeRequestWithPermissions($request);
80
81
        $this->user->createWithRoles($data, $request->roles, true);
0 ignored issues
show
Documentation introduced by
The property roles does not exist on object<Modules\User\Http...ests\CreateUserRequest>. 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...
82
83
        flash(trans('user::messages.user created'));
84
85
        return redirect()->route('admin.user.user.index');
86
    }
87
88
    /**
89
     * Show the form for editing the specified resource.
90
     *
91
     * @param  int      $id
92
     * @return Response
93
     */
94
    public function edit($id)
95
    {
96
        if (!$user = $this->user->find($id)) {
97
            flash()->error(trans('user::messages.user not found'));
98
99
            return redirect()->route('admin.user.user.index');
100
        }
101
        $roles = $this->role->all();
102
103
        $currentUser = $this->auth->check();
104
105
        return view('user::admin.users.edit', compact('user', 'roles', 'currentUser'));
106
    }
107
108
    /**
109
     * Update the specified resource in storage.
110
     *
111
     * @param  int               $id
112
     * @param  UpdateUserRequest $request
113
     * @return Response
114
     */
115
    public function update($id, UpdateUserRequest $request)
116
    {
117
        $data = $this->mergeRequestWithPermissions($request);
118
119
        $this->user->updateAndSyncRoles($id, $data, $request->roles);
0 ignored issues
show
Documentation introduced by
The property roles does not exist on object<Modules\User\Http...ests\UpdateUserRequest>. 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
121
        flash(trans('user::messages.user updated'));
122
123
        return redirect()->route('admin.user.user.index');
124
    }
125
126
    /**
127
     * Remove the specified resource from storage.
128
     *
129
     * @param  int      $id
130
     * @return Response
131
     */
132
    public function destroy($id)
133
    {
134
        $this->user->delete($id);
135
136
        flash(trans('user::messages.user deleted'));
137
138
        return redirect()->route('admin.user.user.index');
139
    }
140
}
141