1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Passport\Package; |
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 Limoncello\Contracts\Data\MigrationInterface; |
24
|
|
|
use Limoncello\Passport\Adaptors\PostgreSql\DatabaseSchemaMigrationTrait; |
25
|
|
|
use Limoncello\Passport\Contracts\Entities\DatabaseSchemaInterface; |
26
|
|
|
use Psr\Container\ContainerInterface; |
27
|
|
|
use function assert; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @package Limoncello\Passport |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
class PostgreSqlPassportMigration implements MigrationInterface |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
use DatabaseSchemaMigrationTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ContainerInterface |
38
|
|
|
*/ |
39
|
|
|
private $container; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
1 |
|
public function init(ContainerInterface $container): MigrationInterface |
45
|
|
|
{ |
46
|
1 |
|
$this->container = $container; |
47
|
|
|
|
48
|
1 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return void |
53
|
|
|
* |
54
|
|
|
* @throws DBALException |
55
|
|
|
*/ |
56
|
1 |
|
public function migrate(): void |
57
|
|
|
{ |
58
|
1 |
|
$this->createDatabaseSchema($this->getConnection(), $this->getDatabaseSchema()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
* |
64
|
|
|
* @throws DBALException |
65
|
|
|
*/ |
66
|
1 |
|
public function rollback(): void |
67
|
|
|
{ |
68
|
1 |
|
$this->removeDatabaseSchema($this->getConnection(), $this->getDatabaseSchema()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return ContainerInterface |
73
|
|
|
*/ |
74
|
1 |
|
protected function getContainer(): ContainerInterface |
75
|
|
|
{ |
76
|
1 |
|
assert($this->container !== null); |
77
|
|
|
|
78
|
1 |
|
return $this->container; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Connection |
83
|
|
|
*/ |
84
|
1 |
|
protected function getConnection(): Connection |
85
|
|
|
{ |
86
|
1 |
|
return $this->getContainer()->get(Connection::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return DatabaseSchemaInterface |
91
|
|
|
*/ |
92
|
1 |
|
protected function getDatabaseSchema(): DatabaseSchemaInterface |
93
|
|
|
{ |
94
|
1 |
|
return $this->getContainer()->get(DatabaseSchemaInterface::class); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.