| Total Complexity | 65 |
| Total Lines | 674 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like ModulesController 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 ModulesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class ModulesController extends AppController |
||
| 46 | { |
||
| 47 | use ApiConfigTrait; |
||
| 48 | use PermissionsTrait; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Object type currently used |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $objectType = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @inheritDoc |
||
| 59 | */ |
||
| 60 | public function initialize(): void |
||
| 61 | { |
||
| 62 | parent::initialize(); |
||
| 63 | |||
| 64 | $this->loadComponent('Children'); |
||
| 65 | $this->loadComponent('History'); |
||
| 66 | $this->loadComponent('ObjectsEditors'); |
||
| 67 | $this->loadComponent('Parents'); |
||
| 68 | $this->loadComponent('Properties'); |
||
| 69 | $this->loadComponent('ProjectConfiguration'); |
||
| 70 | $this->loadComponent('Query'); |
||
| 71 | $this->loadComponent('Thumbs', Configure::read('Thumbs', [])); |
||
| 72 | $this->loadComponent('BEdita/WebTools.ApiFormatter'); |
||
| 73 | if ($this->getRequest()->getParam('object_type')) { |
||
| 74 | $this->objectType = $this->getRequest()->getParam('object_type'); |
||
| 75 | $this->Modules->setConfig('currentModuleName', $this->objectType); |
||
| 76 | $this->Schema->setConfig('type', $this->objectType); |
||
| 77 | } |
||
| 78 | $this->Security->setConfig('unlockedActions', ['save', 'setup']); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritDoc} |
||
| 83 | * |
||
| 84 | * @codeCoverageIgnore |
||
| 85 | */ |
||
| 86 | public function beforeRender(EventInterface $event): ?Response |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Display resources list. |
||
| 95 | * |
||
| 96 | * @return \Cake\Http\Response|null |
||
| 97 | */ |
||
| 98 | public function index(): ?Response |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * View single resource. |
||
| 154 | * |
||
| 155 | * @param string|int $id Resource ID. |
||
| 156 | * @return \Cake\Http\Response|null |
||
| 157 | */ |
||
| 158 | public function view($id): ?Response |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * View single resource by id, doing a proper redirect (302) to resource module view by type. |
||
| 212 | * If no resource found by ID, redirect to referer. |
||
| 213 | * |
||
| 214 | * @param string|int $id Resource ID. |
||
| 215 | * @return \Cake\Http\Response|null |
||
| 216 | */ |
||
| 217 | public function uname($id): ?Response |
||
| 218 | { |
||
| 219 | try { |
||
| 220 | $response = $this->apiClient->get(sprintf('/objects/%s', $id)); |
||
| 221 | } catch (BEditaClientException $e) { |
||
| 222 | $msg = $e->getMessage(); |
||
| 223 | $msgNotFound = sprintf(__('Resource "%s" not found', true), $id); |
||
| 224 | $msgNotAvailable = sprintf(__('Resource "%s" not available. Error: %s', true), $id, $msg); |
||
| 225 | $error = $e->getCode() === 404 ? $msgNotFound : $msgNotAvailable; |
||
| 226 | $this->Flash->error($error); |
||
| 227 | |||
| 228 | return $this->redirect($this->referer()); |
||
| 229 | } |
||
| 230 | $_name = 'modules:view'; |
||
| 231 | $object_type = $response['data']['type']; |
||
| 232 | $id = $response['data']['id']; |
||
| 233 | |||
| 234 | return $this->redirect(compact('_name', 'object_type', 'id')); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Display new resource form. |
||
| 239 | * |
||
| 240 | * @return \Cake\Http\Response|null |
||
| 241 | */ |
||
| 242 | public function create(): ?Response |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Create new object from ajax request. |
||
| 280 | * |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | public function save(): void |
||
| 284 | { |
||
| 285 | $this->viewBuilder()->setClassName('Json'); // force json response |
||
| 286 | $this->getRequest()->allowMethod(['post']); |
||
| 287 | $requestData = $this->prepareRequest($this->objectType); |
||
| 288 | unset($requestData['_csrfToken']); |
||
| 289 | // extract related objects data |
||
| 290 | $relatedData = (array)Hash::get($requestData, '_api'); |
||
| 291 | unset($requestData['_api']); |
||
| 292 | |||
| 293 | try { |
||
| 294 | $uname = Hash::get($requestData, 'uname'); |
||
| 295 | if (!empty($uname) && is_numeric($uname)) { |
||
| 296 | $this->set(['error' => __('Invalid numeric uname. Change it to a valid string')]); |
||
| 297 | $this->setSerialize(['error']); |
||
| 298 | |||
| 299 | return; |
||
| 300 | } |
||
| 301 | $id = (string)Hash::get($requestData, 'id'); |
||
| 302 | // skip save if no data changed |
||
| 303 | $schema = (array)$this->Schema->getSchema($this->objectType); |
||
| 304 | $permissions = (array)Hash::get($requestData, 'permissions'); |
||
| 305 | $skipSaveObject = $this->Modules->skipSaveObject($id, $requestData); |
||
| 306 | $skipSaveRelated = $this->Modules->skipSaveRelated($id, $relatedData); |
||
| 307 | $skipSavePermissions = $this->Modules->skipSavePermissions($id, $permissions, $schema); |
||
| 308 | if ($skipSaveObject && $skipSaveRelated && $skipSavePermissions) { |
||
| 309 | $response = $this->apiClient->getObject($id, $this->objectType, ['count' => 'all']); |
||
| 310 | $this->Thumbs->urls($response); |
||
| 311 | $this->set((array)$response); |
||
| 312 | $this->setSerialize(array_keys($response)); |
||
| 313 | |||
| 314 | return; |
||
| 315 | } |
||
| 316 | |||
| 317 | // upload file (if available) |
||
| 318 | $this->Modules->upload($requestData); |
||
| 319 | |||
| 320 | // save data |
||
| 321 | $lang = I18n::getLocale(); |
||
| 322 | $headers = ['Accept-Language' => $lang]; |
||
| 323 | if (!$skipSaveObject) { |
||
| 324 | $response = $this->apiClient->save($this->objectType, $requestData, $headers); |
||
| 325 | } else { |
||
| 326 | $response = $this->apiClient->getObject($id, $this->objectType); |
||
| 327 | } |
||
| 328 | if (!$skipSavePermissions) { |
||
| 329 | $this->savePermissions( |
||
| 330 | (array)$response, |
||
| 331 | $schema, |
||
| 332 | $permissions |
||
| 333 | ); |
||
| 334 | } |
||
| 335 | $id = (string)Hash::get($response, 'data.id'); |
||
| 336 | if (!$skipSaveRelated) { |
||
| 337 | $this->Modules->saveRelated($id, $this->objectType, $relatedData); |
||
| 338 | } |
||
| 339 | $options = [ |
||
| 340 | 'id' => Hash::get($response, 'data.id'), |
||
| 341 | 'type' => $this->objectType, |
||
| 342 | 'data' => $requestData, |
||
| 343 | ]; |
||
| 344 | $event = new Event('Controller.afterSave', $this, $options); |
||
| 345 | $this->getEventManager()->dispatch($event); |
||
| 346 | } catch (BEditaClientException $error) { |
||
| 347 | $message = new Message($error); |
||
| 348 | $this->log($message->get(), LogLevel::ERROR); |
||
| 349 | $this->Flash->error($message->get(), ['params' => $error]); |
||
| 350 | $this->set(['error' => $message->get()]); |
||
| 351 | $this->setSerialize(['error']); |
||
| 352 | |||
| 353 | return; |
||
| 354 | } |
||
| 355 | if ($response['data']) { |
||
| 356 | $response['data'] = [ $response['data'] ]; |
||
| 357 | } |
||
| 358 | |||
| 359 | $this->Thumbs->urls($response); |
||
| 360 | |||
| 361 | $this->set((array)$response); |
||
| 362 | $this->setSerialize(array_keys($response)); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Clone single object. |
||
| 367 | * |
||
| 368 | * @param string|int $id Object ID. |
||
| 369 | * @return \Cake\Http\Response|null |
||
| 370 | */ |
||
| 371 | public function clone($id): ?Response |
||
| 372 | { |
||
| 373 | $this->viewBuilder()->setTemplate('view'); |
||
| 374 | $schema = $this->Schema->getSchema(); |
||
| 375 | if (!is_array($schema)) { |
||
| 376 | $this->Flash->error(__('Cannot create abstract objects or objects without schema')); |
||
| 377 | |||
| 378 | return $this->redirect(['_name' => 'modules:list', 'object_type' => $this->objectType]); |
||
| 379 | } |
||
| 380 | try { |
||
| 381 | $modified = [ |
||
| 382 | 'title' => $this->getRequest()->getQuery('title'), |
||
| 383 | 'status' => 'draft', |
||
| 384 | ]; |
||
| 385 | $reset = (array)Configure::read(sprintf('Clone.%s.reset', $this->objectType)); |
||
| 386 | foreach ($reset as $field) { |
||
| 387 | $modified[$field] = null; |
||
| 388 | } |
||
| 389 | $included = []; |
||
| 390 | foreach (['relationships', 'translations'] as $attribute) { |
||
| 391 | if ($this->getRequest()->getQuery($attribute) === 'true') { |
||
| 392 | $included[] = $attribute; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | $clone = $this->apiClient->clone($this->objectType, $id, $modified, $included); |
||
| 396 | $id = (string)Hash::get($clone, 'data.id'); |
||
| 397 | } catch (BEditaClientException $e) { |
||
| 398 | $this->log($e->getMessage(), LogLevel::ERROR); |
||
| 399 | $this->Flash->error($e->getMessage(), ['params' => $e]); |
||
| 400 | } |
||
| 401 | |||
| 402 | return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->objectType, 'id' => $id]); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Delete single resource. |
||
| 407 | * |
||
| 408 | * @return \Cake\Http\Response|null |
||
| 409 | */ |
||
| 410 | public function delete(): ?Response |
||
| 411 | { |
||
| 412 | $this->getRequest()->allowMethod(['post']); |
||
| 413 | $id = $this->getRequest()->getData('id'); |
||
| 414 | $ids = $this->getRequest()->getData('ids'); |
||
| 415 | $ids = is_string($ids) ? explode(',', $ids) : $ids; |
||
| 416 | $ids = empty($ids) ? [$id] : $ids; |
||
| 417 | try { |
||
| 418 | $this->apiClient->deleteObjects($ids, $this->objectType); |
||
| 419 | $eventManager = $this->getEventManager(); |
||
| 420 | foreach ($ids as $id) { |
||
| 421 | $event = new Event('Controller.afterDelete', $this, ['id' => $id, 'type' => $this->objectType]); |
||
| 422 | $eventManager->dispatch($event); |
||
| 423 | } |
||
| 424 | } catch (BEditaClientException $e) { |
||
| 425 | $this->log($e->getMessage(), LogLevel::ERROR); |
||
| 426 | $this->Flash->error($e->getMessage(), ['params' => $e]); |
||
| 427 | $id = $this->getRequest()->getData('id'); |
||
| 428 | $options = empty($id) ? $this->referer() : ['_name' => 'modules:view', 'object_type' => $this->objectType, 'id' => $id]; |
||
| 429 | |||
| 430 | return $this->redirect($options); |
||
| 431 | } |
||
| 432 | $this->Flash->success(__('Object(s) deleted')); |
||
| 433 | |||
| 434 | return $this->redirect([ |
||
| 435 | '_name' => 'modules:list', |
||
| 436 | 'object_type' => $this->objectType, |
||
| 437 | ]); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Relation data load via API => `GET /:object_type/:id/related/:relation` |
||
| 442 | * |
||
| 443 | * @param string|int $id The object ID. |
||
| 444 | * @param string $relation The relation name. |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | public function related($id, string $relation): void |
||
| 448 | { |
||
| 449 | if ($id === 'new') { |
||
| 450 | $this->set('data', []); |
||
| 451 | $this->setSerialize(['data']); |
||
| 452 | |||
| 453 | return; |
||
| 454 | } |
||
| 455 | |||
| 456 | $this->getRequest()->allowMethod(['get']); |
||
| 457 | $query = $this->Query->prepare($this->getRequest()->getQueryParams()); |
||
| 458 | try { |
||
| 459 | $response = $this->apiClient->getRelated($id, $this->objectType, $relation, $query); |
||
| 460 | $response = $this->ApiFormatter->embedIncluded((array)$response); |
||
| 461 | } catch (BEditaClientException $error) { |
||
| 462 | $this->log($error->getMessage(), LogLevel::ERROR); |
||
| 463 | |||
| 464 | $this->set(compact('error')); |
||
| 465 | $this->setSerialize(['error']); |
||
| 466 | |||
| 467 | return; |
||
| 468 | } |
||
| 469 | |||
| 470 | $this->Thumbs->urls($response); |
||
| 471 | |||
| 472 | $this->set((array)$response); |
||
| 473 | $this->setSerialize(array_keys($response)); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Load resources of $type callig api `GET /:type/` |
||
| 478 | * Json response |
||
| 479 | * |
||
| 480 | * @param string|int $id the object identifier. |
||
| 481 | * @param string $type the resource type name. |
||
| 482 | * @return void |
||
| 483 | */ |
||
| 484 | public function resources($id, string $type): void |
||
| 485 | { |
||
| 486 | $this->getRequest()->allowMethod(['get']); |
||
| 487 | $query = $this->Query->prepare($this->getRequest()->getQueryParams()); |
||
| 488 | try { |
||
| 489 | $response = $this->apiClient->get($type, $query); |
||
| 490 | } catch (BEditaClientException $error) { |
||
| 491 | $this->log($error, LogLevel::ERROR); |
||
| 492 | |||
| 493 | $this->set(compact('error')); |
||
| 494 | $this->setSerialize(['error']); |
||
| 495 | |||
| 496 | return; |
||
| 497 | } |
||
| 498 | |||
| 499 | $this->set((array)$response); |
||
| 500 | $this->setSerialize(array_keys($response)); |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Relation data load calling api `GET /:object_type/:id/relationships/:relation` |
||
| 505 | * Json response |
||
| 506 | * |
||
| 507 | * @param string|int $id The object ID. |
||
| 508 | * @param string $relation The relation name. |
||
| 509 | * @return void |
||
| 510 | */ |
||
| 511 | public function relationships($id, string $relation): void |
||
| 512 | { |
||
| 513 | $this->getRequest()->allowMethod(['get']); |
||
| 514 | $available = $this->availableRelationshipsUrl($relation); |
||
| 515 | |||
| 516 | try { |
||
| 517 | $query = $this->Query->prepare($this->getRequest()->getQueryParams()); |
||
| 518 | $response = $this->apiClient->get($available, $query); |
||
| 519 | |||
| 520 | $this->Thumbs->urls($response); |
||
| 521 | } catch (BEditaClientException $ex) { |
||
| 522 | $this->log($ex->getMessage(), LogLevel::ERROR); |
||
| 523 | |||
| 524 | $this->set('error', $ex->getMessage()); |
||
| 525 | $this->setSerialize(['error']); |
||
| 526 | |||
| 527 | return; |
||
| 528 | } |
||
| 529 | |||
| 530 | $this->set((array)$response); |
||
| 531 | $this->setSerialize(array_keys($response)); |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Retrieve URL to get objects available for a relation |
||
| 536 | * |
||
| 537 | * @param string $relation The relation name. |
||
| 538 | * @return string |
||
| 539 | */ |
||
| 540 | protected function availableRelationshipsUrl(string $relation): string |
||
| 541 | { |
||
| 542 | $defaults = [ |
||
| 543 | 'children' => '/objects', |
||
| 544 | 'parent' => '/folders', |
||
| 545 | 'parents' => '/folders', |
||
| 546 | ]; |
||
| 547 | $defaultUrl = (string)Hash::get($defaults, $relation); |
||
| 548 | if (!empty($defaultUrl)) { |
||
| 549 | return $defaultUrl; |
||
| 550 | } |
||
| 551 | |||
| 552 | $relationsSchema = $this->Schema->getRelationsSchema(); |
||
| 553 | $types = $this->Modules->relatedTypes($relationsSchema, $relation); |
||
| 554 | |||
| 555 | return count($types) === 1 ? sprintf('/%s', $types[0]) : '/objects?filter[type][]=' . implode('&filter[type][]=', $types); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * get object properties and format them for index |
||
| 560 | * |
||
| 561 | * @param string $objectType objecte type name |
||
| 562 | * @return array $schema |
||
| 563 | */ |
||
| 564 | public function getSchemaForIndex($objectType): array |
||
| 565 | { |
||
| 566 | $schema = (array)$this->Schema->getSchema($objectType); |
||
| 567 | |||
| 568 | // if prop is an enum then prepend an empty string for select element |
||
| 569 | if (!empty($schema['properties'])) { |
||
| 570 | foreach ($schema['properties'] as &$property) { |
||
| 571 | if (isset($property['enum'])) { |
||
| 572 | array_unshift($property['enum'], ''); |
||
| 573 | } |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | return $schema; |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get objectType |
||
| 582 | * |
||
| 583 | * @return string|null |
||
| 584 | */ |
||
| 585 | public function getObjectType(): ?string |
||
| 586 | { |
||
| 587 | return $this->objectType; |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Set objectType |
||
| 592 | * |
||
| 593 | * @param string|null $objectType The object type |
||
| 594 | * @return void |
||
| 595 | */ |
||
| 596 | public function setObjectType(?string $objectType): void |
||
| 597 | { |
||
| 598 | $this->objectType = $objectType; |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Set schemasByType and filtersByType, considering relations and schemas. |
||
| 603 | * |
||
| 604 | * @param array $relations The relations |
||
| 605 | * @return void |
||
| 606 | */ |
||
| 607 | private function setupViewRelations(array $relations): void |
||
| 608 | { |
||
| 609 | // setup relations schema |
||
| 610 | $relationsSchema = $this->Schema->getRelationsSchema(); |
||
| 611 | $this->set('relationsSchema', $relationsSchema); |
||
| 612 | |||
| 613 | // setup relations metadata |
||
| 614 | $this->Modules->setupRelationsMeta( |
||
| 615 | $relationsSchema, |
||
| 616 | $relations, |
||
| 617 | $this->Properties->relationsList($this->objectType), |
||
| 618 | $this->Properties->hiddenRelationsList($this->objectType), |
||
| 619 | $this->Properties->readonlyRelationsList($this->objectType) |
||
| 620 | ); |
||
| 621 | |||
| 622 | // set right types, considering the object type relations |
||
| 623 | $rel = (array)$this->viewBuilder()->getVar('relationsSchema'); |
||
| 624 | $rightTypes = \App\Utility\Schema::rightTypes($rel); |
||
| 625 | $this->set('rightTypes', $rightTypes); |
||
| 626 | |||
| 627 | // set schemas for relations right types |
||
| 628 | $schemasByType = $this->Schema->getSchemasByType($rightTypes); |
||
| 629 | $this->set('schemasByType', $schemasByType); |
||
| 630 | $this->set('filtersByType', $this->Properties->filtersByType($rightTypes)); |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Get list of users / no email, no relationships, no links, no schema, no included. |
||
| 635 | * |
||
| 636 | * @return void |
||
| 637 | */ |
||
| 638 | public function users(): void |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Get single resource, minimal data / no relationships, no links, no schema, no included. |
||
| 656 | * |
||
| 657 | * @param string $id The object ID |
||
| 658 | * @return void |
||
| 659 | */ |
||
| 660 | public function get(string $id): void |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Setup module. |
||
| 686 | * |
||
| 687 | * @return \Cake\Http\Response|null |
||
| 688 | */ |
||
| 689 | public function setup(): ?Response |
||
| 719 | } |
||
| 720 | } |
||
| 721 |