Total Complexity | 40 |
Total Lines | 341 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 3 |
Complex classes like Migrator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Migrator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | final class Migrator |
||
21 | { |
||
22 | private const DB_DATE_FORMAT = 'Y-m-d H:i:s'; |
||
23 | |||
24 | private const MIGRATION_TABLE_FIELDS_LIST = [ |
||
25 | 'id', |
||
26 | 'migration', |
||
27 | 'time_executed', |
||
28 | 'created_at', |
||
29 | ]; |
||
30 | |||
31 | /** @var MigrationConfig */ |
||
32 | private $config; |
||
33 | |||
34 | /** @var DatabaseManager */ |
||
35 | private $dbal; |
||
36 | |||
37 | /** @var RepositoryInterface */ |
||
38 | private $repository; |
||
39 | |||
40 | /** |
||
41 | * @param MigrationConfig $config |
||
42 | * @param DatabaseManager $dbal |
||
43 | * @param RepositoryInterface $repository |
||
44 | */ |
||
45 | public function __construct( |
||
46 | MigrationConfig $config, |
||
47 | DatabaseManager $dbal, |
||
48 | RepositoryInterface $repository |
||
49 | ) { |
||
50 | $this->config = $config; |
||
51 | $this->repository = $repository; |
||
52 | $this->dbal = $dbal; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return MigrationConfig |
||
57 | */ |
||
58 | public function getConfig(): MigrationConfig |
||
59 | { |
||
60 | return $this->config; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return RepositoryInterface |
||
65 | */ |
||
66 | public function getRepository(): RepositoryInterface |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Check if all related databases are configures with migrations. |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function isConfigured(): bool |
||
77 | { |
||
78 | foreach ($this->dbal->getDatabases() as $db) { |
||
79 | if (!$db->hasTable($this->config->getTable()) || !$this->checkMigrationTableStructure($db)) { |
||
80 | return false; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return !$this->isRestoreMigrationDataRequired(); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Configure all related databases with migration table. |
||
89 | */ |
||
90 | public function configure(): void |
||
91 | { |
||
92 | if ($this->isConfigured()) { |
||
93 | return; |
||
94 | } |
||
95 | |||
96 | foreach ($this->dbal->getDatabases() as $db) { |
||
97 | $schema = $db->table($this->config->getTable())->getSchema(); |
||
98 | |||
99 | // Schema update will automatically sync all needed data |
||
100 | $schema->primary('id'); |
||
101 | $schema->string('migration', 191)->nullable(false); |
||
102 | $schema->datetime('time_executed')->datetime(); |
||
103 | $schema->datetime('created_at')->datetime(); |
||
104 | $schema->index(['migration', 'created_at']) |
||
105 | ->unique(true); |
||
106 | |||
107 | if ($schema->hasIndex(['migration'])) { |
||
108 | $schema->dropIndex(['migration']); |
||
109 | } |
||
110 | |||
111 | $schema->save(); |
||
112 | } |
||
113 | |||
114 | if ($this->isRestoreMigrationDataRequired()) { |
||
115 | $this->restoreMigrationData(); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get every available migration with valid meta information. |
||
121 | * |
||
122 | * @return MigrationInterface[] |
||
123 | */ |
||
124 | public function getMigrations(): array |
||
125 | { |
||
126 | $result = []; |
||
127 | foreach ($this->repository->getMigrations() as $migration) { |
||
128 | //Populating migration state and execution time (if any) |
||
129 | $result[] = $migration->withState($this->resolveState($migration)); |
||
130 | } |
||
131 | |||
132 | return $result; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Execute one migration and return it's instance. |
||
137 | * |
||
138 | * @param CapsuleInterface $capsule |
||
139 | * |
||
140 | * @throws MigrationException |
||
141 | * |
||
142 | * @return MigrationInterface|null |
||
143 | */ |
||
144 | public function run(CapsuleInterface $capsule = null): ?MigrationInterface |
||
145 | { |
||
146 | if (!$this->isConfigured()) { |
||
147 | throw new MigrationException('Unable to run migration, Migrator not configured'); |
||
148 | } |
||
149 | |||
150 | foreach ($this->getMigrations() as $migration) { |
||
151 | if ($migration->getState()->getStatus() !== State::STATUS_PENDING) { |
||
152 | continue; |
||
153 | } |
||
154 | |||
155 | try { |
||
156 | $capsule = $capsule ?? new Capsule($this->dbal->database($migration->getDatabase())); |
||
157 | $capsule->getDatabase($migration->getDatabase())->transaction( |
||
158 | static function () use ($migration, $capsule): void { |
||
159 | $migration->withCapsule($capsule)->up(); |
||
160 | } |
||
161 | ); |
||
162 | |||
163 | $this->migrationTable($migration->getDatabase())->insertOne( |
||
164 | [ |
||
165 | 'migration' => $migration->getState()->getName(), |
||
166 | 'time_executed' => new \DateTime('now'), |
||
167 | 'created_at' => $this->getMigrationCreatedAtForDb($migration), |
||
168 | ] |
||
169 | ); |
||
170 | |||
171 | return $migration->withState($this->resolveState($migration)); |
||
172 | } catch (\Throwable $exception) { |
||
173 | throw new MigrationException( |
||
174 | \sprintf( |
||
175 | 'Error in the migration (%s) occurred: %s', |
||
176 | \sprintf( |
||
177 | '%s (%s)', |
||
178 | $migration->getState()->getName(), |
||
179 | $migration->getState()->getTimeCreated()->format(self::DB_DATE_FORMAT) |
||
180 | ), |
||
181 | $exception->getMessage() |
||
182 | ), |
||
183 | $exception->getCode(), |
||
184 | $exception |
||
185 | ); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | return null; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Rollback last migration and return it's instance. |
||
194 | * |
||
195 | * @param CapsuleInterface $capsule |
||
196 | * |
||
197 | * @throws \Throwable |
||
198 | * |
||
199 | * @return MigrationInterface|null |
||
200 | */ |
||
201 | public function rollback(CapsuleInterface $capsule = null): ?MigrationInterface |
||
202 | { |
||
203 | if (!$this->isConfigured()) { |
||
204 | throw new MigrationException('Unable to run migration, Migrator not configured'); |
||
205 | } |
||
206 | |||
207 | /** @var MigrationInterface $migration */ |
||
208 | foreach (array_reverse($this->getMigrations()) as $migration) { |
||
209 | if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) { |
||
210 | continue; |
||
211 | } |
||
212 | |||
213 | $capsule = $capsule ?? new Capsule($this->dbal->database($migration->getDatabase())); |
||
214 | $capsule->getDatabase()->transaction( |
||
215 | static function () use ($migration, $capsule): void { |
||
216 | $migration->withCapsule($capsule)->down(); |
||
217 | } |
||
218 | ); |
||
219 | |||
220 | $migrationData = $this->fetchMigrationData($migration); |
||
221 | |||
222 | if (!empty($migrationData)) { |
||
223 | $this->migrationTable($migration->getDatabase()) |
||
224 | ->delete(['id' => $migrationData['id']]) |
||
225 | ->run(); |
||
226 | } |
||
227 | |||
228 | return $migration->withState($this->resolveState($migration)); |
||
229 | } |
||
230 | |||
231 | return null; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Clarify migration state with valid status and execution time |
||
236 | * |
||
237 | * @param MigrationInterface $migration |
||
238 | * |
||
239 | * @return State |
||
240 | */ |
||
241 | protected function resolveState(MigrationInterface $migration): State |
||
242 | { |
||
243 | $db = $this->dbal->database($migration->getDatabase()); |
||
244 | |||
245 | $data = $this->fetchMigrationData($migration); |
||
246 | |||
247 | if (empty($data['time_executed'])) { |
||
248 | return $migration->getState()->withStatus(State::STATUS_PENDING); |
||
249 | } |
||
250 | |||
251 | return $migration->getState()->withStatus( |
||
252 | State::STATUS_EXECUTED, |
||
253 | new \DateTimeImmutable($data['time_executed'], $db->getDriver()->getTimezone()) |
||
254 | ); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Migration table, all migration information will be stored in it. |
||
259 | * |
||
260 | * @param string|null $database |
||
261 | * |
||
262 | * @return Table |
||
263 | */ |
||
264 | protected function migrationTable(string $database = null): Table |
||
265 | { |
||
266 | return $this->dbal->database($database)->table($this->config->getTable()); |
||
267 | } |
||
268 | |||
269 | protected function checkMigrationTableStructure(Database $db): bool |
||
280 | |||
281 | |||
282 | |||
283 | ; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Fetch migration information from database |
||
288 | * |
||
289 | * @param MigrationInterface $migration |
||
290 | * |
||
291 | * @return array|null |
||
292 | */ |
||
293 | protected function fetchMigrationData(MigrationInterface $migration): ?array |
||
307 | } |
||
308 | |||
309 | protected function restoreMigrationData(): void |
||
310 | { |
||
311 | foreach ($this->repository->getMigrations() as $migration) { |
||
312 | $migrationData = $this->migrationTable($migration->getDatabase()) |
||
313 | ->select('id') |
||
314 | ->where( |
||
315 | [ |
||
316 | 'migration' => $migration->getState()->getName(), |
||
317 | 'created_at' => null, |
||
318 | ] |
||
319 | ) |
||
320 | ->run() |
||
321 | ->fetch(); |
||
322 | |||
323 | if (!empty($migrationData)) { |
||
324 | $this->migrationTable($migration->getDatabase()) |
||
325 | ->update( |
||
326 | ['created_at' => $this->getMigrationCreatedAtForDb($migration)], |
||
327 | ['id' => $migrationData['id']] |
||
328 | ) |
||
329 | ->run(); |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Check if some data modification required |
||
336 | * |
||
337 | * @return bool |
||
338 | */ |
||
339 | protected function isRestoreMigrationDataRequired(): bool |
||
354 | } |
||
355 | |||
356 | protected function getMigrationCreatedAtForDb(MigrationInterface $migration): \DateTimeInterface |
||
357 | { |
||
358 | $db = $this->dbal->database($migration->getDatabase()); |
||
359 | |||
360 | return \DateTimeImmutable::createFromFormat( |
||
361 | self::DB_DATE_FORMAT, |
||
367 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.