LocationToPageTask   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 24
c 1
b 0
f 0
dl 0
loc 68
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 3 1
B migrateLocations() 0 28 8
A getLocations() 0 4 2
1
<?php
2
3
namespace Dynamic\Locator\Tasks;
4
5
use Dynamic\Locator\Location;
6
use Dynamic\Locator\Page\LocationPage;
7
use Dynamic\Locator\Page\Locator;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\BuildTask;
10
use SilverStripe\Versioned\Versioned;
11
12
/**
13
 * Class LocationToPageTask
14
 * @package Dynamic\Locator\Tasks
15
 */
16
class LocationToPageTask extends BuildTask
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $title = 'Locator - Location to Page Task';
22
23
    /**
24
     * @var string
25
     */
26
    private static $segment = 'locator-location-to-page-task';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
27
28
    /**
29
     * @var bool
30
     */
31
    private static $auto_publish_location = false;
0 ignored issues
show
introduced by
The private property $auto_publish_location is not used, and could be removed.
Loading history...
32
33
    /**
34
     * @param \SilverStripe\Control\HTTPRequest $request
35
     */
36
    public function run($request)
37
    {
38
        $this->migrateLocations();
39
    }
40
41
    /**
42
     *
43
     */
44
    protected function migrateLocations()
45
    {
46
        if (!$parent = Locator::get()->first()) {
47
            $parent = Locator::create();
48
            $parent->Title = "Locator";
49
            $parent->writeToStage(Versioned::DRAFT);
50
        }
51
52
        /** @var Location $location */
53
        foreach ($this->getLocations() as $location) {
54
            if (!LocationPage::get()->filter('LegacyObjectID', $location->ID)->first()) {
55
                if ($location->hasMethod('isPublished')) {
56
                    $published = $location->isPublished();
57
                }
58
59
                /** @var LocationPage $newLocation */
60
                $newLocation = Injector::inst()->create(LocationPage::class, $location->toMap(), false);
61
                $newLocation->setClassName(LocationPage::class);
62
                $newLocation->ID = null;
63
                $newLocation->ParentID = $parent->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
                $newLocation->LegacyObjectID = $location->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property LegacyObjectID does not exist on Dynamic\Locator\Page\LocationPage. Since you implemented __set, consider adding a @property annotation.
Loading history...
65
66
                $this->extend('preLocationMigrationUpdate', $location, $newLocation);
67
68
                $newLocation->writeToStage(Versioned::DRAFT);
69
70
                if ((isset($published) && $published) || $this->config()->get('auto_publish_location')) {
71
                    $newLocation->publishSingle();
72
                }
73
            }
74
        }
75
    }
76
77
    /**
78
     * @return \Generator
79
     */
80
    protected function getLocations()
81
    {
82
        foreach (Location::get() as $location) {
83
            yield $location;
84
        }
85
    }
86
}
87