Test Setup Failed
Pull Request — master (#197)
by Gorrie
01:07
created

src/Tasks/MigrateContentToElement.php (3 issues)

1
<?php
2
3
namespace DNADesign\Elemental\Tasks;
4
5
use DNADesign\Elemental\Models\ElementalArea;
6
use DNADesign\Elemental\Models\ElementContent;
7
8
use SilverStripe\Dev\BuildTask;
9
10
class MigrateContentToElement extends BuildTask
11
{
12
13
    protected $title = 'MigrateContentToElement';
14
15
    protected $description = 'When installing Elemental this task converts content in the $Content field to an ElementContent';
16
17
    public function run($request)
18
    {
19
        // TODO: needs rewriting for multiple elemental areas
20
        $pageTypes = ElementalArea::elemental_page_types();
0 ignored issues
show
The method elemental_page_types() does not exist on DNADesign\Elemental\Models\ElementalArea. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        /** @scrutinizer ignore-call */ 
21
        $pageTypes = ElementalArea::elemental_page_types();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
        $count = 0;
22
        foreach ($pageTypes as $pageType) {
23
            $pages = $pageType::get()->filter('ElementalAreaID', 0);
24
            foreach ($pages as $page) {
25
                $content = $page->Content;
26
                $page->Content = '';
27
                // trigger area relations to be setup
28
                $page->write();
29
                $area = $page->ElementalArea();
30
                $element = new ElementContent();
31
                $element->Title = 'Auto migrated content';
32
                $element->HTML = $content;
0 ignored issues
show
Bug Best Practice introduced by
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
33
                $element->ParentID = $area->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
34
                $element->write();
35
            }
36
            $count += $pages->Count();
37
            echo 'Migrated ' . $pages->Count() . ' ' . $pageType . ' pages\' content<br>';
38
        }
39
        echo 'Finished migrating ' . $count . ' pages\' content<br>';
40
    }
41
}
42