| Conditions | 10 |
| Paths | 16 |
| Total Lines | 65 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 55 | public function run($request) |
||
| 56 | { |
||
| 57 | $pageTypes = singleton(ElementalArea::class)->supportedPageTypes(); |
||
| 58 | $count = 0; |
||
| 59 | foreach ($pageTypes as $pageType) { |
||
| 60 | // Only pages that have the ElementalPageExtension have a known ElementalArea relation |
||
| 61 | if (!$this->isMigratable($pageType)) { |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | |||
| 65 | $pages = $pageType::get()->filter('Content:not', ['', null]);; |
||
| 66 | $clearContent = $this->config()->get('clear_content'); |
||
| 67 | |||
| 68 | $this->extend('updatePageFilter', $pages, $pageType); |
||
| 69 | $pageTypeCount = 0; |
||
| 70 | |||
| 71 | /** @var SiteTree&ElementalAreasExtension $page */ |
||
| 72 | foreach ($pages as $page) { |
||
| 73 | if ($this->shouldSkipMigration($page)) { |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | // Fetch and clear existing content (if configured) |
||
| 77 | $content = $page->Content; |
||
| 78 | if ($clearContent) { |
||
| 79 | $page->Content = ''; |
||
| 80 | } |
||
| 81 | |||
| 82 | // Get the area |
||
| 83 | $area = $this->getAreaRelationFromPage($page); |
||
| 84 | |||
| 85 | // Write the page if we're clearing content or if the area doesn't exist - we write to trigger a |
||
| 86 | // relationship update |
||
| 87 | if ($clearContent || !$area->exists()) { |
||
| 88 | $page->write(); |
||
| 89 | |||
| 90 | if (!$area->exists()) { |
||
| 91 | $area = $this->getAreaRelationFromPage($page); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | // Create a new element |
||
| 96 | /** @var BaseElement $element */ |
||
| 97 | $element = Injector::inst()->create($this->config()->get('target_element')); |
||
| 98 | $element->Title = 'Auto migrated content'; |
||
| 99 | |||
| 100 | // Set the configured field |
||
| 101 | $element->setField($this->config()->get('target_element_field'), $content); |
||
| 102 | |||
| 103 | // Provide an extension hook for further updates to the new element |
||
| 104 | $this->extend('updateMigratedElement', $element); |
||
| 105 | |||
| 106 | // Add and write to the area |
||
| 107 | $area->Elements()->add($element); |
||
| 108 | |||
| 109 | // Publish the record if configured |
||
| 110 | if ($this->config()->get('publish_changes')) { |
||
| 111 | $page->publishRecursive(); |
||
| 112 | } |
||
| 113 | |||
| 114 | $pageTypeCount++; |
||
| 115 | } |
||
| 116 | $count += $pageTypeCount; |
||
| 117 | echo 'Migrated ' . $pageTypeCount . ' ' . $pageType . ' pages\' content<br>'; |
||
| 118 | } |
||
| 119 | echo 'Finished migrating ' . $count . ' pages\' content<br>'; |
||
| 120 | } |
||
| 166 |