AbstractInstall   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 8
dl 0
loc 243
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
getIdentityTableName() 0 1 ?
getProviderTableName() 0 1 ?
A safeUp() 0 19 2
A installKeyChain() 0 6 2
A getProviderFields() 0 27 1
A createTables() 0 31 2
A safeDown() 0 10 1
B createIndexes() 0 61 2
A addForeignKeys() 0 45 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 2/9/18
6
 * Time: 2:23 PM
7
 */
8
9
namespace flipbox\saml\core\migrations;
10
11
use craft\db\Migration;
12
use craft\records\Site;
13
use craft\records\User;
14
use flipbox\keychain\records\KeyChainRecord;
15
use yii\base\InvalidConfigException;
16
use flipbox\saml\core\models\SettingsInterface;
17
use flipbox\saml\core\records\AbstractProvider;
18
use flipbox\saml\core\records\LinkRecord;
19
20
abstract class AbstractInstall extends Migration
21
{
22
23
    const PROVIDER_AFTER_COLUMN = 'sha256';
24
    protected $linkTableExist = false;
25
26
    /**
27
     * @return string
28
     */
29
    abstract protected function getIdentityTableName(): string;
30
31
    /**
32
     * @return string
33
     */
34
    abstract protected function getProviderTableName(): string;
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function safeUp()
40
    {
41
        try {
42
            /**
43
             * if it doesn't exist, this will throw an exception
44
             */
45
            LinkRecord::getTableSchema();
46
            $this->linkTableExist=true;
47
        } catch (InvalidConfigException $e) {
48
            \Craft::warning('Link table doesn\'t exist. Going to create it and the indexes.');
49
        }
50
51
        $this->installKeyChain();
52
        $this->createTables();
53
        $this->createIndexes();
54
        $this->addForeignKeys();
55
56
        return true;
57
    }
58
59
    protected function installKeyChain()
60
    {
61
        if (! \Craft::$app->plugins->getPlugin('keychain')) {
62
            \Craft::$app->plugins->installPlugin('keychain');
63
        }
64
    }
65
66
    protected function getProviderFields()
67
    {
68
        return [
69
            'id' => $this->primaryKey(),
70
            'label' => $this->string(64),
71
            'entityId' => $this->string()->notNull(),
72
            'metadata' => $this->mediumText()->notNull(),
73
            'sha256' => $this->string()->notNull(),
74
            'providerType' => $this->enum('providerType', [
75
                SettingsInterface::SP,
76
                SettingsInterface::IDP,
77
            ])->notNull(),
78
            'encryptAssertions' => $this->boolean()->defaultValue(false)->notNull(),
79
            'encryptionMethod' => $this->string(64)->null(),
80
            'siteId' => $this->integer()->null(),
81
            'groupOptions' => $this->text(),
82
            'metadataOptions' => $this->text(),
83
            'syncGroups' => $this->boolean()->defaultValue(true)->notNull(),
84
            'groupsAttributeName' => $this->string(64)->defaultValue(AbstractProvider::DEFAULT_GROUPS_ATTRIBUTE_NAME),
85
            'nameIdOverride' => $this->text(),
86
            'mapping' => $this->text(),
87
            'enabled' => $this->boolean()->defaultValue(true)->notNull(),
88
            'dateUpdated' => $this->dateTime()->notNull(),
89
            'dateCreated' => $this->dateTime()->notNull(),
90
            'uid' => $this->uid(),
91
        ];
92
    }
93
94
    /**
95
     * Creates the tables.
96
     *
97
     * @return void
98
     */
99
    protected function createTables()
100
    {
101
102
103
        $this->createTable($this->getProviderTableName(), $this->getProviderFields());
104
105
        if ($this->linkTableExist === false) {
106
            $this->createTable(LinkRecord::tableName(), [
107
                'id' => $this->primaryKey(),
108
                'providerId' => $this->integer()->notNull(),
109
                'providerUid' => $this->uid()->notNull(),
110
                'keyChainId' => $this->integer()->notNull(),
111
                'dateUpdated' => $this->dateTime()->notNull(),
112
                'dateCreated' => $this->dateTime()->notNull(),
113
                'uid' => $this->uid(),
114
            ]);
115
        }
116
117
        $this->createTable($this->getIdentityTableName(), [
118
            'id' => $this->primaryKey(),
119
            'providerId' => $this->integer()->notNull(),
120
            'userId' => $this->integer()->notNull(),
121
            'nameId' => $this->string()->notNull(),
122
            'sessionId' => $this->string()->null(),
123
            'enabled' => $this->boolean()->defaultValue(true)->notNull(),
124
            'lastLoginDate' => $this->dateTime()->notNull(),
125
            'dateUpdated' => $this->dateTime()->notNull(),
126
            'dateCreated' => $this->dateTime()->notNull(),
127
            'uid' => $this->uid(),
128
        ]);
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    public function safeDown()
135
    {
136
137
138
        // Delete tables
139
        $this->dropTableIfExists($this->getIdentityTableName());
140
        $this->dropTableIfExists(LinkRecord::tableName());
141
        $this->dropTableIfExists($this->getProviderTableName());
142
        return true;
143
    }
144
145
    /**
146
     * Creates the indexes.
147
     *
148
     * @return void
149
     */
150
    protected function createIndexes()
151
    {
152
153
        $this->createIndex(
154
            $this->db->getIndexName($this->getProviderTableName(), 'entityId', false, true),
155
            $this->getProviderTableName(),
156
            'entityId',
157
            false
158
        );
159
        $this->createIndex(
160
            $this->db->getIndexName($this->getProviderTableName(), [
161
                'sha256',
162
            ], true, true),
163
            $this->getProviderTableName(),
164
            [
165
                'sha256',
166
            ],
167
            true
168
        );
169
170
        if ($this->linkTableExist === false) {
171
            $this->createIndex(
172
                $this->db->getIndexName(LinkRecord::tableName(), [
173
                    'providerUid',
174
                    'keyChainId',
175
                ], true, true),
176
                LinkRecord::tableName(),
177
                [
178
                    'providerUid',
179
                    'keyChainId',
180
                ],
181
                true
182
            );
183
        }
184
185
        $this->createIndex(
186
            $this->db->getIndexName($this->getIdentityTableName(), 'nameId', false, true),
187
            $this->getIdentityTableName(),
188
            'nameId',
189
            false
190
        );
191
192
        $this->createIndex(
193
            $this->db->getIndexName(
194
                $this->getIdentityTableName(),
195
                [
196
                    'providerId',
197
                    'nameId',
198
                    'userId',
199
                ],
200
                true
201
            ),
202
            $this->getIdentityTableName(),
203
            [
204
                'providerId',
205
                'nameId',
206
                'userId',
207
            ],
208
            true
209
        );
210
    }
211
212
    /**
213
     * Adds the foreign keys.
214
     *
215
     * @return void
216
     */
217
    protected function addForeignKeys()
218
    {
219
220
        if ($this->linkTableExist === false) {
221
            /**
222
             * Link KeyChain
223
             */
224
            $this->addForeignKey(
225
                $this->db->getForeignKeyName(LinkRecord::tableName(), 'keyChainId'),
226
                LinkRecord::tableName(),
227
                'keyChainId',
228
                KeyChainRecord::tableName(),
229
                'id',
230
                'CASCADE'
231
            );
232
        }
233
234
        $this->addForeignKey(
235
            $this->db->getForeignKeyName($this->getIdentityTableName(), 'userId'),
236
            $this->getIdentityTableName(),
237
            'userId',
238
            User::tableName(),
239
            'id',
240
            'CASCADE'
241
        );
242
        $this->addForeignKey(
243
            $this->db->getForeignKeyName($this->getIdentityTableName(), 'providerId'),
244
            $this->getIdentityTableName(),
245
            'providerId',
246
            $this->getProviderTableName(),
247
            'id',
248
            'CASCADE'
249
        );
250
251
        $this->addForeignKey(
252
            $this->db->getForeignKeyName(
253
                $this->getProviderTableName(),
254
                'siteId'
255
            ),
256
            $this->getProviderTableName(),
257
            'siteId',
258
            Site::tableName(),
259
            'id'
260
        );
261
    }
262
}
263