m190601_092217_tokens::createTables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.9666
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(): void
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(): void
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
        if (!isset($result['settings'])) {
71
            return true;
72
        }
73
74
        $settings = Json::decode($result['settings']);
75
76
        foreach ($providers as $provider) {
77
            switch ($provider['class']) {
78
                case 'dailymotion':
79
                    $providerHandle = 'dailymotion';
80
                    break;
81
                case 'vimeo':
82
                    $providerHandle = 'vimeo';
83
                    break;
84
                case 'google':
85
                    $providerHandle = 'youtube';
86
                    break;
87
                default:
88
                    $providerHandle = null;
89
            }
90
91
            if ($providerHandle === null) {
92
                continue;
93
            }
94
95
            $settings['oauthProviderOptions'][$providerHandle]['clientId'] = $provider['clientId'];
96
            $settings['oauthProviderOptions'][$providerHandle]['clientSecret'] = $provider['clientSecret'];
97
        }
98
99
        $this->update('{{%plugins}}', ['settings' => Json::encode($settings)], ['handle' => 'videos']);
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function safeDown(): bool
106
    {
107
        echo "m190601_092217_tokens cannot be reverted.\n";
108
        return false;
109
    }
110
}
111