Passed
Push — develop ( 99eb22...54d191 )
by Benjamin
04:07
created

m190601_092217_tokens::insertDefaultData()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 2
b 0
f 0
nc 16
nop 0
dl 0
loc 22
rs 9.5222
1
<?php
2
3
namespace dukt\videos\migrations;
4
5
use craft\db\Migration;
6
use craft\db\Query;
7
use yii\helpers\Json;
8
9
/**
10
 * m190601_092217_tokens migration.
11
 */
12
class m190601_092217_tokens extends Migration
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function safeUp()
18
    {
19
        if (!$this->db->tableExists('{{%videos_tokens}}')) {
20
            $this->createTables();
21
        }
22
23
        // migrate OAuth client ID & secret
24
        $this->updateOauthClient();
25
    }
26
27
    public function createTables()
28
    {
29
        $this->createTable(
30
            '{{%videos_tokens}}',
31
            [
32
                'id' => $this->primaryKey(),
33
                'gateway' => $this->string()->notNull(),
34
                'accessToken' => $this->text(),
35
36
                'dateCreated' => $this->dateTime()->notNull(),
37
                'dateUpdated' => $this->dateTime()->notNull(),
38
                'uid' => $this->uid()
39
            ]
40
        );
41
42
        $this->createIndex(null, '{{%videos_tokens}}', 'gateway', true);
43
    }
44
45
46
    private function updateOauthClient()
47
    {
48
        if (!$this->db->tableExists('{{%oauth_providers}}')) {
49
            return true;
50
        }
51
52
        // Get OAuth clients for Dailymotion, YouTube and Vimeo, from `oauth_providers` table
53
        $providers = (new Query())
54
            ->select('*')
55
            ->from(['{{%oauth_providers}}'])
56
            ->where(['class' => ['google', 'vimeo']])
57
            ->all();
58
59
        // Get plugin settings
60
        $result = (new Query())
61
            ->select('*')
62
            ->from(['{{%plugins}}'])
63
            ->where(['handle' => 'videos'])
64
            ->one();
65
66
        if (!$result) {
67
            return true;
68
        }
69
70
        $settings = Json::decode($result['settings']);
71
72
        foreach ($providers as $provider) {
73
            switch ($provider['class']) {
74
                case 'dailymotion':
75
                    $providerHandle = 'dailymotion';
76
                    break;
77
                case 'vimeo':
78
                    $providerHandle = 'vimeo';
79
                    break;
80
                case 'google':
81
                    $providerHandle = 'youtube';
82
                    break;
83
                default:
84
                    $providerHandle = null;
85
            }
86
87
            if (!$providerHandle) {
88
                continue;
89
            }
90
91
            $settings['oauthProviderOptions'][$providerHandle]['clientId'] = $provider['clientId'];
92
            $settings['oauthProviderOptions'][$providerHandle]['clientSecret'] = $provider['clientSecret'];
93
        }
94
95
        $this->update('{{%plugins}}', ['settings' => Json::encode($settings)], ['handle' => 'videos']);
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function safeDown()
102
    {
103
        echo "m190601_092217_tokens cannot be reverted.\n";
104
        return false;
105
    }
106
}
107