| Total Complexity | 77 |
| Total Lines | 454 |
| 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 | $posts = $this->request->posts(); |
||
| 193 | $name = array_keys($posts)[0]; |
||
| 194 | $allows = $this->file->call('config/admin/users/pages/update.php'); |
||
| 195 | |||
| 196 | $columns = $this->file->fileContent('config/admin/users/columns.json'); |
||
| 197 | $columns = json_decode($columns); |
||
| 198 | $table = $columns->$name->table; |
||
| 199 | $column = $columns->$name; |
||
| 200 | $filters = $columns->$name->filters; |
||
| 201 | $value = ($posts[$name] == '') ? null : isset($filters->date) ? date('Y-m-d', strtotime($posts[$name])) : $posts[$name]; |
||
| 202 | $user_id_table_name = $column->user_id_table_name; |
||
| 203 | |||
| 204 | $methods = $this->updateMethods([ |
||
| 205 | 'id' => $id, |
||
| 206 | 'name' => $name, |
||
| 207 | 'allows' => $allows, |
||
| 208 | 'table' => $table, |
||
| 209 | 'user_id_table_name' => $user_id_table_name, |
||
| 210 | 'value' => $value, |
||
| 211 | 'filters' => $filters, |
||
| 212 | ]); |
||
| 213 | |||
| 214 | $error = $this->checkForErrorsInUpdateMethods($methods); |
||
| 215 | if ($error) { |
||
| 216 | return json_encode($error); |
||
| 217 | } |
||
| 218 | |||
| 219 | $msg = $this->userUpdateMsg($name, $value, $filters); |
||
| 220 | return json_encode($msg); |
||
| 221 | } |
||
| 222 | |||
| 223 | private function checkForErrorsInUpdateMethods($methods) |
||
| 224 | { |
||
| 225 | $msg = null; |
||
| 226 | foreach ($methods as $method => $options) { |
||
| 227 | if (call_user_func_array(array($this, $method), $options[0]) == false) { |
||
| 228 | if (array_keys($options[1])[0] === 'msg') { |
||
| 229 | $msg = array_values($options[1]); |
||
| 230 | } else { |
||
| 231 | if (array_keys($options[1])[0] === 'error') { |
||
| 232 | $msg['error'] = $this->validator->getErrors(); |
||
| 233 | } else { |
||
| 234 | $msg[array_keys($options[1])[0]] = array_values($options[1]); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | return $msg; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | private function updateMethods($args) |
||
| 244 | { |
||
| 245 | extract($args); |
||
| 246 | return [ |
||
| 247 | 'isUserFound' => [ |
||
| 248 | [$id], |
||
| 249 | ['msg' => 'reload'], |
||
| 250 | ], |
||
| 251 | 'checkPostParameters' => [ |
||
| 252 | [$name, $allows], |
||
| 253 | ['msg' => 'reload'], |
||
| 254 | ], |
||
| 255 | 'isValueChanged' => [ |
||
| 256 | [$name, $table, $user_id_table_name, $id, $value], |
||
| 257 | ['same' => $value ? strtolower($value) : ''], |
||
| 258 | ], |
||
| 259 | 'validatorPasses' => [ |
||
| 260 | [$filters, $name], |
||
| 261 | ['error' => ''], |
||
| 262 | ], |
||
| 263 | 'updateUser' => [ |
||
| 264 | [$name, $value, $user_id_table_name, $id, $table], |
||
| 265 | ['msg' => 'reload'], |
||
| 266 | ], |
||
| 267 | ]; |
||
| 268 | } |
||
| 269 | |||
| 270 | private function checkPostParameters($name, $allows) |
||
| 271 | { |
||
| 272 | if (!in_array($name, $allows)) { |
||
| 273 | return false; |
||
| 274 | } |
||
| 275 | return true; |
||
| 276 | } |
||
| 277 | |||
| 278 | private function userUpdateMsg($name, $value, $filters) |
||
| 279 | { |
||
| 280 | $msg = null; |
||
| 281 | |||
| 282 | if ($name === 'country') { |
||
| 283 | $msg['country'] = [ |
||
| 284 | $value => $this->countries($value), |
||
| 285 | ]; |
||
| 286 | } else { |
||
| 287 | $msg['text'] = isset($filters->date) ? $this->changeFormatDate($value, ['Y-m-d', 'd M Y']) : _e($value); |
||
| 288 | } |
||
| 289 | return $msg; |
||
| 290 | } |
||
| 291 | |||
| 292 | private function updateUser($name, $value, $user_id_table_name, $id, $table) |
||
| 293 | { |
||
| 294 | return $this->db->data($name, $value)->where($user_id_table_name . ' = ?', $id)->update($table); |
||
| 295 | } |
||
| 296 | |||
| 297 | private function isUserFound($id) |
||
| 298 | { |
||
| 299 | return $this->load->model('User')->get($id); |
||
| 300 | } |
||
| 301 | |||
| 302 | private function isValueChanged($name, $table, $user_id_table_name, $id, $value) |
||
| 303 | { |
||
| 304 | $current_value = $this->db->select($name)->from($table)->where($user_id_table_name . ' = ?', [$id])->fetch()->$name; |
||
| 305 | if (($current_value === strtolower($value)) || ($value == null && $current_value == null)) { |
||
| 306 | return false; |
||
| 307 | } |
||
| 308 | return true; |
||
| 309 | } |
||
| 310 | |||
| 311 | private function validatorPasses($filters, $name) |
||
| 312 | { |
||
| 313 | foreach ($filters as $func => $arg) { |
||
| 314 | if (method_exists($this->validator, $func) == 1) { |
||
| 315 | if (gettype($arg) === 'boolean') { |
||
| 316 | if ($arg) { |
||
| 317 | $this->validator->input($name)->$func(); |
||
| 318 | } |
||
| 319 | } else { |
||
| 320 | $this->validator->input($name)->$func($arg); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 | return $this->validator->passes(); |
||
| 325 | } |
||
| 326 | |||
| 327 | public function new() |
||
| 328 | { |
||
| 329 | $countries = $this->countries('all', 'name'); |
||
| 330 | $context = [ |
||
| 331 | 'countries' => $countries, |
||
| 332 | ]; |
||
| 333 | return $this->view->render('admin/pages/users/new', $context); |
||
| 334 | } |
||
| 335 | |||
| 336 | public function add() |
||
| 432 | } |
||
| 433 | |||
| 434 | private function isUserNew($date) |
||
| 435 | { |
||
| 436 | if (!$date) { |
||
| 437 | return; |
||
| 438 | } |
||
| 439 | |||
| 440 | $register_year = $this->changeFormatDate($date, ['Y-m-d H:i:s', 'Y']); |
||
| 441 | $register_month = $this->changeFormatDate($date, ['Y-m-d H:i:s', 'm']); |
||
| 442 | $register_day = $this->changeFormatDate($date, ['Y-m-d H:i:s', 'd']); |
||
| 464 |