Completed
Push — develop ( c1b735...79dd51 )
by Nate
02:58
created

deleteLegacyTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\migrations;
10
11
use craft\db\Migration;
12
use craft\helpers\Json;
13
use flipbox\craft\ember\helpers\ObjectHelper;
14
use flipbox\patron\records\Provider;
15
use yii\db\Query;
16
17
class m190426_101341_project_config extends Migration
18
{
19
    /**
20
     * @return bool
21
     * @throws \yii\base\NotSupportedException
22
     */
23
    public function safeUp()
24
    {
25
        $this->addColumn(
26
            Provider::tableName(),
27
            'clientId',
28
            $this->char(Provider::CLIENT_ID_LENGTH)->notNull()
29
        );
30
31
        $this->addColumn(
32
            Provider::tableName(),
33
            'clientSecret',
34
            $this->char(Provider::CLIENT_SECRET_LENGTH)
35
        );
36
37
        if (!$this->providerInstances()) {
38
            return false;
39
        }
40
41
        return $this->deleteLegacyTables();
42
    }
43
44
    /**
45
     * @throws \yii\base\NotSupportedException
46
     */
47
    protected function providerInstances(): bool
48
    {
49
        $tableName = '{{%patron_provider_instances}}';
50
51
        $schema = $this->getDb()->getSchema();
52
        if ($schema->getTableSchema($schema->getRawTableName($tableName)) === null) {
53
            return true;
54
        }
55
56
        $providers = Provider::findAll([
57
            'enabled' => null
58
        ]);
59
60
        foreach ($providers as $provider) {
61
            if (!$query = (new Query())
62
                ->select([
63
                    'clientId',
64
                    'clientSecret',
65
                    'settings'
66
                ])
67
                ->from([$tableName])
68
                ->where([
69
                    'providerId' => $provider->id
70
                ])->one()
71
            ) {
72
                continue;
73
            }
74
75
            $provider->clientId = $query['clientId'];
76
            $provider->clientSecret = $query['clientSecret'];
77
78
            if ($settings = $query['settings']) {
79
                ObjectHelper::populate(
80
                    $provider->getSettings(),
81
                    Json::decodeIfJson($settings)
82
                );
83
            }
84
85
            $provider->save();
86
        }
87
88
        return true;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    protected function deleteLegacyTables(): bool
95
    {
96
        $this->dropTableIfExists('{{%patron_provider_environments}}');
97
        $this->dropTableIfExists('{{%patron_token_environments}}');
98
        $this->dropTableIfExists('{{%patron_provider_instances}}');
99
100
        return true;
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function safeDown()
107
    {
108
        return true;
109
    }
110
}
111