1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Taisiya\PropelBundle\Database; |
4
|
|
|
|
5
|
|
|
use Taisiya\PropelBundle\Database\Exception\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
final class Schema |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $databases = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Database $database |
16
|
|
|
* |
17
|
|
|
* @throws InvalidArgumentException |
18
|
|
|
* |
19
|
|
|
* @return self |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function createDatabase(Database $database): self |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if ($this->hasDatabase($database::getName())) { |
24
|
|
|
throw new InvalidArgumentException('Database '.$database::getName().' already added'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->databases[$database::getName()] = $database; |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Database $database |
34
|
|
|
* |
35
|
|
|
* @return Schema |
36
|
|
|
*/ |
37
|
|
|
public function createDatabaseIfNotExists(Database $database): self |
38
|
|
|
{ |
39
|
|
|
if (!$this->hasDatabase($database::getName())) { |
40
|
|
|
$this->createDatabase($database); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Database $database |
48
|
|
|
* |
49
|
|
|
* @throws InvalidArgumentException |
50
|
|
|
* |
51
|
|
|
* @return Schema |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function removeDatabase(Database $database): self |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if (!$this->hasDatabase($database::getName())) { |
56
|
|
|
throw new InvalidArgumentException('Database '.$database::getName().' not exists'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
unset($this->databases[$database::getName()]); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* |
67
|
|
|
* @throws InvalidArgumentException |
68
|
|
|
* |
69
|
|
|
* @return Database |
70
|
|
|
*/ |
71
|
|
|
public function getDatabase(string $name): Database |
72
|
|
|
{ |
73
|
|
|
if (!array_key_exists($name, $this->databases)) { |
74
|
|
|
throw new InvalidArgumentException('Database '.$name.' not added'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->databases[$name]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getDatabaseByClassName(string $class): Database |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $name |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasDatabase(string $name): bool |
90
|
|
|
{ |
91
|
|
|
return array_key_exists($name, $this->databases); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function hasDatabaseByClassName(string $className): bool |
95
|
|
|
{ |
96
|
|
|
if (!class_exists($className)) { |
97
|
|
|
throw new InvalidArgumentException('Class '.$className.' not exists.'); |
98
|
|
|
} |
99
|
|
|
$reflectionClass = new \ReflectionClass($className); |
100
|
|
|
if (!$reflectionClass->isSubclassOf(Database::class)) { |
101
|
|
|
throw new InvalidArgumentException('Class must be instance of '.Database::class.'.'); |
102
|
|
|
} |
103
|
|
|
$name = $reflectionClass->getMethod('getName')->invoke($reflectionClass); |
104
|
|
|
exit(var_dump($name)); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function getDatabases(): array |
111
|
|
|
{ |
112
|
|
|
return $this->databases; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
final public function generateOutputXml(): string |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
121
|
|
|
$dom->formatOutput = true; |
122
|
|
|
|
123
|
|
|
/** @var Database $database */ |
124
|
|
|
foreach ($this->getDatabases() as $database) { |
125
|
|
|
$database->appendToXmlDocument($dom); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $dom->saveXML(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.