1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\SavedSearches\Doctrine; |
4
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\Doctrine\DBAL\SchemaConfiguratorInterface; |
6
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
9
|
|
|
use Doctrine\DBAL\Types\Type; |
10
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
11
|
|
|
|
12
|
|
|
class SchemaConfigurator implements SchemaConfiguratorInterface |
13
|
|
|
{ |
14
|
|
|
const ID = 'id'; |
15
|
|
|
const USER = 'user_id'; |
16
|
|
|
const NAME = 'name'; |
17
|
|
|
const QUERY = 'query'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var StringLiteral |
21
|
|
|
*/ |
22
|
|
|
private $tableName; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* SchemaConfigurator constructor. |
26
|
|
|
* @param StringLiteral $tableName |
27
|
|
|
*/ |
28
|
|
|
public function __construct(StringLiteral $tableName) |
29
|
|
|
{ |
30
|
|
|
$this->tableName = $tableName; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function configure(AbstractSchemaManager $schemaManager) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$schema = $schemaManager->createSchema(); |
39
|
|
|
|
40
|
|
|
if (!$schema->hasTable($this->tableName->toNative())) { |
41
|
|
|
$table = $this->createTable($schema, $this->tableName); |
42
|
|
|
|
43
|
|
|
$schemaManager->createTable($table); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Schema $schema |
49
|
|
|
* @param StringLiteral $tableName |
50
|
|
|
* @return Table |
51
|
|
|
*/ |
52
|
|
|
private function createTable(Schema $schema, StringLiteral $tableName) |
53
|
|
|
{ |
54
|
|
|
$table = $schema->createTable($tableName->toNative()); |
55
|
|
|
|
56
|
|
|
$table->addColumn(self::ID, Type::GUID) |
57
|
|
|
->setLength(36) |
58
|
|
|
->setNotnull(true); |
59
|
|
|
|
60
|
|
|
$table->addColumn(self::USER, Type::GUID) |
61
|
|
|
->setLength(36) |
62
|
|
|
->setNotnull(true); |
63
|
|
|
|
64
|
|
|
$table->addColumn(self::NAME, Type::STRING) |
65
|
|
|
->setLength(255) |
66
|
|
|
->setNotnull(true); |
67
|
|
|
|
68
|
|
|
$table->addColumn(self::QUERY, Type::TEXT) |
69
|
|
|
->setNotnull(true); |
70
|
|
|
|
71
|
|
|
$table->addIndex([self::ID]); |
|
|
|
|
72
|
|
|
$table->addIndex([self::USER]); |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $table; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.