| Total Complexity | 73 |
| Total Lines | 402 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 1 |
Complex classes like UsersController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UsersController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class UsersController extends Controller |
||
| 9 | { |
||
| 10 | public function index() |
||
| 11 | { |
||
| 12 | $users = $this->load->model('User')->users(); |
||
| 13 | $usersformatted = $this->formatUsers($users); |
||
| 14 | $countries = $this->countries('all', 'name'); |
||
| 15 | |||
| 16 | $context = [ |
||
| 17 | 'users' => $usersformatted, |
||
| 18 | 'countries' => $countries, |
||
| 19 | ]; |
||
| 20 | return $this->view->render('admin/pages/users/users', $context); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function row() |
||
| 24 | { |
||
| 25 | $id = userId(); |
||
| 26 | $model = $this->load->model('User'); |
||
| 27 | $user = $model->user($id); |
||
| 28 | |||
| 29 | $user->new = $this->isUserNew($user->registration); |
||
| 30 | $user->registration = $this->changeFormatDate($user->registration); |
||
| 31 | $user->last_login = $this->changeFormatDate($user->last_login); |
||
| 32 | $user->last_logout = $this->changeFormatDate($user->last_logout); |
||
| 33 | $user->birthday = $this->changeFormatDate($user->birthday, ['Y-m-d', 'd M Y']); |
||
| 34 | $user->country_icon = $this->countries($user->country); |
||
| 35 | |||
| 36 | $countries = $this->countries('all', 'name'); |
||
| 37 | $countries_options = implode(',', $countries); |
||
| 38 | |||
| 39 | $context = [ |
||
| 40 | 'user' => $user, |
||
| 41 | 'countries_options' => $countries_options, |
||
| 42 | ]; |
||
| 43 | return $this->view->render('admin/pages/users/user', $context); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function formatUsers($users) |
||
| 59 | } |
||
| 60 | |||
| 61 | public function filter() |
||
| 185 | } |
||
| 186 | |||
| 187 | public function update() |
||
| 188 | { |
||
| 189 | $msg = null; |
||
| 190 | $id = userId(); |
||
| 191 | |||
| 192 | if (!$this->isUserNotFound($id)) { |
||
| 193 | $msg = 'reload'; |
||
| 194 | return json_encode($msg); |
||
| 195 | } |
||
| 196 | $posts = $this->request->posts(); |
||
| 197 | $name = array_keys($posts)[0]; |
||
| 198 | $allows = $this->file->call('config/admin/users/pages/update.php'); |
||
| 199 | |||
| 200 | if (!in_array($name, $allows)) { |
||
| 201 | $msg = 'reload'; |
||
| 202 | return json_encode($msg); |
||
| 203 | } |
||
| 204 | $columns = $this->file->fileContent('config/admin/users/columns.json'); |
||
| 205 | $columns = json_decode($columns); |
||
| 206 | $table = $columns->$name->table; |
||
| 207 | $column = $columns->$name; |
||
| 208 | $filters = $columns->$name->filters; |
||
| 209 | $value = ($posts[$name] == '') ? null : isset($filters->date) ? date('Y-m-d', strtotime($posts[$name])) : $posts[$name]; |
||
| 210 | $user_id_table_name = $column->user_id_table_name; |
||
| 211 | |||
| 212 | if ($this->isValuesAreTheSame($name, $table, $user_id_table_name, $id, $value)) { |
||
| 213 | $msg['same'] = $value ? strtolower($value) : ''; |
||
| 214 | return json_encode($msg); |
||
| 215 | } |
||
| 216 | if ($this->validatorFails($filters, $name)) { |
||
| 217 | $msg['error'] = $this->validator->getErrors(); |
||
| 218 | return json_encode($msg); |
||
| 219 | } |
||
| 220 | if (!$this->updateUser($name, $value, $user_id_table_name, $id, $table)) { |
||
| 221 | $msg = 'reload'; |
||
| 222 | return json_encode($msg); |
||
| 223 | } |
||
| 224 | $msg = $this->userUpdateMsg($name, $value, $filters); |
||
| 225 | return json_encode($msg); |
||
| 226 | } |
||
| 227 | |||
| 228 | private function userUpdateMsg($name, $value, $filters) |
||
| 229 | { |
||
| 230 | if ($name === 'country') { |
||
| 231 | $msg['country'] = [ |
||
|
|
|||
| 232 | $value => $this->countries($value), |
||
| 233 | ]; |
||
| 234 | } else { |
||
| 235 | $msg['text'] = isset($filters->date) ? $this->changeFormatDate($value, ['Y-m-d', 'd M Y']) : _e($value); |
||
| 236 | } |
||
| 237 | return $msg; |
||
| 238 | } |
||
| 239 | |||
| 240 | private function updateUser($name, $value, $user_id_table_name, $id, $table) |
||
| 241 | { |
||
| 242 | return $this->db->data($name, $value)->where($user_id_table_name . ' = ?', $id)->update($table); |
||
| 243 | } |
||
| 244 | |||
| 245 | private function isUserNotFound($id) |
||
| 246 | { |
||
| 247 | return $this->load->model('User')->get($id); |
||
| 248 | } |
||
| 249 | |||
| 250 | private function isValuesAreTheSame($name, $table, $user_id_table_name, $id, $value) |
||
| 251 | { |
||
| 252 | $current_value = $this->db->select($name)->from($table)->where($user_id_table_name . ' = ?', [$id])->fetch()->$name; |
||
| 253 | if (($current_value === strtolower($value)) || ($value == null && $current_value == null)) { |
||
| 254 | return true; |
||
| 255 | } |
||
| 256 | return false; |
||
| 257 | } |
||
| 258 | |||
| 259 | private function validatorFails($filters, $name) |
||
| 260 | { |
||
| 261 | foreach ($filters as $func => $arg) { |
||
| 262 | if (method_exists($this->validator, $func) == 1) { |
||
| 263 | if (gettype($arg) === 'boolean') { |
||
| 264 | if ($arg) { |
||
| 265 | $this->validator->input($name)->$func(); |
||
| 266 | } |
||
| 267 | } else { |
||
| 268 | $this->validator->input($name)->$func($arg); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | return $this->validator->fails(); |
||
| 273 | } |
||
| 274 | |||
| 275 | public function new() |
||
| 276 | { |
||
| 277 | $countries = $this->countries('all', 'name'); |
||
| 278 | $context = [ |
||
| 279 | 'countries' => $countries, |
||
| 280 | ]; |
||
| 281 | return $this->view->render('admin/pages/users/new', $context); |
||
| 282 | } |
||
| 283 | |||
| 284 | public function add() |
||
| 380 | } |
||
| 381 | |||
| 382 | private function isUserNew($date) |
||
| 383 | { |
||
| 384 | if (!$date) { |
||
| 385 | return; |
||
| 386 | } |
||
| 387 | |||
| 412 |