Code Duplication    Length = 257-257 lines in 2 locations

src/Adaptors/MySql/DatabaseSchemaMigrationTrait.php 1 location

@@ 27-283 (lines=257) @@
24
/**
25
 * @package Limoncello\Passport
26
 */
27
trait DatabaseSchemaMigrationTrait
28
{
29
    use BaseDatabaseSchemaMigrationTrait {
30
        BaseDatabaseSchemaMigrationTrait::createDatabaseSchema as createDatabaseTables;
31
        BaseDatabaseSchemaMigrationTrait::removeDatabaseSchema as removeDatabaseTables;
32
    }
33
34
    /**
35
     * @param Connection              $connection
36
     * @param DatabaseSchemaInterface $schema
37
     *
38
     * @throws DBALException
39
     *
40
     * @return void
41
     */
42
    protected function createDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void
43
    {
44
        try {
45
            $this->createDatabaseTables($connection, $schema);
46
            $this->createDatabaseViews($connection, $schema);
47
        } catch (DBALException $exception) {
48
            if ($connection->isConnected() === true) {
49
                $this->removeDatabaseSchema($connection, $schema);
50
            }
51
52
            throw $exception;
53
        }
54
    }
55
56
    /**
57
     * @param Connection              $connection
58
     * @param DatabaseSchemaInterface $schema
59
     *
60
     * @return void
61
     *
62
     * @throws DBALException
63
     */
64
    protected function removeDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void
65
    {
66
        $this->removeDatabaseTables($connection, $schema);
67
        $this->removeDatabaseViews($connection, $schema);
68
    }
69
70
    /**
71
     * @param Connection              $connection
72
     * @param DatabaseSchemaInterface $schema
73
     *
74
     * @return void
75
     *
76
     * @throws DBALException
77
     */
78
    protected function createDatabaseViews(Connection $connection, DatabaseSchemaInterface $schema): void
79
    {
80
        $this->createClientsView($connection, $schema);
81
        $this->createTokensView($connection, $schema);
82
        $this->createUsersView($connection, $schema);
83
        $this->createPassportView($connection, $schema);
84
    }
85
86
    /**
87
     * @param Connection              $connection
88
     * @param DatabaseSchemaInterface $schema
89
     *
90
     * @return void
91
     *
92
     * @throws DBALException
93
     */
94
    protected function removeDatabaseViews(Connection $connection, DatabaseSchemaInterface $schema): void
95
    {
96
        $this->removePassportView($connection, $schema);
97
        $this->removeClientsView($connection, $schema);
98
        $this->removeTokensView($connection, $schema);
99
        $this->removeUsersView($connection, $schema);
100
    }
101
102
    /**
103
     * @param Connection              $connection
104
     * @param DatabaseSchemaInterface $schema
105
     *
106
     * @throws DBALException
107
     *
108
     * @return void
109
     */
110
    private function createTokensView(Connection $connection, DatabaseSchemaInterface $schema): void
111
    {
112
        $view                = $schema->getTokensView();
113
        $tokens              = $schema->getTokensTable();
114
        $intermediate        = $schema->getTokensScopesTable();
115
        $tokensTokenId       = $schema->getTokensIdentityColumn();
116
        $intermediateTokenId = $schema->getTokensScopesTokenIdentityColumn();
117
        $intermediateScopeId = $schema->getTokensScopesScopeIdentityColumn();
118
        $scopes              = $schema->getTokensViewScopesColumn();
119
120
        $sql = <<< EOT
121
CREATE OR REPLACE VIEW {$view} AS
122
    SELECT
123
      t.*,
124
      GROUP_CONCAT(DISTINCT s.{$intermediateScopeId} ORDER BY s.{$intermediateScopeId} ASC SEPARATOR ' ') AS {$scopes}
125
    FROM {$tokens} AS t
126
      LEFT JOIN {$intermediate} AS s ON t.{$tokensTokenId} = s.{$intermediateTokenId}
127
    GROUP BY t.{$tokensTokenId};
128
EOT;
129
        $connection->exec($sql);
130
    }
131
132
    /**
133
     * @param Connection              $connection
134
     * @param DatabaseSchemaInterface $schema
135
     *
136
     * @throws DBALException
137
     *
138
     * @return void
139
     */
140
    private function removeTokensView(Connection $connection, DatabaseSchemaInterface $schema)
141
    {
142
        $view = $schema->getTokensView();
143
        $sql  = "DROP VIEW IF EXISTS {$view}";
144
        $connection->exec($sql);
145
    }
146
147
    /**
148
     * @param Connection              $connection
149
     * @param DatabaseSchemaInterface $schema
150
     *
151
     * @throws DBALException
152
     *
153
     * @return void
154
     */
155
    private function createPassportView(Connection $connection, DatabaseSchemaInterface $schema): void
156
    {
157
        $tokensView   = $schema->getTokensView();
158
        $view         = $schema->getPassportView();
159
        $users        = $schema->getUsersTable();
160
        $tokensUserFk = $schema->getTokensUserIdentityColumn();
161
162
        $sql = <<< EOT
163
CREATE OR REPLACE VIEW {$view} AS
164
    SELECT *
165
    FROM $tokensView
166
      LEFT JOIN $users USING ($tokensUserFk);
167
EOT;
168
        $connection->exec($sql);
169
    }
170
171
    /**
172
     * @param Connection              $connection
173
     * @param DatabaseSchemaInterface $schema
174
     *
175
     * @throws DBALException
176
     *
177
     * @return void
178
     */
179
    private function removePassportView(Connection $connection, DatabaseSchemaInterface $schema): void
180
    {
181
        $view = $schema->getPassportView();
182
        $sql  = "DROP VIEW IF EXISTS {$view}";
183
        $connection->exec($sql);
184
    }
185
186
    /**
187
     * @param Connection              $connection
188
     * @param DatabaseSchemaInterface $schema
189
     *
190
     * @throws DBALException
191
     *
192
     * @return void
193
     */
194
    private function createClientsView(Connection $connection, DatabaseSchemaInterface $schema)
195
    {
196
        $view             = $schema->getClientsView();
197
        $scopes           = $schema->getClientsViewScopesColumn();
198
        $redirectUris     = $schema->getClientsViewRedirectUrisColumn();
199
        $clientsScopes    = $schema->getClientsScopesTable();
200
        $clientsUris      = $schema->getRedirectUrisTable();
201
        $clients          = $schema->getClientsTable();
202
        $clientsClientId  = $schema->getClientsIdentityColumn();
203
        $clScopesClientId = $schema->getClientsScopesClientIdentityColumn();
204
        $clUrisClientId   = $schema->getRedirectUrisClientIdentityColumn();
205
        $urisValue        = $schema->getRedirectUrisValueColumn();
206
        $scopesScopeId    = $schema->getScopesIdentityColumn();
207
        $sql              = <<< EOT
208
CREATE VIEW {$view} AS
209
    SELECT
210
      c.*,
211
      GROUP_CONCAT(DISTINCT s.{$scopesScopeId} ORDER BY s.{$scopesScopeId} ASC SEPARATOR ' ') AS {$scopes},
212
      GROUP_CONCAT(DISTINCT u.{$urisValue}     ORDER BY u.{$urisValue} ASC SEPARATOR ' ')     AS {$redirectUris}
213
    FROM {$clients} AS c
214
      LEFT JOIN {$clientsScopes} AS s ON c.{$clientsClientId} = s.{$clScopesClientId}
215
      LEFT JOIN {$clientsUris}   AS u ON c.{$clientsClientId} = u.{$clUrisClientId}
216
    GROUP BY c.{$clientsClientId};
217
EOT;
218
        $connection->exec($sql);
219
    }
220
221
    /**
222
     * @param Connection              $connection
223
     * @param DatabaseSchemaInterface $schema
224
     *
225
     * @throws DBALException
226
     *
227
     * @return void
228
     */
229
    private function removeClientsView(Connection $connection, DatabaseSchemaInterface $schema)
230
    {
231
        $view = $schema->getClientsView();
232
        $sql  = "DROP VIEW IF EXISTS {$view}";
233
        $connection->exec($sql);
234
    }
235
236
    /**
237
     * @param Connection              $connection
238
     * @param DatabaseSchemaInterface $schema
239
     *
240
     * @throws DBALException
241
     *
242
     * @return void
243
     */
244
    private function createUsersView(Connection $connection, DatabaseSchemaInterface $schema)
245
    {
246
        $users = $schema->getUsersTable();
247
        if ($users !== null) {
248
            $view            = $schema->getUsersView();
249
            $tokensValue     = $schema->getTokensValueColumn();
250
            $tokensValueAt   = $schema->getTokensValueCreatedAtColumn();
251
            $tokensScopes    = $schema->getTokensViewScopesColumn();
252
            $tokensView      = $schema->getTokensView();
253
            $tokensUserId    = $schema->getTokensUserIdentityColumn();
254
            $usersUserId     = $schema->getUsersIdentityColumn();
255
            $tokensIsEnabled = $schema->getTokensIsEnabledColumn();
256
257
            $sql = <<< EOT
258
CREATE OR REPLACE VIEW {$view} AS
259
    SELECT
260
        t.$tokensValue, t.$tokensValueAt, t.$tokensScopes, u.*
261
    FROM {$tokensView} AS t
262
      LEFT JOIN {$users} AS u ON t.{$tokensUserId} = u.{$usersUserId}
263
    WHERE $tokensIsEnabled IS TRUE;
264
EOT;
265
            $connection->exec($sql);
266
        }
267
    }
268
269
    /**
270
     * @param Connection              $connection
271
     * @param DatabaseSchemaInterface $schema
272
     *
273
     * @throws DBALException
274
     *
275
     * @return void
276
     */
277
    private function removeUsersView(Connection $connection, DatabaseSchemaInterface $schema)
278
    {
279
        $view = $schema->getUsersView();
280
        $sql  = "DROP VIEW IF EXISTS {$view}";
281
        $connection->exec($sql);
282
    }
283
}
284

src/Adaptors/PostgreSql/DatabaseSchemaMigrationTrait.php 1 location

@@ 27-283 (lines=257) @@
24
/**
25
 * @package Limoncello\Passport
26
 */
27
trait DatabaseSchemaMigrationTrait
28
{
29
    use BaseDatabaseSchemaMigrationTrait {
30
        BaseDatabaseSchemaMigrationTrait::createDatabaseSchema as createDatabaseTables;
31
        BaseDatabaseSchemaMigrationTrait::removeDatabaseSchema as removeDatabaseTables;
32
    }
33
34
    /**
35
     * @param Connection              $connection
36
     * @param DatabaseSchemaInterface $schema
37
     *
38
     * @throws DBALException
39
     *
40
     * @return void
41
     */
42
    protected function createDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void
43
    {
44
        try {
45
            $this->createDatabaseTables($connection, $schema);
46
            $this->createDatabaseViews($connection, $schema);
47
        } catch (DBALException $exception) {
48
            if ($connection->isConnected() === true) {
49
                $this->removeDatabaseSchema($connection, $schema);
50
            }
51
52
            throw $exception;
53
        }
54
    }
55
56
    /**
57
     * @param Connection              $connection
58
     * @param DatabaseSchemaInterface $schema
59
     *
60
     * @return void
61
     *
62
     * @throws DBALException
63
     */
64
    protected function removeDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void
65
    {
66
        $this->removeDatabaseTables($connection, $schema);
67
        $this->removeDatabaseViews($connection, $schema);
68
    }
69
70
    /**
71
     * @param Connection              $connection
72
     * @param DatabaseSchemaInterface $schema
73
     *
74
     * @return void
75
     *
76
     * @throws DBALException
77
     */
78
    protected function createDatabaseViews(Connection $connection, DatabaseSchemaInterface $schema): void
79
    {
80
        $this->createClientsView($connection, $schema);
81
        $this->createTokensView($connection, $schema);
82
        $this->createUsersView($connection, $schema);
83
        $this->createPassportView($connection, $schema);
84
    }
85
86
    /**
87
     * @param Connection              $connection
88
     * @param DatabaseSchemaInterface $schema
89
     *
90
     * @return void
91
     *
92
     * @throws DBALException
93
     */
94
    protected function removeDatabaseViews(Connection $connection, DatabaseSchemaInterface $schema): void
95
    {
96
        $this->removePassportView($connection, $schema);
97
        $this->removeClientsView($connection, $schema);
98
        $this->removeTokensView($connection, $schema);
99
        $this->removeUsersView($connection, $schema);
100
    }
101
102
    /**
103
     * @param Connection              $connection
104
     * @param DatabaseSchemaInterface $schema
105
     *
106
     * @throws DBALException
107
     *
108
     * @return void
109
     */
110
    private function createTokensView(Connection $connection, DatabaseSchemaInterface $schema): void
111
    {
112
        $view                = $schema->getTokensView();
113
        $tokens              = $schema->getTokensTable();
114
        $intermediate        = $schema->getTokensScopesTable();
115
        $tokensTokenId       = $schema->getTokensIdentityColumn();
116
        $intermediateTokenId = $schema->getTokensScopesTokenIdentityColumn();
117
        $intermediateScopeId = $schema->getTokensScopesScopeIdentityColumn();
118
        $scopes              = $schema->getTokensViewScopesColumn();
119
120
        $sql = <<< EOT
121
CREATE OR REPLACE VIEW {$view} AS
122
    SELECT
123
      t.*,
124
      array_remove(array_agg(s.{$intermediateScopeId}), NULL) AS {$scopes}
125
    FROM {$tokens} AS t
126
      LEFT JOIN {$intermediate} AS s ON t.{$tokensTokenId} = s.{$intermediateTokenId}
127
    GROUP BY t.{$tokensTokenId};
128
EOT;
129
        $connection->exec($sql);
130
    }
131
132
    /**
133
     * @param Connection              $connection
134
     * @param DatabaseSchemaInterface $schema
135
     *
136
     * @throws DBALException
137
     *
138
     * @return void
139
     */
140
    private function removeTokensView(Connection $connection, DatabaseSchemaInterface $schema)
141
    {
142
        $view = $schema->getTokensView();
143
        $sql  = "DROP VIEW IF EXISTS {$view}";
144
        $connection->exec($sql);
145
    }
146
147
    /**
148
     * @param Connection              $connection
149
     * @param DatabaseSchemaInterface $schema
150
     *
151
     * @throws DBALException
152
     *
153
     * @return void
154
     */
155
    private function createPassportView(Connection $connection, DatabaseSchemaInterface $schema): void
156
    {
157
        $tokensView   = $schema->getTokensView();
158
        $view         = $schema->getPassportView();
159
        $users        = $schema->getUsersTable();
160
        $tokensUserFk = $schema->getTokensUserIdentityColumn();
161
162
        $sql = <<< EOT
163
CREATE OR REPLACE VIEW {$view} AS
164
    SELECT *
165
    FROM $tokensView
166
      LEFT JOIN $users USING ($tokensUserFk);
167
EOT;
168
        $connection->exec($sql);
169
    }
170
171
    /**
172
     * @param Connection              $connection
173
     * @param DatabaseSchemaInterface $schema
174
     *
175
     * @throws DBALException
176
     *
177
     * @return void
178
     */
179
    private function removePassportView(Connection $connection, DatabaseSchemaInterface $schema): void
180
    {
181
        $view = $schema->getPassportView();
182
        $sql  = "DROP VIEW IF EXISTS {$view}";
183
        $connection->exec($sql);
184
    }
185
186
    /**
187
     * @param Connection              $connection
188
     * @param DatabaseSchemaInterface $schema
189
     *
190
     * @throws DBALException
191
     *
192
     * @return void
193
     */
194
    private function createClientsView(Connection $connection, DatabaseSchemaInterface $schema)
195
    {
196
        $view             = $schema->getClientsView();
197
        $scopes           = $schema->getClientsViewScopesColumn();
198
        $redirectUris     = $schema->getClientsViewRedirectUrisColumn();
199
        $clientsScopes    = $schema->getClientsScopesTable();
200
        $clientsUris      = $schema->getRedirectUrisTable();
201
        $clients          = $schema->getClientsTable();
202
        $clientsClientId  = $schema->getClientsIdentityColumn();
203
        $clScopesClientId = $schema->getClientsScopesClientIdentityColumn();
204
        $clUrisClientId   = $schema->getRedirectUrisClientIdentityColumn();
205
        $urisValue        = $schema->getRedirectUrisValueColumn();
206
        $scopesScopeId    = $schema->getScopesIdentityColumn();
207
        $sql              = <<< EOT
208
CREATE VIEW {$view} AS
209
    SELECT
210
      c.*,
211
      array_remove(array_agg(s.{$scopesScopeId}), NULL) AS {$scopes},
212
      array_remove(array_agg(u.{$urisValue}), NULL)     AS {$redirectUris}
213
    FROM {$clients} AS c
214
      LEFT JOIN {$clientsScopes} AS s ON c.{$clientsClientId} = s.{$clScopesClientId}
215
      LEFT JOIN {$clientsUris}   AS u ON c.{$clientsClientId} = u.{$clUrisClientId}
216
    GROUP BY c.{$clientsClientId};
217
EOT;
218
        $connection->exec($sql);
219
    }
220
221
    /**
222
     * @param Connection              $connection
223
     * @param DatabaseSchemaInterface $schema
224
     *
225
     * @throws DBALException
226
     *
227
     * @return void
228
     */
229
    private function removeClientsView(Connection $connection, DatabaseSchemaInterface $schema)
230
    {
231
        $view = $schema->getClientsView();
232
        $sql  = "DROP VIEW IF EXISTS {$view}";
233
        $connection->exec($sql);
234
    }
235
236
    /**
237
     * @param Connection              $connection
238
     * @param DatabaseSchemaInterface $schema
239
     *
240
     * @throws DBALException
241
     *
242
     * @return void
243
     */
244
    private function createUsersView(Connection $connection, DatabaseSchemaInterface $schema)
245
    {
246
        $users = $schema->getUsersTable();
247
        if ($users !== null) {
248
            $view            = $schema->getUsersView();
249
            $tokensValue     = $schema->getTokensValueColumn();
250
            $tokensValueAt   = $schema->getTokensValueCreatedAtColumn();
251
            $tokensScopes    = $schema->getTokensViewScopesColumn();
252
            $tokensView      = $schema->getTokensView();
253
            $tokensUserId    = $schema->getTokensUserIdentityColumn();
254
            $usersUserId     = $schema->getUsersIdentityColumn();
255
            $tokensIsEnabled = $schema->getTokensIsEnabledColumn();
256
257
            $sql = <<< EOT
258
CREATE OR REPLACE VIEW {$view} AS
259
    SELECT
260
        t.$tokensValue, t.$tokensValueAt, t.$tokensScopes, u.*
261
    FROM {$tokensView} AS t
262
      LEFT JOIN {$users} AS u ON t.{$tokensUserId} = u.{$usersUserId}
263
    WHERE $tokensIsEnabled IS TRUE;
264
EOT;
265
            $connection->exec($sql);
266
        }
267
    }
268
269
    /**
270
     * @param Connection              $connection
271
     * @param DatabaseSchemaInterface $schema
272
     *
273
     * @throws DBALException
274
     *
275
     * @return void
276
     */
277
    private function removeUsersView(Connection $connection, DatabaseSchemaInterface $schema)
278
    {
279
        $view = $schema->getUsersView();
280
        $sql  = "DROP VIEW IF EXISTS {$view}";
281
        $connection->exec($sql);
282
    }
283
}
284