1
|
|
|
<?php namespace Limoncello\Passport\Traits; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Doctrine\DBAL\Connection; |
20
|
|
|
use Doctrine\DBAL\DBALException; |
21
|
|
|
use Doctrine\DBAL\Schema\Table; |
22
|
|
|
use Doctrine\DBAL\Types\Type; |
23
|
|
|
use Limoncello\Passport\Contracts\Entities\DatabaseSchemaInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Limoncello\Passport |
27
|
|
|
*/ |
28
|
|
|
trait DatabaseSchemaMigrationTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param Connection $connection |
32
|
|
|
* @param DatabaseSchemaInterface $schema |
33
|
|
|
* |
34
|
|
|
* @throws DBALException |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
32 |
|
protected function createDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
32 |
|
$this->createScopesTable($connection, $schema); |
42
|
31 |
|
$this->createClientsTable($connection, $schema); |
43
|
31 |
|
$this->createRedirectUrisTable($connection, $schema); |
44
|
31 |
|
$this->createTokensTable($connection, $schema); |
45
|
31 |
|
$this->createClientsScopesTable($connection, $schema); |
46
|
31 |
|
$this->createTokensScopesTable($connection, $schema); |
47
|
1 |
|
} catch (DBALException $exception) { |
48
|
1 |
|
if ($connection->isConnected() === true) { |
49
|
1 |
|
$this->removeDatabaseSchema($connection, $schema); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
throw $exception; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Connection $connection |
58
|
|
|
* @param DatabaseSchemaInterface $schema |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
32 |
|
protected function removeDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void |
63
|
|
|
{ |
64
|
32 |
|
$manager = $connection->getSchemaManager(); |
65
|
|
|
|
66
|
32 |
|
if ($manager->tablesExist([$schema->getTokensScopesTable()]) === true) { |
67
|
30 |
|
$manager->dropTable($schema->getTokensScopesTable()); |
68
|
|
|
} |
69
|
32 |
|
if ($manager->tablesExist([$schema->getClientsScopesTable()]) === true) { |
70
|
30 |
|
$manager->dropTable($schema->getClientsScopesTable()); |
71
|
|
|
} |
72
|
32 |
|
if ($manager->tablesExist([$schema->getTokensTable()]) === true) { |
73
|
30 |
|
$manager->dropTable($schema->getTokensTable()); |
74
|
|
|
} |
75
|
32 |
|
if ($manager->tablesExist([$schema->getRedirectUrisTable()]) === true) { |
76
|
30 |
|
$manager->dropTable($schema->getRedirectUrisTable()); |
77
|
|
|
} |
78
|
32 |
|
if ($manager->tablesExist([$schema->getClientsTable()]) === true) { |
79
|
30 |
|
$manager->dropTable($schema->getClientsTable()); |
80
|
|
|
} |
81
|
32 |
|
if ($manager->tablesExist([$schema->getScopesTable()]) === true) { |
82
|
30 |
|
$manager->dropTable($schema->getScopesTable()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Connection $connection |
88
|
|
|
* @param DatabaseSchemaInterface $schema |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
32 |
|
protected function createScopesTable(Connection $connection, DatabaseSchemaInterface $schema): void |
93
|
|
|
{ |
94
|
32 |
|
$manager = $connection->getSchemaManager(); |
95
|
|
|
|
96
|
32 |
|
$table = new Table($schema->getScopesTable()); |
97
|
32 |
|
$table->addColumn($schema->getScopesIdentityColumn(), Type::STRING)->setNotnull(true); |
98
|
32 |
|
$table->addColumn($schema->getScopesDescriptionColumn(), Type::STRING)->setNotnull(false); |
99
|
32 |
|
$table->addColumn($schema->getScopesCreatedAtColumn(), Type::DATETIME)->setNotnull(true); |
100
|
32 |
|
$table->addColumn($schema->getScopesUpdatedAtColumn(), Type::DATETIME)->setNotnull(false); |
101
|
32 |
|
$table->setPrimaryKey([$schema->getScopesIdentityColumn()]); |
102
|
|
|
|
103
|
32 |
|
$manager->dropAndCreateTable($table); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Connection $connection |
108
|
|
|
* @param DatabaseSchemaInterface $schema |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
31 |
|
protected function createClientsTable(Connection $connection, DatabaseSchemaInterface $schema): void |
113
|
|
|
{ |
114
|
31 |
|
$manager = $connection->getSchemaManager(); |
115
|
|
|
|
116
|
31 |
|
$table = new Table($schema->getClientsTable()); |
117
|
31 |
|
$table->addColumn($schema->getClientsIdentityColumn(), Type::STRING)->setNotnull(true); |
118
|
31 |
|
$table->addColumn($schema->getClientsNameColumn(), Type::STRING)->setNotnull(true); |
119
|
31 |
|
$table->addColumn($schema->getClientsDescriptionColumn(), Type::STRING)->setNotnull(false); |
120
|
31 |
|
$table->addColumn($schema->getClientsCredentialsColumn(), Type::STRING)->setNotnull(false); |
121
|
31 |
|
$table->addColumn($schema->getClientsIsConfidentialColumn(), Type::BOOLEAN)->setDefault(true); |
122
|
31 |
|
$table->addColumn($schema->getClientsIsScopeExcessAllowedColumn(), Type::BOOLEAN)->setDefault(false); |
123
|
31 |
|
$table->addColumn($schema->getClientsIsUseDefaultScopeColumn(), Type::BOOLEAN)->setDefault(true); |
124
|
31 |
|
$table->addColumn($schema->getClientsIsCodeGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false); |
125
|
31 |
|
$table->addColumn($schema->getClientsIsImplicitGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false); |
126
|
31 |
|
$table->addColumn($schema->getClientsIsPasswordGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false); |
127
|
31 |
|
$table->addColumn($schema->getClientsIsClientGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false); |
128
|
31 |
|
$table->addColumn($schema->getClientsIsRefreshGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false); |
129
|
31 |
|
$table->addColumn($schema->getClientsCreatedAtColumn(), Type::DATETIME)->setNotnull(true); |
130
|
31 |
|
$table->addColumn($schema->getClientsUpdatedAtColumn(), Type::DATETIME)->setNotnull(false); |
131
|
31 |
|
$table->setPrimaryKey([$schema->getClientsIdentityColumn()]); |
132
|
31 |
|
$manager->dropAndCreateTable($table); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param Connection $connection |
137
|
|
|
* @param DatabaseSchemaInterface $schema |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
31 |
|
protected function createRedirectUrisTable(Connection $connection, DatabaseSchemaInterface $schema): void |
142
|
|
|
{ |
143
|
31 |
|
$manager = $connection->getSchemaManager(); |
144
|
|
|
|
145
|
31 |
|
$table = new Table($schema->getRedirectUrisTable()); |
146
|
31 |
|
$table->addColumn($schema->getRedirectUrisIdentityColumn(), Type::INTEGER) |
147
|
31 |
|
->setNotnull(true)->setAutoincrement(true)->setUnsigned(true); |
148
|
31 |
|
$table->addColumn($schema->getRedirectUrisClientIdentityColumn(), Type::STRING)->setNotnull(true); |
149
|
31 |
|
$table->addColumn($schema->getRedirectUrisValueColumn(), Type::STRING)->setNotnull(true); |
150
|
31 |
|
$table->addColumn($schema->getRedirectUrisCreatedAtColumn(), Type::DATETIME)->setNotnull(true); |
151
|
31 |
|
$table->addColumn($schema->getRedirectUrisUpdatedAtColumn(), Type::DATETIME)->setNotnull(false); |
152
|
31 |
|
$table->setPrimaryKey([$schema->getRedirectUrisIdentityColumn()]); |
153
|
|
|
|
154
|
31 |
|
$table->addForeignKeyConstraint( |
155
|
31 |
|
$schema->getClientsTable(), |
156
|
31 |
|
[$schema->getRedirectUrisClientIdentityColumn()], |
157
|
31 |
|
[$schema->getClientsIdentityColumn()], |
158
|
31 |
|
$this->getOnDeleteCascadeConstraint() |
159
|
|
|
); |
160
|
|
|
|
161
|
31 |
|
$manager->dropAndCreateTable($table); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param Connection $connection |
166
|
|
|
* @param DatabaseSchemaInterface $schema |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
31 |
|
protected function createTokensTable(Connection $connection, DatabaseSchemaInterface $schema): void |
171
|
|
|
{ |
172
|
31 |
|
$manager = $connection->getSchemaManager(); |
173
|
|
|
|
174
|
31 |
|
$table = new Table($schema->getTokensTable()); |
175
|
31 |
|
$table->addColumn($schema->getTokensIdentityColumn(), Type::INTEGER) |
176
|
31 |
|
->setNotnull(true)->setAutoincrement(true)->setUnsigned(true); |
177
|
31 |
|
$table->addColumn($schema->getTokensIsEnabledColumn(), Type::BOOLEAN)->setNotnull(true)->setDefault(true); |
178
|
31 |
|
$table->addColumn($schema->getTokensIsScopeModified(), Type::BOOLEAN)->setNotnull(true)->setDefault(false); |
179
|
31 |
|
$table->addColumn($schema->getTokensClientIdentityColumn(), Type::STRING)->setNotnull(true); |
180
|
31 |
|
$table->addColumn($schema->getTokensUserIdentityColumn(), Type::INTEGER)->setNotnull(false)->setUnsigned(true); |
181
|
31 |
|
$table->addColumn($schema->getTokensRedirectUriColumn(), Type::STRING)->setNotnull(false); |
182
|
31 |
|
$table->addColumn($schema->getTokensCodeColumn(), Type::STRING)->setNotnull(false); |
183
|
31 |
|
$table->addColumn($schema->getTokensTypeColumn(), Type::STRING)->setNotnull(false); |
184
|
31 |
|
$table->addColumn($schema->getTokensValueColumn(), Type::STRING)->setNotnull(false); |
185
|
31 |
|
$table->addColumn($schema->getTokensRefreshColumn(), Type::STRING)->setNotnull(false); |
186
|
31 |
|
$table->addColumn($schema->getTokensCodeCreatedAtColumn(), Type::DATETIME)->setNotnull(false); |
187
|
31 |
|
$table->addColumn($schema->getTokensValueCreatedAtColumn(), Type::DATETIME)->setNotnull(false); |
188
|
31 |
|
$table->addColumn($schema->getTokensRefreshCreatedAtColumn(), Type::DATETIME)->setNotnull(false); |
189
|
31 |
|
$table->setPrimaryKey([$schema->getTokensIdentityColumn()]); |
190
|
|
|
|
191
|
31 |
|
$table->addForeignKeyConstraint( |
192
|
31 |
|
$schema->getClientsTable(), |
193
|
31 |
|
[$schema->getTokensClientIdentityColumn()], |
194
|
31 |
|
[$schema->getClientsIdentityColumn()], |
195
|
31 |
|
$this->getOnDeleteCascadeConstraint() |
196
|
|
|
); |
197
|
|
|
|
198
|
31 |
|
$usersTable = $schema->getUsersTable(); |
199
|
31 |
|
$usersIdentityColumn = $schema->getUsersIdentityColumn(); |
200
|
31 |
|
if ($usersTable !== null && $usersIdentityColumn !== null) { |
201
|
30 |
|
$table->addForeignKeyConstraint( |
202
|
30 |
|
$usersTable, |
203
|
30 |
|
[$schema->getTokensUserIdentityColumn()], |
204
|
30 |
|
[$usersIdentityColumn], |
205
|
30 |
|
$this->getOnDeleteCascadeConstraint() |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
31 |
|
$manager->dropAndCreateTable($table); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param Connection $connection |
214
|
|
|
* @param DatabaseSchemaInterface $schema |
215
|
|
|
* |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
31 |
View Code Duplication |
protected function createClientsScopesTable(Connection $connection, DatabaseSchemaInterface $schema): void |
|
|
|
|
219
|
|
|
{ |
220
|
31 |
|
$manager = $connection->getSchemaManager(); |
221
|
|
|
|
222
|
31 |
|
$table = new Table($schema->getClientsScopesTable()); |
223
|
31 |
|
$table->addColumn($schema->getClientsScopesIdentityColumn(), Type::INTEGER) |
224
|
31 |
|
->setNotnull(true)->setAutoincrement(true)->setUnsigned(true); |
225
|
31 |
|
$table->addColumn($schema->getClientsScopesClientIdentityColumn(), Type::STRING)->setNotnull(true); |
226
|
31 |
|
$table->addColumn($schema->getClientsScopesScopeIdentityColumn(), Type::STRING)->setNotnull(true); |
227
|
31 |
|
$table->setPrimaryKey([$schema->getClientsScopesIdentityColumn()]); |
228
|
31 |
|
$table->addUniqueIndex([ |
229
|
31 |
|
$schema->getClientsScopesClientIdentityColumn(), |
230
|
31 |
|
$schema->getClientsScopesScopeIdentityColumn() |
231
|
|
|
]); |
232
|
|
|
|
233
|
31 |
|
$table->addForeignKeyConstraint( |
234
|
31 |
|
$schema->getClientsTable(), |
235
|
31 |
|
[$schema->getClientsScopesClientIdentityColumn()], |
236
|
31 |
|
[$schema->getClientsIdentityColumn()], |
237
|
31 |
|
$this->getOnDeleteCascadeConstraint() |
238
|
|
|
); |
239
|
|
|
|
240
|
31 |
|
$table->addForeignKeyConstraint( |
241
|
31 |
|
$schema->getScopesTable(), |
242
|
31 |
|
[$schema->getClientsScopesScopeIdentityColumn()], |
243
|
31 |
|
[$schema->getScopesIdentityColumn()], |
244
|
31 |
|
$this->getOnDeleteCascadeConstraint() |
245
|
|
|
); |
246
|
|
|
|
247
|
31 |
|
$manager->dropAndCreateTable($table); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param Connection $connection |
252
|
|
|
* @param DatabaseSchemaInterface $schema |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
31 |
View Code Duplication |
protected function createTokensScopesTable(Connection $connection, DatabaseSchemaInterface $schema): void |
|
|
|
|
257
|
|
|
{ |
258
|
31 |
|
$manager = $connection->getSchemaManager(); |
259
|
|
|
|
260
|
31 |
|
$table = new Table($schema->getTokensScopesTable()); |
261
|
31 |
|
$table->addColumn($schema->getTokensScopesIdentityColumn(), Type::INTEGER) |
262
|
31 |
|
->setNotnull(true)->setAutoincrement(true)->setUnsigned(true); |
263
|
31 |
|
$table->addColumn($schema->getTokensScopesTokenIdentityColumn(), Type::INTEGER)->setNotnull(true) |
264
|
31 |
|
->setUnsigned(true); |
265
|
31 |
|
$table->addColumn($schema->getTokensScopesScopeIdentityColumn(), Type::STRING)->setNotnull(true); |
266
|
31 |
|
$table->setPrimaryKey([$schema->getTokensScopesIdentityColumn()]); |
267
|
31 |
|
$table->addUniqueIndex([ |
268
|
31 |
|
$schema->getTokensScopesTokenIdentityColumn(), |
269
|
31 |
|
$schema->getTokensScopesScopeIdentityColumn() |
270
|
|
|
]); |
271
|
|
|
|
272
|
31 |
|
$table->addForeignKeyConstraint( |
273
|
31 |
|
$schema->getTokensTable(), |
274
|
31 |
|
[$schema->getTokensScopesTokenIdentityColumn()], |
275
|
31 |
|
[$schema->getTokensIdentityColumn()], |
276
|
31 |
|
$this->getOnDeleteCascadeConstraint() |
277
|
|
|
); |
278
|
|
|
|
279
|
31 |
|
$table->addForeignKeyConstraint( |
280
|
31 |
|
$schema->getScopesTable(), |
281
|
31 |
|
[$schema->getTokensScopesScopeIdentityColumn()], |
282
|
31 |
|
[$schema->getScopesIdentityColumn()], |
283
|
31 |
|
$this->getOnDeleteCascadeConstraint() |
284
|
|
|
); |
285
|
|
|
|
286
|
31 |
|
$manager->dropAndCreateTable($table); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return array |
291
|
|
|
*/ |
292
|
31 |
|
protected function getOnDeleteCascadeConstraint(): array |
293
|
|
|
{ |
294
|
31 |
|
return ['onDelete' => 'CASCADE']; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
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.