Code Duplication    Length = 257-257 lines in 2 locations

src/Adaptors/MySql/DatabaseSchemaMigrationTrait.php 1 location

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

src/Adaptors/PostgreSql/DatabaseSchemaMigrationTrait.php 1 location

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