Conditions | 12 |
Paths | 24 |
Total Lines | 74 |
Code Lines | 38 |
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 |
||
56 | public function run($request) |
||
57 | { |
||
58 | $pageTypes = singleton(ElementalArea::class)->supportedPageTypes(); |
||
59 | $count = 0; |
||
60 | foreach ($pageTypes as $pageType) { |
||
61 | // Only pages that have the ElementalPageExtension have a known ElementalArea relation |
||
62 | if (!$this->isMigratable($pageType)) { |
||
63 | continue; |
||
64 | } |
||
65 | |||
66 | $pages = $pageType::get()->filter('Content:not', ['', null]); |
||
67 | $clearContent = $this->config()->get('clear_content'); |
||
68 | |||
69 | $this->extend('updatePageFilter', $pages, $pageType); |
||
70 | $pageTypeCount = 0; |
||
71 | |||
72 | /** @var SiteTree&ElementalAreasExtension $page */ |
||
73 | foreach ($pages as $page) { |
||
74 | if ($this->shouldSkipMigration($page)) { |
||
75 | continue; |
||
76 | } |
||
77 | // Fetch and clear existing content (if configured) |
||
78 | $content = $page->Content; |
||
79 | $pageIsLive = $page->isPublished(); |
||
80 | if ($clearContent) { |
||
81 | $page->Content = ''; |
||
82 | } |
||
83 | |||
84 | // Get the area |
||
85 | $area = $this->getAreaRelationFromPage($page); |
||
86 | |||
87 | // Write the page if we're clearing content or if the area doesn't exist - we write to trigger a |
||
88 | // relationship update |
||
89 | if ($clearContent || !$area->exists()) { |
||
90 | try { |
||
91 | $page->write(); |
||
92 | } catch (Exception $e) { |
||
93 | echo sprintf( |
||
94 | 'Could not clear content on page %s: %s', |
||
95 | $page->ID, |
||
96 | $e->getMessage() |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | if (!$area->exists()) { |
||
101 | $area = $this->getAreaRelationFromPage($page); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // Create a new element |
||
106 | /** @var BaseElement $element */ |
||
107 | $element = Injector::inst()->create($this->config()->get('target_element')); |
||
108 | $element->Title = 'Auto migrated content'; |
||
109 | |||
110 | // Set the configured field |
||
111 | $element->setField($this->config()->get('target_element_field'), $content); |
||
112 | |||
113 | // Provide an extension hook for further updates to the new element |
||
114 | $this->extend('updateMigratedElement', $element, $content, $page); |
||
115 | |||
116 | // Add and write to the area |
||
117 | $area->Elements()->add($element); |
||
118 | |||
119 | // Publish the record if configured |
||
120 | if ($this->config()->get('publish_changes') && $pageIsLive) { |
||
121 | $page->publishRecursive(); |
||
122 | } |
||
123 | |||
124 | $pageTypeCount++; |
||
125 | } |
||
126 | $count += $pageTypeCount; |
||
127 | echo 'Migrated ' . $pageTypeCount . ' ' . $pageType . ' pages\' content<br>'; |
||
128 | } |
||
129 | echo 'Finished migrating ' . $count . ' pages\' content<br>'; |
||
130 | } |
||
176 |