Passed
Pull Request — 1.0 (#1)
by Chris
04:10 queued 50s
created

ZoneMigrationTask::up()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
rs 9.9
cc 4
nc 3
nop 0
1
<?php
2
3
namespace SilverCommerce\GeoZones\Tasks;
4
5
use SilverStripe\ORM\DB;
6
use SilverStripe\Dev\MigrationTask;
7
use SilverStripe\ORM\DatabaseAdmin;
8
use SilverStripe\Control\Controller;
9
use SilverCommerce\GeoZones\Model\Zone;
10
use SilverStripe\Control\Director;
11
12
class ZoneMigrationTask extends MigrationTask
13
{
14
    /**
15
     * Should this task be invoked automatically via dev/build?
16
     *
17
     * @config
18
     *
19
     * @var bool
20
     */
21
    private static $run_during_dev_build = true;
0 ignored issues
show
introduced by
The private property $run_during_dev_build is not used, and could be removed.
Loading history...
22
23
    private static $segment = 'ZoneMigrationTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
24
25
    protected $description = "Upgrade zones to allowing multiple countries";
26
27
28
    /**
29
     * Run this task
30
     *
31
     * @param HTTPRequest $request The current request
0 ignored issues
show
Bug introduced by
The type SilverCommerce\GeoZones\Tasks\HTTPRequest was not found. Did you mean HTTPRequest? If so, make sure to prefix the type with \.
Loading history...
32
     *
33
     * @return void
34
     */
35
    public function run($request)
36
    {
37
        if ($request->getVar('direction') == 'down') {
38
            $this->down();
39
        } else {
40
            $this->up();
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function up()
48
    {
49
        $zones = Zone::get();
50
51
        $this->log('Migrating Zones');
52
        $i = 0;
53
54
        foreach ($zones as $zone) {
55
            $countries = json_decode($zone->Country);
56
57
            if (empty($countries) && isset($zone->Country)) {
58
                $countries = [$zone->Country];
59
                $zone->Country = json_encode($countries);
60
                $zone->write();
61
                $i++;
62
            }
63
        }
64
65
        $this->log("Migrated {$i} Zones");
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function down()
72
    {
73
        $zones = Zone::get();
74
75
        $this->log('Downgrading Zones');
76
        $i = 0;
77
78
        foreach ($zones as $zone) {
79
            $countries = json_decode($zone->Country);
80
81
            if (is_array($countries)) {
82
                $zone->Country = $countries[0];
83
                $zone->write();
84
                $i++;
85
            }
86
        }
87
88
        $this->log("Downgraded {$i} Zones");
89
    }
90
91
    /**
92
     * @param string $text
93
     */
94
    protected function log($text)
95
    {
96
        if (Controller::curr() instanceof DatabaseAdmin) {
97
            DB::alteration_message($text, 'obsolete');
98
        } elseif (Director::is_cli()) {
99
            echo $text . "\n";
100
        } else {
101
            echo $text . "<br/>";
102
        }
103
    }
104
}
105