Passed
Push — develop ( 47057d...100e75 )
by Benjamin
05:14
created

m210308_211023_accounts   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A safeUp() 0 39 4
A safeDown() 0 4 1
1
<?php
2
3
namespace dukt\facebook\migrations;
4
5
use Craft;
6
use craft\db\Migration;
7
use craft\helpers\Json;
8
use dukt\facebook\Plugin;
9
10
/**
11
 * m210308_211023_accounts migration.
12
 */
13
class m210308_211023_accounts extends Migration
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function safeUp()
19
    {
20
        if (Craft::$app->db->schema->getTableSchema('{{%facebook_accounts}}') !== null) {
21
            return null;
22
        }
23
24
        $this->createTable(
25
            '{{%facebook_accounts}}',
26
            [
27
                'id'                => $this->primaryKey(),
28
                'token'             => $this->text(),
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
            !$settings->token
41
        ) {
42
            return null;
43
        }
44
45
        $this->insert(
46
            '{{%facebook_accounts}}',
47
            [
48
                'token' => Json::encode($settings->token),
49
            ]
50
        );
51
52
        // Reset token and token secret on the settings
53
        $settings->token = null;
54
55
        $plugin = Craft::$app->getPlugins()->getPlugin('facebook');
56
        Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->toArray());
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function safeDown()
63
    {
64
        echo "m210308_211023_accounts cannot be reverted.\n";
65
        return false;
66
    }
67
}
68