Passed
Push — develop ( c511ae...c7368a )
by Benjamin
05:05
created

m180529_125418_info::insertDefaultData()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
nc 18
nop 0
dl 0
loc 46
rs 8.8497
c 0
b 0
f 0
1
<?php
2
3
namespace dukt\analytics\migrations;
4
5
use craft\db\Migration;
6
use craft\db\Query;
7
use craft\helpers\Json;
8
use dukt\analytics\models\Info;
9
use dukt\analytics\Plugin;
10
11
/**
12
 * m180529_125418_info migration.
13
 */
14
class m180529_125418_info extends Migration
15
{
16
    /**
17
     * @inheritdoc
18
     */
19
    public function safeUp()
20
    {
21
        $this->createTables();
22
        $this->insertDefaultData();
23
    }
24
25
    /**
26
     * Creates the tables.
27
     */
28
    public function createTables()
29
    {
30
        $this->createTable(
31
            '{{%analytics_info}}',
32
            [
33
                'id' => $this->primaryKey(),
34
                'forceConnect' => $this->boolean()->defaultValue(false)->notNull(),
35
                'token' => $this->text(),
36
37
                'dateCreated' => $this->dateTime()->notNull(),
38
                'dateUpdated' => $this->dateTime()->notNull(),
39
                'uid' => $this->uid()
40
            ]
41
        );
42
    }
43
44
    /**
45
     * Populates the DB with the default data.
46
     */
47
    public function insertDefaultData()
48
    {
49
        $forceConnect = false;
50
        $token = null;
51
        $settings = null;
52
53
        $row = (new Query())
54
            ->select('*')
55
            ->from(['{{%plugins}}'])
56
            ->where(['handle' => 'analytics'])
57
            ->one();
58
59
        if ($row) {
60
            $settingsJson = $row['settings'];
61
            $settings = Json::decode($settingsJson);
62
        }
63
64
        if($settings) {
65
            $forceConnect = $settings['forceConnect'];
66
67
            if(isset($settings['token'])) {
68
                $token = $settings['token'];
69
            }
70
        }
71
72
        // Populate the analytics_info table
73
        echo '    > populate the analytics_info table ...';
74
        Plugin::getInstance()->saveInfo(new Info([
75
            'forceConnect' => $forceConnect,
76
            'token' => $token,
77
        ]));
78
        echo " done\n";
79
80
        // Get rid of the old plugin settings
81
        echo '    > remove old plugin settings ...';
82
        if($settings) {
83
            unset($settings['forceConnect']);
84
85
            if(isset($settings['token'])) {
86
                unset($settings['token']);
87
            }
88
89
            $settingsJson = Json::encode($settings);
90
            $this->update('{{%plugins}}', ['settings' => $settingsJson], ['id' => $row['id']]);
91
        }
92
        echo " done\n";
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function safeDown()
99
    {
100
        echo "m180529_125418_info cannot be reverted.\n";
101
        return false;
102
    }
103
}
104