Conditions | 11 |
Paths | 16 |
Total Lines | 66 |
Code Lines | 32 |
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 | $pageIsLive = $page->isPublished(); |
||
79 | if ($clearContent) { |
||
80 | $page->Content = ''; |
||
81 | } |
||
82 | |||
83 | // Get the area |
||
84 | $area = $this->getAreaRelationFromPage($page); |
||
85 | |||
86 | // Write the page if we're clearing content or if the area doesn't exist - we write to trigger a |
||
87 | // relationship update |
||
88 | if ($clearContent || !$area->exists()) { |
||
89 | $page->write(); |
||
90 | |||
91 | if (!$area->exists()) { |
||
92 | $area = $this->getAreaRelationFromPage($page); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // Create a new element |
||
97 | /** @var BaseElement $element */ |
||
98 | $element = Injector::inst()->create($this->config()->get('target_element')); |
||
99 | $element->Title = 'Auto migrated content'; |
||
100 | |||
101 | // Set the configured field |
||
102 | $element->setField($this->config()->get('target_element_field'), $content); |
||
103 | |||
104 | // Provide an extension hook for further updates to the new element |
||
105 | $this->extend('updateMigratedElement', $element, $content, $page); |
||
106 | |||
107 | // Add and write to the area |
||
108 | $area->Elements()->add($element); |
||
109 | |||
110 | // Publish the record if configured |
||
111 | if ($this->config()->get('publish_changes') && $pageIsLive) { |
||
112 | $page->publishRecursive(); |
||
113 | } |
||
114 | |||
115 | $pageTypeCount++; |
||
116 | } |
||
117 | $count += $pageTypeCount; |
||
118 | echo 'Migrated ' . $pageTypeCount . ' ' . $pageType . ' pages\' content<br>'; |
||
119 | } |
||
120 | echo 'Finished migrating ' . $count . ' pages\' content<br>'; |
||
121 | } |
||
167 |