Passed
Push — master ( 52718a...4c1d96 )
by Benjamin
11:03 queued 03:42
created

m190601_092217_tokens::updateOauthClient()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 35
c 2
b 0
f 0
nc 12
nop 0
dl 0
loc 54
rs 8.0555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        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) {
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()
106
    {
107
        echo "m190601_092217_tokens cannot be reverted.\n";
108
        return false;
109
    }
110
}
111