DatabaseSchemaMigrationTrait::createTokensTable()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 32
cts 32
cp 1
rs 9.264
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Passport\Traits;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Doctrine\DBAL\Connection;
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Schema\Table;
24
use Doctrine\DBAL\Types\Type;
25
use Limoncello\Passport\Contracts\Entities\DatabaseSchemaInterface;
26
27
/**
28
 * @package Limoncello\Passport
29
 */
30
trait DatabaseSchemaMigrationTrait
31
{
32
    /**
33
     * @param Connection              $connection
34
     * @param DatabaseSchemaInterface $schema
35
     *
36
     * @throws DBALException
37
     *
38
     * @return void
39
     */
40 33
    protected function createDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void
41
    {
42
        try {
43 33
            $this->createScopesTable($connection, $schema);
44 32
            $this->createClientsTable($connection, $schema);
45 32
            $this->createRedirectUrisTable($connection, $schema);
46 32
            $this->createTokensTable($connection, $schema);
47 32
            $this->createClientsScopesTable($connection, $schema);
48 32
            $this->createTokensScopesTable($connection, $schema);
49 1
        } catch (DBALException $exception) {
50 1
            if ($connection->isConnected() === true) {
51 1
                $this->removeDatabaseSchema($connection, $schema);
52
            }
53
54 1
            throw $exception;
55
        }
56
    }
57
58
    /**
59
     * @param Connection              $connection
60
     * @param DatabaseSchemaInterface $schema
61
     *
62
     * @return void
63
     */
64 64
    protected function removeDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void
65
    {
66 64
        $manager = $connection->getSchemaManager();
67
68 64
        if ($manager->tablesExist([$schema->getTokensScopesTable()]) === true) {
69 31
            $manager->dropTable($schema->getTokensScopesTable());
70
        }
71 64
        if ($manager->tablesExist([$schema->getClientsScopesTable()]) === true) {
72 31
            $manager->dropTable($schema->getClientsScopesTable());
73
        }
74 64
        if ($manager->tablesExist([$schema->getTokensTable()]) === true) {
75 31
            $manager->dropTable($schema->getTokensTable());
76
        }
77 64
        if ($manager->tablesExist([$schema->getRedirectUrisTable()]) === true) {
78 31
            $manager->dropTable($schema->getRedirectUrisTable());
79
        }
80 64
        if ($manager->tablesExist([$schema->getClientsTable()]) === true) {
81 31
            $manager->dropTable($schema->getClientsTable());
82
        }
83 64
        if ($manager->tablesExist([$schema->getScopesTable()]) === true) {
84 31
            $manager->dropTable($schema->getScopesTable());
85
        }
86
    }
87
88
    /**
89
     * @param Connection              $connection
90
     * @param DatabaseSchemaInterface $schema
91
     *
92
     * @return void
93
     *
94
     * @throws DBALException
95
     */
96 33
    protected function createScopesTable(Connection $connection, DatabaseSchemaInterface $schema): void
97
    {
98 33
        $manager = $connection->getSchemaManager();
99
100 33
        $table = new Table($schema->getScopesTable());
101 33
        $table->addColumn($schema->getScopesIdentityColumn(), Type::STRING)->setNotnull(true);
102 33
        $table->addColumn($schema->getScopesDescriptionColumn(), Type::STRING)->setNotnull(false);
103 33
        $table->addColumn($schema->getScopesCreatedAtColumn(), Type::DATETIME)->setNotnull(true);
104 33
        $table->addColumn($schema->getScopesUpdatedAtColumn(), Type::DATETIME)->setNotnull(false);
105 33
        $table->setPrimaryKey([$schema->getScopesIdentityColumn()]);
106
107 33
        $manager->dropAndCreateTable($table);
108
    }
109
110
    /**
111
     * @param Connection              $connection
112
     * @param DatabaseSchemaInterface $schema
113
     *
114
     * @return void
115
     *
116
     * @throws DBALException
117
     */
118 32
    protected function createClientsTable(Connection $connection, DatabaseSchemaInterface $schema): void
119
    {
120 32
        $manager = $connection->getSchemaManager();
121
122 32
        $table = new Table($schema->getClientsTable());
123 32
        $table->addColumn($schema->getClientsIdentityColumn(), Type::STRING)->setNotnull(true);
124 32
        $table->addColumn($schema->getClientsNameColumn(), Type::STRING)->setNotnull(true);
125 32
        $table->addColumn($schema->getClientsDescriptionColumn(), Type::STRING)->setNotnull(false);
126 32
        $table->addColumn($schema->getClientsCredentialsColumn(), Type::STRING)->setNotnull(false);
127 32
        $table->addColumn($schema->getClientsIsConfidentialColumn(), Type::BOOLEAN)->setDefault(true);
128 32
        $table->addColumn($schema->getClientsIsScopeExcessAllowedColumn(), Type::BOOLEAN)->setDefault(false);
129 32
        $table->addColumn($schema->getClientsIsUseDefaultScopeColumn(), Type::BOOLEAN)->setDefault(true);
130 32
        $table->addColumn($schema->getClientsIsCodeGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
131 32
        $table->addColumn($schema->getClientsIsImplicitGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
132 32
        $table->addColumn($schema->getClientsIsPasswordGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
133 32
        $table->addColumn($schema->getClientsIsClientGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
134 32
        $table->addColumn($schema->getClientsIsRefreshGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
135 32
        $table->addColumn($schema->getClientsCreatedAtColumn(), Type::DATETIME)->setNotnull(true);
136 32
        $table->addColumn($schema->getClientsUpdatedAtColumn(), Type::DATETIME)->setNotnull(false);
137 32
        $table->setPrimaryKey([$schema->getClientsIdentityColumn()]);
138 32
        $manager->dropAndCreateTable($table);
139
    }
140
141
    /**
142
     * @param Connection              $connection
143
     * @param DatabaseSchemaInterface $schema
144
     *
145
     * @return void
146
     *
147
     * @throws DBALException
148
     */
149 32
    protected function createRedirectUrisTable(Connection $connection, DatabaseSchemaInterface $schema): void
150
    {
151 32
        $manager = $connection->getSchemaManager();
152
153 32
        $table = new Table($schema->getRedirectUrisTable());
154 32
        $table->addColumn($schema->getRedirectUrisIdentityColumn(), Type::INTEGER)
155 32
            ->setNotnull(true)->setAutoincrement(true)->setUnsigned(true);
156 32
        $table->addColumn($schema->getRedirectUrisClientIdentityColumn(), Type::STRING)->setNotnull(true);
157 32
        $table->addColumn($schema->getRedirectUrisValueColumn(), Type::STRING)->setNotnull(true);
158 32
        $table->addColumn($schema->getRedirectUrisCreatedAtColumn(), Type::DATETIME)->setNotnull(true);
159 32
        $table->addColumn($schema->getRedirectUrisUpdatedAtColumn(), Type::DATETIME)->setNotnull(false);
160 32
        $table->setPrimaryKey([$schema->getRedirectUrisIdentityColumn()]);
161
162 32
        $table->addForeignKeyConstraint(
163 32
            $schema->getClientsTable(),
164 32
            [$schema->getRedirectUrisClientIdentityColumn()],
165 32
            [$schema->getClientsIdentityColumn()],
166 32
            $this->getOnDeleteCascadeConstraint()
167
        );
168
169 32
        $manager->dropAndCreateTable($table);
170
    }
171
172
    /**
173
     * @param Connection              $connection
174
     * @param DatabaseSchemaInterface $schema
175
     *
176
     * @return void
177
     *
178
     * @throws DBALException
179
     */
180 32
    protected function createTokensTable(Connection $connection, DatabaseSchemaInterface $schema): void
181
    {
182 32
        $manager = $connection->getSchemaManager();
183
184 32
        $table = new Table($schema->getTokensTable());
185 32
        $table->addColumn($schema->getTokensIdentityColumn(), Type::INTEGER)
186 32
            ->setNotnull(true)->setAutoincrement(true)->setUnsigned(true);
187 32
        $table->addColumn($schema->getTokensIsEnabledColumn(), Type::BOOLEAN)->setNotnull(true)->setDefault(true);
188 32
        $table->addColumn($schema->getTokensIsScopeModified(), Type::BOOLEAN)->setNotnull(true)->setDefault(false);
189 32
        $table->addColumn($schema->getTokensClientIdentityColumn(), Type::STRING)->setNotnull(true);
190 32
        $table->addColumn($schema->getTokensUserIdentityColumn(), Type::INTEGER)->setNotnull(false)->setUnsigned(true);
191 32
        $table->addColumn($schema->getTokensRedirectUriColumn(), Type::STRING)->setNotnull(false);
192 32
        $table->addColumn($schema->getTokensCodeColumn(), Type::STRING)->setNotnull(false);
193 32
        $table->addColumn($schema->getTokensTypeColumn(), Type::STRING)->setNotnull(false);
194 32
        $table->addColumn($schema->getTokensValueColumn(), Type::STRING)->setNotnull(false);
195 32
        $table->addColumn($schema->getTokensRefreshColumn(), Type::STRING)->setNotnull(false);
196 32
        $table->addColumn($schema->getTokensCodeCreatedAtColumn(), Type::DATETIME)->setNotnull(false);
197 32
        $table->addColumn($schema->getTokensValueCreatedAtColumn(), Type::DATETIME)->setNotnull(false);
198 32
        $table->addColumn($schema->getTokensRefreshCreatedAtColumn(), Type::DATETIME)->setNotnull(false);
199 32
        $table->setPrimaryKey([$schema->getTokensIdentityColumn()]);
200
201 32
        $table->addForeignKeyConstraint(
202 32
            $schema->getClientsTable(),
203 32
            [$schema->getTokensClientIdentityColumn()],
204 32
            [$schema->getClientsIdentityColumn()],
205 32
            $this->getOnDeleteCascadeConstraint()
206
        );
207
208 32
        $usersTable          = $schema->getUsersTable();
209 32
        $usersIdentityColumn = $schema->getUsersIdentityColumn();
210 32
        if ($usersTable !== null && $usersIdentityColumn !== null) {
211 30
            $table->addForeignKeyConstraint(
212 30
                $usersTable,
213 30
                [$schema->getTokensUserIdentityColumn()],
214 30
                [$usersIdentityColumn],
215 30
                $this->getOnDeleteCascadeConstraint()
216
            );
217
        }
218
219 32
        $manager->dropAndCreateTable($table);
220
    }
221
222
    /**
223
     * @param Connection              $connection
224
     * @param DatabaseSchemaInterface $schema
225
     *
226
     * @return void
227
     *
228
     * @throws DBALException
229
     */
230 32 View Code Duplication
    protected function createClientsScopesTable(Connection $connection, DatabaseSchemaInterface $schema): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
    {
232 32
        $manager = $connection->getSchemaManager();
233
234 32
        $table = new Table($schema->getClientsScopesTable());
235 32
        $table->addColumn($schema->getClientsScopesIdentityColumn(), Type::INTEGER)
236 32
            ->setNotnull(true)->setAutoincrement(true)->setUnsigned(true);
237 32
        $table->addColumn($schema->getClientsScopesClientIdentityColumn(), Type::STRING)->setNotnull(true);
238 32
        $table->addColumn($schema->getClientsScopesScopeIdentityColumn(), Type::STRING)->setNotnull(true);
239 32
        $table->setPrimaryKey([$schema->getClientsScopesIdentityColumn()]);
240 32
        $table->addUniqueIndex([
241 32
            $schema->getClientsScopesClientIdentityColumn(),
242 32
            $schema->getClientsScopesScopeIdentityColumn()
243
        ]);
244
245 32
        $table->addForeignKeyConstraint(
246 32
            $schema->getClientsTable(),
247 32
            [$schema->getClientsScopesClientIdentityColumn()],
248 32
            [$schema->getClientsIdentityColumn()],
249 32
            $this->getOnDeleteCascadeConstraint()
250
        );
251
252 32
        $table->addForeignKeyConstraint(
253 32
            $schema->getScopesTable(),
254 32
            [$schema->getClientsScopesScopeIdentityColumn()],
255 32
            [$schema->getScopesIdentityColumn()],
256 32
            $this->getOnDeleteCascadeConstraint()
257
        );
258
259 32
        $manager->dropAndCreateTable($table);
260
    }
261
262
    /**
263
     * @param Connection              $connection
264
     * @param DatabaseSchemaInterface $schema
265
     *
266
     * @return void
267
     *
268
     * @throws DBALException
269
     */
270 32 View Code Duplication
    protected function createTokensScopesTable(Connection $connection, DatabaseSchemaInterface $schema): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
    {
272 32
        $manager = $connection->getSchemaManager();
273
274 32
        $table = new Table($schema->getTokensScopesTable());
275 32
        $table->addColumn($schema->getTokensScopesIdentityColumn(), Type::INTEGER)
276 32
            ->setNotnull(true)->setAutoincrement(true)->setUnsigned(true);
277 32
        $table->addColumn($schema->getTokensScopesTokenIdentityColumn(), Type::INTEGER)->setNotnull(true)
278 32
            ->setUnsigned(true);
279 32
        $table->addColumn($schema->getTokensScopesScopeIdentityColumn(), Type::STRING)->setNotnull(true);
280 32
        $table->setPrimaryKey([$schema->getTokensScopesIdentityColumn()]);
281 32
        $table->addUniqueIndex([
282 32
            $schema->getTokensScopesTokenIdentityColumn(),
283 32
            $schema->getTokensScopesScopeIdentityColumn()
284
        ]);
285
286 32
        $table->addForeignKeyConstraint(
287 32
            $schema->getTokensTable(),
288 32
            [$schema->getTokensScopesTokenIdentityColumn()],
289 32
            [$schema->getTokensIdentityColumn()],
290 32
            $this->getOnDeleteCascadeConstraint()
291
        );
292
293 32
        $table->addForeignKeyConstraint(
294 32
            $schema->getScopesTable(),
295 32
            [$schema->getTokensScopesScopeIdentityColumn()],
296 32
            [$schema->getScopesIdentityColumn()],
297 32
            $this->getOnDeleteCascadeConstraint()
298
        );
299
300 32
        $manager->dropAndCreateTable($table);
301
    }
302
303
    /**
304
     * @return array
305
     */
306 32
    protected function getOnDeleteCascadeConstraint(): array
307
    {
308 32
        return ['onDelete' => 'CASCADE'];
309
    }
310
}
311