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