Total Complexity | 56 |
Total Lines | 526 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like ResourcesPage 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 ResourcesPage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class ResourcesPage extends AdministrationPage |
||
16 | { |
||
17 | /** |
||
18 | * This method is invoked from the `Sortable` class and it contains the |
||
19 | * logic for sorting (or unsorting) the resource index. It provides a basic |
||
20 | * wrapper to the `ResourceManager`'s `fetch()` method. |
||
21 | * |
||
22 | * @see toolkit.ResourceManager#getSortingField |
||
23 | * @see toolkit.ResourceManager#getSortingOrder |
||
24 | * @see toolkit.ResourceManager#fetch |
||
25 | * @param string $sort |
||
26 | * The field to sort on which should match one of the table's column names. |
||
27 | * If this is not provided the default will be determined by |
||
28 | * `ResourceManager::getSortingField` |
||
29 | * @param string $order |
||
30 | * The direction to sort in, either 'asc' or 'desc'. If this is not provided |
||
31 | * the value will be determined by `ResourceManager::getSortingOrder`. |
||
32 | * @param array $params |
||
33 | * An associative array of params (usually populated from the URL) that this |
||
34 | * function uses. The current implementation will use `type` and `unsort` keys |
||
35 | * @throws Exception |
||
36 | * @throws SymphonyErrorPage |
||
37 | * @return array |
||
38 | * An associative of the resource as determined by `ResourceManager::fetch` |
||
39 | */ |
||
40 | public function sort(&$sort, &$order, array $params) |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * This function creates an array of all page titles in the system. |
||
77 | * |
||
78 | * @return array |
||
79 | * An array of page titles |
||
80 | */ |
||
81 | public function pagesFlatView() |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * This function contains the minimal amount of logic for generating the |
||
94 | * index table of a given `$resource_type`. The table has name, source, pages |
||
95 | * release date and author columns. The values for these columns are determined |
||
96 | * by the resource's `about()` method. |
||
97 | * |
||
98 | * As Datasources types can be installed using Providers, the Source column |
||
99 | * can be overridden with a Datasource's `getSourceColumn` method (if it exists). |
||
100 | * |
||
101 | * @param integer $resource_type |
||
102 | * Either `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE` |
||
103 | * @throws InvalidArgumentException |
||
104 | */ |
||
105 | public function __viewIndex($resource_type) |
||
362 | } |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * This function is called from the resources index when a user uses the |
||
367 | * With Selected, or Apply, menu. The type of resource is given by |
||
368 | * `$resource_type`. At this time the only two valid values, |
||
369 | * `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE`. |
||
370 | * |
||
371 | * The function handles 'delete', 'attach', 'detach', 'attach all', |
||
372 | * 'detach all' actions. |
||
373 | * |
||
374 | * @param integer $resource_type |
||
375 | * Either `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE` |
||
376 | * @throws Exception |
||
377 | */ |
||
378 | public function __actionIndex($resource_type) |
||
379 | { |
||
380 | $manager = ResourceManager::getManagerFromType($resource_type); |
||
381 | $resource_name = str_replace('Manager', '', $manager); |
||
382 | $delegate_path = strtolower($resource_name); |
||
383 | $checked = (is_array($_POST['items'])) ? array_keys($_POST['items']) : null; |
||
384 | $context = Administration::instance()->getPageCallback(); |
||
385 | |||
386 | if (isset($_POST['action']) && is_array($_POST['action'])) { |
||
387 | /** |
||
388 | * Extensions can listen for any custom actions that were added |
||
389 | * through `AddCustomPreferenceFieldsets` or `AddCustomActions` |
||
390 | * delegates. |
||
391 | * |
||
392 | * @delegate CustomActions |
||
393 | * @since Symphony 2.3.2 |
||
394 | * @param string $context |
||
395 | * '/blueprints/datasources/' or '/blueprints/events/' |
||
396 | * @param array $checked |
||
397 | * An array of the selected rows. The value is usually the ID of the |
||
398 | * the associated object. |
||
399 | */ |
||
400 | Symphony::ExtensionManager()->notifyMembers('CustomActions', $context['pageroot'], array( |
||
401 | 'checked' => $checked |
||
402 | )); |
||
403 | |||
404 | if (is_array($checked) && !empty($checked)) { |
||
405 | if ($_POST['with-selected'] == 'delete') { |
||
406 | $canProceed = true; |
||
407 | |||
408 | foreach ($checked as $handle) { |
||
409 | $path = call_user_func(array($manager, '__getDriverPath'), $handle); |
||
410 | |||
411 | // Don't allow Extension resources to be deleted. RE: #2027 |
||
412 | if (stripos($path, EXTENSIONS) === 0) { |
||
413 | continue; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Prior to deleting the Resource file. Target file path is provided. |
||
418 | * |
||
419 | * @since Symphony 3.0.0 |
||
420 | * @param string $context |
||
421 | * '/blueprints/{$resource_name}/' |
||
422 | * @param string $file |
||
423 | * The path to the Resource file |
||
424 | * @param string $handle |
||
425 | * The handle of the Resource |
||
426 | */ |
||
427 | Symphony::ExtensionManager()->notifyMembers( |
||
428 | "{$resource_name}PreDelete", |
||
429 | $context['pageroot'], |
||
430 | array( |
||
431 | 'file' => $path, |
||
432 | 'handle' => $handle, |
||
433 | ) |
||
434 | ); |
||
435 | |||
436 | if (!General::deleteFile($path)) { |
||
437 | $folder = str_replace(DOCROOT, '', $path); |
||
438 | $folder = str_replace('/' . basename($path), '', $folder); |
||
439 | |||
440 | $this->pageAlert( |
||
441 | __('Failed to delete %s.', array('<code>' . basename($path) . '</code>')) |
||
442 | . ' ' . __('Please check permissions on %s', array('<code>' . $folder . '</code>')), |
||
443 | Alert::ERROR |
||
444 | ); |
||
445 | $canProceed = false; |
||
446 | } else { |
||
447 | $pages = ResourceManager::getAttachedPages($resource_type, $handle); |
||
448 | |||
449 | foreach ($pages as $page) { |
||
450 | ResourceManager::detach($resource_type, $handle, $page['id']); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * After deleting the Resource file. Target file path is provided. |
||
455 | * |
||
456 | * @since Symphony 3.0.0 |
||
457 | * @param string $context |
||
458 | * '/blueprints/{$resource_name}/' |
||
459 | * @param string $file |
||
460 | * The path to the Resource file |
||
461 | * @param string $handle |
||
462 | * The handle of the Resource |
||
463 | */ |
||
464 | Symphony::ExtensionManager()->notifyMembers( |
||
465 | "{$resource_name}PostDelete", |
||
466 | "/blueprints/{$delegate_path}/", |
||
467 | array( |
||
468 | 'file' => $path, |
||
469 | 'handle' => $handle, |
||
470 | ) |
||
471 | ); |
||
472 | } |
||
473 | } |
||
474 | |||
475 | if ($canProceed) { |
||
476 | redirect(Administration::instance()->getCurrentPageURL()); |
||
477 | } |
||
478 | } elseif (preg_match('/^(at|de)?tach-(to|from)-page-/', $_POST['with-selected'])) { |
||
479 | if (substr($_POST['with-selected'], 0, 6) == 'detach') { |
||
480 | $page = str_replace('detach-from-page-', '', $_POST['with-selected']); |
||
481 | |||
482 | foreach ($checked as $handle) { |
||
483 | ResourceManager::detach($resource_type, $handle, $page); |
||
484 | } |
||
485 | } else { |
||
486 | $page = str_replace('attach-to-page-', '', $_POST['with-selected']); |
||
487 | |||
488 | foreach ($checked as $handle) { |
||
489 | ResourceManager::attach($resource_type, $handle, $page); |
||
490 | } |
||
491 | } |
||
492 | |||
493 | if ($canProceed) { |
||
494 | redirect(Administration::instance()->getCurrentPageURL()); |
||
495 | } |
||
496 | } elseif (preg_match('/^(at|de)?tach-all-pages$/', $_POST['with-selected'])) { |
||
497 | $pages = (new PageManager)->select(['id'])->execute()->rows(); |
||
498 | |||
499 | if (substr($_POST['with-selected'], 0, 6) == 'detach') { |
||
500 | foreach ($checked as $handle) { |
||
501 | foreach ($pages as $page) { |
||
502 | ResourceManager::detach($resource_type, $handle, $page['id']); |
||
503 | } |
||
504 | } |
||
505 | } else { |
||
506 | foreach ($checked as $handle) { |
||
507 | foreach ($pages as $page) { |
||
508 | ResourceManager::attach($resource_type, $handle, $page['id']); |
||
509 | } |
||
510 | } |
||
511 | } |
||
512 | |||
513 | redirect(Administration::instance()->getCurrentPageURL()); |
||
514 | } |
||
515 | } |
||
516 | } |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Returns the path to the component-template by looking at the |
||
521 | * `WORKSPACE/template/` directory, then at the `TEMPLATES` |
||
522 | * directory for the convention `*.tpl`. If the template |
||
523 | * is not found, false is returned |
||
524 | * |
||
525 | * @param string $name |
||
526 | * Name of the template |
||
527 | * @return mixed |
||
528 | * String, which is the path to the template if the template is found, |
||
529 | * false otherwise |
||
530 | */ |
||
531 | protected function getTemplate($name) |
||
541 | } |
||
542 | } |
||
544 |