Conditions | 25 |
Paths | 204 |
Total Lines | 130 |
Code Lines | 97 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
94 | public function edit(Request $request) |
||
95 | { |
||
96 | $this->setAdminPrefs(); |
||
97 | |||
98 | $user = [ |
||
99 | 'id' => '', |
||
100 | 'username' => '', |
||
101 | 'email' => '', |
||
102 | 'password' => '', |
||
103 | 'role' => User::ROLE_USER, |
||
104 | 'notes' => '', |
||
105 | 'rate_limit' => 60, |
||
106 | ]; |
||
107 | |||
108 | $meta_title = $title = 'View User'; |
||
109 | |||
110 | // set the current action |
||
111 | $action = $request->input('action') ?? 'view'; |
||
112 | |||
113 | // get the user roles |
||
114 | $userRoles = Role::cursor()->remember(); |
||
115 | $roles = []; |
||
116 | $defaultRole = 'User'; |
||
117 | $defaultInvites = Invitation::DEFAULT_INVITES; |
||
118 | foreach ($userRoles as $r) { |
||
119 | $roles[$r->id] = $r->name; |
||
120 | if ($r->isdefault === 1) { |
||
121 | $defaultRole = $r->id; |
||
122 | $defaultInvites = $r->defaultinvites; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | switch ($action) { |
||
127 | case 'add': |
||
128 | $user += [ |
||
129 | 'role' => $defaultRole, |
||
130 | 'notes' => '', |
||
131 | 'invites' => $defaultInvites, |
||
132 | 'movieview' => 0, |
||
133 | 'xxxview' => 0, |
||
134 | 'musicview' => 0, |
||
135 | 'consoleview' => 0, |
||
136 | 'gameview' => 0, |
||
137 | 'bookview' => 0, |
||
138 | ]; |
||
139 | $this->smarty->assign('user', $user); |
||
140 | break; |
||
141 | case 'submit': |
||
142 | if (empty($request->input('id'))) { |
||
143 | $invites = $defaultInvites; |
||
144 | foreach ($userRoles as $role) { |
||
145 | if ($role['id'] === $request->input('role')) { |
||
146 | $invites = $role['defaultinvites']; |
||
147 | } |
||
148 | } |
||
149 | $ret = User::signUp($request->input('username'), $request->input('password'), $request->input('email'), '', $request->input('notes'), $invites, '', true, $request->input('role'), false); |
||
150 | $this->smarty->assign('role', $request->input('role')); |
||
151 | } else { |
||
152 | $editedUser = User::find($request->input('id')); |
||
153 | $ret = User::updateUser($editedUser->id, $request->input('username'), $request->input('email'), $request->input('grabs'), $request->input('role'), $request->input('notes'), $request->input('invites'), ($request->has('movieview') ? 1 : 0), ($request->has('musicview') ? 1 : 0), ($request->has('gameview') ? 1 : 0), ($request->has('xxxview') ? 1 : 0), ($request->has('consoleview') ? 1 : 0), ($request->has('bookview') ? 1 : 0)); |
||
154 | if ($request->input('password') !== null) { |
||
155 | User::updatePassword($editedUser->id, $request->input('password')); |
||
156 | } |
||
157 | if ($request->input('rolechangedate') !== null) { |
||
158 | User::updateUserRoleChangeDate($editedUser->id, $request->input('rolechangedate')); |
||
159 | } |
||
160 | if ($request->input('role') !== null) { |
||
161 | $editedUser->refresh(); |
||
162 | SendAccountChangedEmail::dispatch($editedUser)->onQueue('emails'); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | if ($ret >= 0) { |
||
167 | return redirect()->to('admin/user-list'); |
||
168 | } |
||
169 | |||
170 | switch ($ret) { |
||
171 | case User::ERR_SIGNUP_BADUNAME: |
||
172 | $this->smarty->assign('error', 'Bad username. Try a better one.'); |
||
173 | break; |
||
174 | case User::ERR_SIGNUP_BADPASS: |
||
175 | $this->smarty->assign('error', 'Bad password. Try a longer one.'); |
||
176 | break; |
||
177 | case User::ERR_SIGNUP_BADEMAIL: |
||
178 | $this->smarty->assign('error', 'Bad email.'); |
||
179 | break; |
||
180 | case User::ERR_SIGNUP_UNAMEINUSE: |
||
181 | $this->smarty->assign('error', 'Username in use.'); |
||
182 | break; |
||
183 | case User::ERR_SIGNUP_EMAILINUSE: |
||
184 | $this->smarty->assign('error', 'Email in use.'); |
||
185 | break; |
||
186 | default: |
||
187 | $this->smarty->assign('error', 'Unknown save error.'); |
||
188 | break; |
||
189 | } |
||
190 | $user += [ |
||
191 | 'id' => $request->input('id'), |
||
192 | 'username' => $request->input('username'), |
||
193 | 'email' => $request->input('email'), |
||
194 | 'role' => $request->input('role'), |
||
195 | 'notes' => $request->input('notes'), |
||
196 | ]; |
||
197 | $this->smarty->assign('user', $user); |
||
198 | break; |
||
199 | case 'view': |
||
200 | default: |
||
201 | if ($request->has('id')) { |
||
202 | $title = 'User Edit'; |
||
203 | $id = $request->input('id'); |
||
204 | $user = User::find($id); |
||
205 | |||
206 | $this->smarty->assign('user', $user); |
||
207 | } |
||
208 | |||
209 | break; |
||
210 | } |
||
211 | |||
212 | $this->smarty->assign('yesno_ids', [1, 0]); |
||
213 | $this->smarty->assign('yesno_names', ['Yes', 'No']); |
||
214 | |||
215 | $this->smarty->assign('role_ids', array_keys($roles)); |
||
216 | $this->smarty->assign('role_names', $roles); |
||
217 | $this->smarty->assign('user', $user); |
||
218 | |||
219 | $content = $this->smarty->fetch('user-edit.tpl'); |
||
220 | |||
221 | $this->smarty->assign(compact('title', 'meta_title', 'content')); |
||
222 | |||
223 | $this->adminrender(); |
||
224 | } |
||
271 |