Completed
Push — 2.0 ( 2d1915 )
by Nicolas
14:43
created

UserController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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