Completed
Push — develop ( 856b14...45ab37 )
by Nate
17:44
created

m181019_220655_provider_instances::safeUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 134

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 134
ccs 0
cts 114
cp 0
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2

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
/**
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\db\Query;
13
use craft\helpers\MigrationHelper;
14
use flipbox\patron\events\handlers\BeforeInsertProviderInstance;
15
use flipbox\patron\records\Provider;
16
use flipbox\patron\records\ProviderEnvironment;
17
use flipbox\patron\records\ProviderInstance;
18
use yii\base\Event;
19
20
class m181019_220655_provider_instances extends Migration
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    public function safeUp()
26
    {
27
        // No insert events please...
28
        Event::off(
29
            ProviderInstance::class,
30
            ProviderInstance::EVENT_BEFORE_INSERT,
31
            [
32
                BeforeInsertProviderInstance::class,
33
                'handle'
34
            ]
35
        );
36
37
        $this->createTable(
38
            ProviderInstance::tableName(),
39
            [
40
                'id' => $this->primaryKey(),
41
                'providerId' => $this->integer()->notNull(),
42
                'clientId' => $this->char(ProviderInstance::CLIENT_ID_LENGTH)->notNull(),
43
                'clientSecret' => $this->char(ProviderInstance::CLIENT_SECRET_LENGTH),
44
                'settings' => $this->text(),
45
                'dateCreated' => $this->dateTime()->notNull(),
46
                'dateUpdated' => $this->dateTime()->notNull(),
47
                'uid' => $this->uid(),
48
            ]
49
        );
50
51
        $this->addForeignKey(
52
            $this->db->getForeignKeyName(
53
                ProviderInstance::tableName(),
54
                'providerId'
55
            ),
56
            ProviderInstance::tableName(),
57
            'providerId',
58
            Provider::tableName(),
59
            'id',
60
            'CASCADE'
61
        );
62
63
        $this->addColumn(
64
            ProviderEnvironment::tableName(),
65
            'instanceId',
66
            $this->integer()
67
        );
68
69
        // Migrates all the existing providers to new 'instance' types
70
        $this->migrateToInstances();
71
72
        // Make the provider handle unique
73
        $this->createIndex(
74
            $this->db->getIndexName(
75
                Provider::tableName(),
76
                'handle',
77
                true,
78
                false
79
            ),
80
            Provider::tableName(),
81
            'handle',
82
            true
83
        );
84
85
        $this->dropColumn(
86
            Provider::tableName(),
87
            'settings'
88
        );
89
90
        $this->dropColumn(
91
            Provider::tableName(),
92
            'clientId'
93
        );
94
95
        $this->dropColumn(
96
            Provider::tableName(),
97
            'clientSecret'
98
        );
99
100
        // We should be able to enforce notNull now
101
        $this->alterColumn(
102
            ProviderEnvironment::tableName(),
103
            'instanceId',
104
            $this->integer()->notNull()
105
        );
106
107
        // Apply foreign key to settings
108
        $this->addForeignKey(
109
            $this->db->getForeignKeyName(
110
                ProviderEnvironment::tableName(),
111
                'instanceId'
112
            ),
113
            ProviderEnvironment::tableName(),
114
            'instanceId',
115
            ProviderInstance::tableName(),
116
            'id',
117
            'CASCADE'
118
        );
119
120
        // Remove existing foreign key
121
        $this->dropForeignKey(
122
            $this->db->getForeignKeyName(
123
                ProviderEnvironment::tableName(),
124
                'providerId'
125
            ),
126
            ProviderEnvironment::tableName()
127
        );
128
129
        $this->dropColumn(
130
            ProviderEnvironment::tableName(),
131
            'providerId'
132
        );
133
134
        $this->dropPrimaryKey(
135
            $this->db->getPrimaryKeyName(
136
                ProviderEnvironment::tableName(),
137
                [
138
                    'environment'
139
                ]
140
            ),
141
            ProviderEnvironment::tableName()
142
        );
143
144
        $this->addPrimaryKey(
145
            $this->db->getPrimaryKeyName(
146
                ProviderEnvironment::tableName(),
147
                [
148
                    'instanceId',
149
                    'environment'
150
                ]
151
            ),
152
            ProviderEnvironment::tableName(),
153
            [
154
                'instanceId',
155
                'environment'
156
            ]
157
        );
158
    }
159
160
    /**
161
     *
162
     */
163
    protected function migrateToInstances()
164
    {
165
        $query = (new Query)
166
            ->select(['id', 'settings', 'clientId', 'clientSecret'])
167
            ->from(Provider::tableName());
168
169
        $rows = $query->all();
170
171
        foreach ($rows as $row) {
172
            $instance = new ProviderInstance();
173
            $instance->setProviderId($row['id']);
174
            $instance->settings = $row['settings'];
175
            $instance->clientId = $row['clientId'];
176
            $instance->clientSecret = $row['clientSecret'];
177
178
            if (!$instance->save()) {
179
                continue;
180
            }
181
182
            /** @var ProviderEnvironment[] $environments */
183
            $environments = ProviderEnvironment::findAll([
184
                'providerId' => $row['id']
185
            ]);
186
187
            foreach ($environments as $environment) {
188
                $environment->instanceId = $instance->getId();
189
                $environment->save(true, ['instanceId']);
190
            }
191
        }
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197
    public function safeDown()
198
    {
199
        $this->dropTableIfExists(ProviderInstance::tableName());
200
201
        return true;
202
    }
203
}
204