| Total Complexity | 107 |
| Total Lines | 595 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RecordsRequestHandler 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 RecordsRequestHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class RecordsRequestHandler extends RequestHandler |
||
| 13 | { |
||
| 14 | public static $config; |
||
| 15 | |||
| 16 | // configurables |
||
| 17 | public static $recordClass; |
||
| 18 | public static $accountLevelRead = false; |
||
| 19 | public static $accountLevelBrowse = 'Staff'; |
||
| 20 | public static $accountLevelWrite = 'Staff'; |
||
| 21 | public static $accountLevelAPI = false; |
||
| 22 | public static $browseOrder = false; |
||
| 23 | public static $browseConditions = false; |
||
| 24 | public static $browseLimitDefault = false; |
||
| 25 | public static $editableFields = false; |
||
| 26 | public static $searchConditions = false; |
||
| 27 | |||
| 28 | public static $calledClass = __CLASS__; |
||
| 29 | public static $responseMode = 'dwoo'; |
||
| 30 | |||
| 31 | public static function handleRequest() |
||
| 48 | } |
||
| 49 | |||
| 50 | |||
| 51 | public static function handleRecordsRequest($action = false) |
||
| 52 | { |
||
| 53 | switch ($action ? $action : $action = static::shiftPath()) { |
||
| 54 | case 'save': |
||
| 55 | { |
||
| 56 | return static::handleMultiSaveRequest(); |
||
| 57 | } |
||
| 58 | |||
| 59 | case 'destroy': |
||
| 60 | { |
||
| 61 | return static::handleMultiDestroyRequest(); |
||
| 62 | } |
||
| 63 | |||
| 64 | case 'create': |
||
| 65 | { |
||
| 66 | return static::handleCreateRequest(); |
||
| 67 | } |
||
| 68 | |||
| 69 | case '': |
||
| 70 | case false: |
||
| 71 | { |
||
| 72 | return static::handleBrowseRequest(); |
||
| 73 | } |
||
| 74 | |||
| 75 | default: |
||
| 76 | { |
||
| 77 | if ($Record = static::getRecordByHandle($action)) { |
||
| 78 | return static::handleRecordRequest($Record); |
||
| 79 | } else { |
||
| 80 | return static::throwRecordNotFoundError(); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | public static function getRecordByHandle($handle) |
||
| 87 | { |
||
| 88 | $className = static::$recordClass; |
||
| 89 | |||
| 90 | if (method_exists($className, 'getByHandle')) { |
||
| 91 | return $className::getByHandle($handle); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | public static function prepareBrowseConditions($conditions = []) |
||
| 96 | { |
||
| 97 | if (static::$browseConditions) { |
||
| 98 | if (!is_array(static::$browseConditions)) { |
||
| 99 | static::$browseConditions = [static::$browseConditions]; |
||
| 100 | } |
||
| 101 | $conditions = array_merge(static::$browseConditions, $conditions); |
||
| 102 | } |
||
| 103 | return $conditions; |
||
| 104 | } |
||
| 105 | |||
| 106 | public static function prepareDefaultBrowseOptions() |
||
| 107 | { |
||
| 108 | if (empty($_REQUEST['offset']) && is_numeric($_REQUEST['start'])) { |
||
| 109 | $_REQUEST['offset'] = $_REQUEST['start']; |
||
| 110 | } |
||
| 111 | |||
| 112 | $limit = !empty($_REQUEST['limit']) && is_numeric($_REQUEST['limit']) ? $_REQUEST['limit'] : static::$browseLimitDefault; |
||
| 113 | $offset = !empty($_REQUEST['offset']) && is_numeric($_REQUEST['offset']) ? $_REQUEST['offset'] : false; |
||
| 114 | |||
| 115 | $options = [ |
||
| 116 | 'limit' => $limit, |
||
| 117 | 'offset' => $offset, |
||
| 118 | 'order' => static::$browseOrder, |
||
| 119 | ]; |
||
| 120 | |||
| 121 | return $options; |
||
| 122 | } |
||
| 123 | |||
| 124 | public static function handleBrowseRequest($options = [], $conditions = [], $responseID = null, $responseData = []) |
||
| 125 | { |
||
| 126 | if (!static::checkBrowseAccess(func_get_args())) { |
||
| 127 | return static::throwUnauthorizedError(); |
||
| 128 | } |
||
| 129 | |||
| 130 | $conditions = static::prepareBrowseConditions($conditions); |
||
| 131 | |||
| 132 | $options = static::prepareDefaultBrowseOptions(); |
||
| 133 | |||
| 134 | // process sorter |
||
| 135 | if (!empty($_REQUEST['sort'])) { |
||
| 136 | $sort = json_decode($_REQUEST['sort'], true); |
||
| 137 | if (!$sort || !is_array($sort)) { |
||
| 138 | return static::respond('error', [ |
||
| 139 | 'success' => false, |
||
| 140 | 'failed' => [ |
||
| 141 | 'errors' => 'Invalid sorter.', |
||
| 142 | ], |
||
| 143 | ]); |
||
| 144 | } |
||
| 145 | |||
| 146 | if (is_array($sort)) { |
||
|
1 ignored issue
–
show
|
|||
| 147 | foreach ($sort as $field) { |
||
| 148 | $options['order'][$field['property']] = $field['direction']; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | // process filter |
||
| 154 | if (!empty($_REQUEST['filter'])) { |
||
| 155 | $filter = json_decode($_REQUEST['filter'], true); |
||
| 156 | if (!$filter || !is_array($filter)) { |
||
| 157 | return static::respond('error', [ |
||
| 158 | 'success' => false, |
||
| 159 | 'failed' => [ |
||
| 160 | 'errors' => 'Invalid filter.', |
||
| 161 | ], |
||
| 162 | ]); |
||
| 163 | } |
||
| 164 | |||
| 165 | foreach ($filter as $field) { |
||
| 166 | $conditions[$field['property']] = $field['value']; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $className = static::$recordClass; |
||
| 171 | |||
| 172 | return static::respond( |
||
| 173 | isset($responseID) ? $responseID : static::getTemplateName($className::$pluralNoun), |
||
| 174 | array_merge($responseData, [ |
||
| 175 | 'success' => true, |
||
| 176 | 'data' => $className::getAllByWhere($conditions, $options), |
||
| 177 | 'conditions' => $conditions, |
||
| 178 | 'total' => DB::foundRows(), |
||
| 179 | 'limit' => $options['limit'], |
||
| 180 | 'offset' => $options['offset'], |
||
| 181 | ]) |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | |||
| 186 | public static function handleRecordRequest(ActiveRecord $Record, $action = false) |
||
| 187 | { |
||
| 188 | if (!static::checkReadAccess($Record)) { |
||
| 189 | return static::throwUnauthorizedError(); |
||
| 190 | } |
||
| 191 | |||
| 192 | switch ($action ? $action : $action = static::shiftPath()) { |
||
| 193 | case '': |
||
| 194 | case false: |
||
| 195 | { |
||
| 196 | $className = static::$recordClass; |
||
| 197 | |||
| 198 | return static::respond(static::getTemplateName($className::$singularNoun), [ |
||
| 199 | 'success' => true, |
||
| 200 | 'data' => $Record, |
||
| 201 | ]); |
||
| 202 | } |
||
| 203 | |||
| 204 | case 'edit': |
||
| 205 | { |
||
| 206 | return static::handleEditRequest($Record); |
||
| 207 | } |
||
| 208 | |||
| 209 | case 'delete': |
||
| 210 | { |
||
| 211 | return static::handleDeleteRequest($Record); |
||
| 212 | } |
||
| 213 | |||
| 214 | default: |
||
| 215 | { |
||
| 216 | return static::onRecordRequestNotHandled($Record, $action); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | public static function handleMultiSaveRequest() |
||
| 224 | { |
||
| 225 | $className = static::$recordClass; |
||
| 226 | |||
| 227 | $PrimaryKey = $className::getPrimaryKey(); |
||
| 228 | |||
| 229 | if (static::$responseMode == 'json' && in_array($_SERVER['REQUEST_METHOD'], ['POST','PUT'])) { |
||
| 230 | $JSONData = JSON::getRequestData(); |
||
| 231 | if (is_array($JSONData)) { |
||
| 232 | $_REQUEST = $JSONData; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | if ($className::fieldExists(key($_REQUEST['data']))) { |
||
| 237 | $_REQUEST['data'] = [$_REQUEST['data']]; |
||
| 238 | } |
||
| 239 | |||
| 240 | if (empty($_REQUEST['data']) || !is_array($_REQUEST['data'])) { |
||
| 241 | if (static::$responseMode == 'json') { |
||
| 242 | return static::respond('error', [ |
||
| 243 | 'success' => false, |
||
| 244 | 'failed' => [ |
||
| 245 | 'errors' => 'Save expects "data" field as array of records.', |
||
| 246 | ], |
||
| 247 | ]); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | $results = []; |
||
| 252 | $failed = []; |
||
| 253 | |||
| 254 | foreach ($_REQUEST['data'] as $datum) { |
||
| 255 | // get record |
||
| 256 | if (empty($datum[$PrimaryKey])) { |
||
| 257 | $Record = new $className::$defaultClass(); |
||
| 258 | static::onRecordCreated($Record, $datum); |
||
| 259 | } else { |
||
| 260 | if (!$Record = $className::getByID($datum[$PrimaryKey])) { |
||
| 261 | $failed[] = [ |
||
| 262 | 'record' => $datum, |
||
| 263 | 'errors' => 'Record not found', |
||
| 264 | ]; |
||
| 265 | continue; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | // check write access |
||
| 270 | if (!static::checkWriteAccess($Record)) { |
||
| 271 | $failed[] = [ |
||
| 272 | 'record' => $datum, |
||
| 273 | 'errors' => 'Write access denied', |
||
| 274 | ]; |
||
| 275 | continue; |
||
| 276 | } |
||
| 277 | |||
| 278 | // apply delta |
||
| 279 | static::applyRecordDelta($Record, $datum); |
||
| 280 | |||
| 281 | // call template function |
||
| 282 | static::onBeforeRecordValidated($Record, $datum); |
||
| 283 | |||
| 284 | // try to save record |
||
| 285 | try { |
||
| 286 | // call template function |
||
| 287 | static::onBeforeRecordSaved($Record, $datum); |
||
| 288 | |||
| 289 | $Record->save(); |
||
| 290 | $results[] = (!$Record::fieldExists('Class') || get_class($Record) == $Record->Class) ? $Record : $Record->changeClass(); |
||
| 291 | |||
| 292 | // call template function |
||
| 293 | static::onRecordSaved($Record, $datum); |
||
| 294 | } catch (Exception $e) { |
||
| 295 | $failed[] = [ |
||
| 296 | 'record' => $Record->data, |
||
| 297 | 'validationErrors' => $Record->validationErrors, |
||
| 298 | ]; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | |||
| 303 | return static::respond(static::getTemplateName($className::$pluralNoun).'Saved', [ |
||
| 304 | 'success' => count($results) || !count($failed), |
||
| 305 | 'data' => $results, |
||
| 306 | 'failed' => $failed, |
||
| 307 | ]); |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | public static function handleMultiDestroyRequest() |
||
| 384 | ]); |
||
| 385 | } |
||
| 386 | |||
| 387 | |||
| 388 | public static function handleCreateRequest(ActiveRecord $Record = null) |
||
| 389 | { |
||
| 390 | // save static class |
||
| 391 | static::$calledClass = get_called_class(); |
||
| 392 | |||
| 393 | if (!$Record) { |
||
| 394 | $className = static::$recordClass; |
||
| 395 | $Record = new $className::$defaultClass(); |
||
| 396 | } |
||
| 397 | |||
| 398 | // call template function |
||
| 399 | static::onRecordCreated($Record, $_REQUEST); |
||
| 400 | |||
| 401 | return static::handleEditRequest($Record); |
||
| 402 | } |
||
| 403 | |||
| 404 | public static function handleEditRequest(ActiveRecord $Record) |
||
| 405 | { |
||
| 406 | $className = static::$recordClass; |
||
| 407 | |||
| 408 | if (!static::checkWriteAccess($Record)) { |
||
| 409 | return static::throwUnauthorizedError(); |
||
| 410 | } |
||
| 411 | |||
| 412 | if (in_array($_SERVER['REQUEST_METHOD'], ['POST','PUT'])) { |
||
| 413 | if (static::$responseMode == 'json') { |
||
| 414 | $_REQUEST = JSON::getRequestData(); |
||
| 415 | if (is_array($_REQUEST['data'])) { |
||
| 416 | $_REQUEST = $_REQUEST['data']; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | $_REQUEST = $_REQUEST ? $_REQUEST : $_POST; |
||
| 420 | |||
| 421 | // apply delta |
||
| 422 | static::applyRecordDelta($Record, $_REQUEST); |
||
| 423 | |||
| 424 | // call template function |
||
| 425 | static::onBeforeRecordValidated($Record, $_REQUEST); |
||
| 426 | |||
| 427 | // validate |
||
| 428 | if ($Record->validate()) { |
||
| 429 | // call template function |
||
| 430 | static::onBeforeRecordSaved($Record, $_REQUEST); |
||
| 431 | |||
| 432 | try { |
||
| 433 | // save session |
||
| 434 | $Record->save(); |
||
| 435 | } catch (Exception $e) { |
||
| 436 | return static::respond('Error', [ |
||
| 437 | 'success' => false, |
||
| 438 | 'failed' => [ |
||
| 439 | 'errors' => $e->getMessage(), |
||
| 440 | ], |
||
| 441 | ]); |
||
| 442 | } |
||
| 443 | |||
| 444 | // call template function |
||
| 445 | static::onRecordSaved($Record, $_REQUEST); |
||
| 446 | |||
| 447 | // fire created response |
||
| 448 | $responseID = static::getTemplateName($className::$singularNoun).'Saved'; |
||
| 449 | $responseData = [ |
||
| 450 | 'success' => true, |
||
| 451 | 'data' => $Record, |
||
| 452 | ]; |
||
| 453 | return static::respond($responseID, $responseData); |
||
| 454 | } |
||
| 455 | |||
| 456 | // fall through back to form if validation failed |
||
| 457 | } |
||
| 458 | |||
| 459 | $responseID = static::getTemplateName($className::$singularNoun).'Edit'; |
||
| 460 | $responseData = [ |
||
| 461 | 'success' => false, |
||
| 462 | 'data' => $Record, |
||
| 463 | ]; |
||
| 464 | |||
| 465 | return static::respond($responseID, $responseData); |
||
| 466 | } |
||
| 467 | |||
| 468 | |||
| 469 | public static function handleDeleteRequest(ActiveRecord $Record) |
||
| 470 | { |
||
| 471 | $className = static::$recordClass; |
||
| 472 | |||
| 473 | if (!static::checkWriteAccess($Record)) { |
||
| 474 | return static::throwUnauthorizedError(); |
||
| 475 | } |
||
| 476 | |||
| 477 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
| 478 | $data = $Record->data; |
||
| 479 | $Record->destroy(); |
||
| 480 | |||
| 481 | // call cleanup function after delete |
||
| 482 | static::onRecordDeleted($Record, $data); |
||
| 483 | |||
| 484 | // fire created response |
||
| 485 | return static::respond(static::getTemplateName($className::$singularNoun).'Deleted', [ |
||
| 486 | 'success' => true, |
||
| 487 | 'data' => $Record, |
||
| 488 | ]); |
||
| 489 | } |
||
| 490 | |||
| 491 | return static::respond('confirm', [ |
||
| 492 | 'question' => 'Are you sure you want to delete this '.$className::$singularNoun.'?', |
||
| 493 | 'data' => $Record, |
||
| 494 | ]); |
||
| 495 | } |
||
| 496 | |||
| 497 | |||
| 498 | public static function respond($responseID, $responseData = [], $responseMode = false) |
||
| 499 | { |
||
| 500 | // default to static property |
||
| 501 | if (!$responseMode) { |
||
| 502 | $responseMode = static::$responseMode; |
||
| 503 | } |
||
| 504 | |||
| 505 | return parent::respond($responseID, $responseData, $responseMode); |
||
| 506 | } |
||
| 507 | |||
| 508 | // access control template functions |
||
| 509 | public static function checkBrowseAccess($arguments) |
||
| 510 | { |
||
| 511 | return true; |
||
| 512 | } |
||
| 513 | |||
| 514 | public static function checkReadAccess(ActiveRecord $Record) |
||
| 515 | { |
||
| 516 | return true; |
||
| 517 | } |
||
| 518 | |||
| 519 | public static function checkWriteAccess(ActiveRecord $Record) |
||
| 522 | } |
||
| 523 | |||
| 524 | public static function checkAPIAccess() |
||
| 525 | { |
||
| 526 | return true; |
||
| 527 | } |
||
| 528 | |||
| 529 | public static function throwUnauthorizedError() |
||
| 530 | { |
||
| 531 | return static::respond('Unauthorized', [ |
||
| 532 | 'success' => false, |
||
| 533 | 'failed' => [ |
||
| 534 | 'errors' => 'Login required.', |
||
| 535 | ], |
||
| 536 | ]); |
||
| 537 | } |
||
| 538 | |||
| 539 | public static function throwAPIUnAuthorizedError() |
||
| 540 | { |
||
| 541 | return static::respond('Unauthorized', [ |
||
| 542 | 'success' => false, |
||
| 543 | 'failed' => [ |
||
| 544 | 'errors' => 'API access required.', |
||
| 545 | ], |
||
| 546 | ]); |
||
| 547 | } |
||
| 548 | |||
| 549 | public static function throwNotFoundError() |
||
| 550 | { |
||
| 551 | return static::respond('error', [ |
||
| 552 | 'success' => false, |
||
| 553 | 'failed' => [ |
||
| 554 | 'errors' => 'Record not found.', |
||
| 555 | ], |
||
| 556 | ]); |
||
| 557 | } |
||
| 558 | |||
| 559 | public static function onRecordRequestNotHandled(ActiveRecord $Record, $action) |
||
| 560 | { |
||
| 561 | return static::respond('error', [ |
||
| 562 | 'success' => false, |
||
| 563 | 'failed' => [ |
||
| 564 | 'errors' => 'Malformed request.', |
||
| 565 | ], |
||
| 566 | ]); |
||
| 567 | } |
||
| 568 | |||
| 569 | |||
| 570 | |||
| 571 | public static function getTemplateName($noun) |
||
| 572 | { |
||
| 573 | return preg_replace_callback('/\s+([a-zA-Z])/', function ($matches) { |
||
| 574 | return strtoupper($matches[1]); |
||
| 575 | }, $noun); |
||
| 576 | } |
||
| 577 | |||
| 578 | public static function applyRecordDelta(ActiveRecord $Record, $data) |
||
| 579 | { |
||
| 580 | if (is_array(static::$editableFields)) { |
||
|
1 ignored issue
–
show
|
|||
| 581 | $Record->setFields(array_intersect_key($data, array_flip(static::$editableFields))); |
||
| 582 | } else { |
||
| 583 | $Record->setFields($data); |
||
| 584 | } |
||
| 585 | } |
||
| 586 | |||
| 587 | // event template functions |
||
| 588 | protected static function onRecordCreated(ActiveRecord $Record, $data) |
||
| 589 | { |
||
| 590 | } |
||
| 591 | protected static function onBeforeRecordValidated(ActiveRecord $Record, $data) |
||
| 592 | { |
||
| 593 | } |
||
| 594 | protected static function onBeforeRecordSaved(ActiveRecord $Record, $data) |
||
| 595 | { |
||
| 596 | } |
||
| 597 | protected static function onRecordDeleted(ActiveRecord $Record, $data) |
||
| 599 | } |
||
| 600 | protected static function onRecordSaved(ActiveRecord $Record, $data) |
||
| 601 | { |
||
| 602 | } |
||
| 603 | |||
| 604 | protected static function throwRecordNotFoundError() |
||
| 605 | { |
||
| 606 | return static::throwNotFoundError(); |
||
| 607 | } |
||
| 608 | } |
||
| 609 |