Passed
Push — master ( 0b518c...5d9f70 )
by Benjamin
08:02 queued 13s
created

m210307_101413_accounts::safeDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace dukt\twitter\migrations;
4
5
use Craft;
6
use craft\db\Migration;
7
use dukt\twitter\Plugin;
8
9
/**
10
 * m210307_101413_accounts migration.
11
 */
12
class m210307_101413_accounts extends Migration
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function safeUp()
18
    {
19
        if (Craft::$app->db->schema->getTableSchema('{{%twitter_accounts}}') !== null) {
20
            return null;
21
        }
22
23
        $this->createTable(
24
            '{{%twitter_accounts}}',
25
            [
26
                'id'                => $this->primaryKey(),
27
                'token'             => $this->string(50),
28
                'tokenSecret'       => $this->string(50),
29
                'dateCreated'       => $this->dateTime()->notNull(),
30
                'dateUpdated'       => $this->dateTime()->notNull(),
31
                'uid'               => $this->uid()
32
            ]
33
        );
34
35
        // If OAuth token is set on the settings, keep it
36
        $settings = Plugin::$plugin->getSettings();
37
38
        if (
39
            !isset($settings->token) ||
40
            !isset($settings->tokenSecret) ||
41
            !$settings->token ||
42
            !$settings->tokenSecret
43
        ) {
44
            return null;
45
        }
46
47
        $this->insert(
48
        '{{%twitter_accounts}}',
49
            [
50
                'token' => $settings->token,
51
                'tokenSecret' => $settings->tokenSecret,
52
            ]
53
        );
54
55
        // Reset token and token secret on the settings
56
        $settings->token = null;
57
        $settings->tokenSecret = null;
58
59
        $plugin = Craft::$app->getPlugins()->getPlugin('twitter');
60
        Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->toArray());
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function safeDown()
67
    {
68
        echo "m210307_101413_accounts cannot be reverted.\n";
69
        return false;
70
    }
71
}
72