| Conditions | 24 |
| Paths | 11441 |
| Total Lines | 145 |
| Code Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 88 |
| CRAP Score | 24.9953 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 49 | 3 | protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
|
| 50 | { |
||
| 51 | 3 | $client = new \MongoClient($input->getOption('server')); |
|
| 52 | 3 | $db = $client->selectDB($input->getArgument('database')); |
|
| 53 | 3 | $output->writeln("<info>✓ Successfully established database connection</info>", $output::VERBOSITY_VERBOSE); |
|
| 54 | |||
| 55 | 3 | $directories = $input->getArgument('migration-directories'); |
|
| 56 | 3 | foreach ($directories as $directory) { |
|
| 57 | 3 | if (! is_dir($directory)) { |
|
| 58 | throw new Console\Exception\InvalidArgumentException("'{$directory}' is no valid directory"); |
||
| 59 | } |
||
| 60 | |||
| 61 | 3 | $output->writeln("<comment>Iterating '{$directory}' for potential migrations classes</comment>", $output::VERBOSITY_DEBUG); |
|
| 62 | 3 | $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::LEAVES_ONLY); |
|
| 63 | |||
| 64 | /** @var \SplFileInfo $file */ |
||
| 65 | 3 | foreach ($iterator as $file) { |
|
| 66 | 3 | if ($file->getBasename('.php') === $file->getBasename()) { |
|
| 67 | 3 | continue; |
|
| 68 | } |
||
| 69 | |||
| 70 | 3 | require_once $file->getRealPath(); |
|
| 71 | |||
| 72 | 3 | $output->writeln("<comment>Loaded potential migration '{$file->getRealPath()}'</comment>", $output::VERBOSITY_DEBUG); |
|
| 73 | 3 | } |
|
| 74 | 3 | } |
|
| 75 | |||
| 76 | /** @var MongoDbMigrations\MigrationInterface[] $migrations */ |
||
| 77 | 3 | $migrations = []; |
|
| 78 | 3 | foreach (get_declared_classes() as $className) { |
|
| 79 | 3 | $reflectionClass = new \ReflectionClass($className); |
|
| 80 | |||
| 81 | 3 | if ($reflectionClass->implementsInterface(MongoDbMigrations\MigrationInterface::class)) { |
|
| 82 | /** @var MongoDbMigrations\MigrationInterface $newInstance */ |
||
| 83 | 3 | $newInstance = $reflectionClass->newInstance(); |
|
| 84 | 3 | $id = md5($newInstance->getId()); |
|
| 85 | |||
| 86 | 3 | if (isset($migrations[$id])) { |
|
| 87 | $existingMigrationClass = get_class($migrations[$id]); |
||
| 88 | throw new Console\Exception\RuntimeException("Found a non unique migration id '{$newInstance->getId()}' in '{$reflectionClass->getName()}', already defined by migration class '{$existingMigrationClass}'"); |
||
|
1 ignored issue
–
show
|
|||
| 89 | } |
||
| 90 | |||
| 91 | 3 | $migrations[$id] = $newInstance; |
|
| 92 | |||
| 93 | 3 | $output->writeln("<comment>Found valid migration class '{$reflectionClass->getName()}'</comment>", $output::VERBOSITY_DEBUG); |
|
|
1 ignored issue
–
show
|
|||
| 94 | 3 | } |
|
| 95 | 3 | } |
|
| 96 | |||
| 97 | 3 | $migrationClassesCount = count($migrations); |
|
| 98 | 3 | $output->writeln("<info>✓ Found {$migrationClassesCount} valid migration classes</info>", $output::VERBOSITY_VERBOSE); |
|
| 99 | |||
| 100 | 3 | uasort($migrations, function (MongoDbMigrations\MigrationInterface $a, MongoDbMigrations\MigrationInterface $b) { |
|
| 101 | 3 | if ($a->getCreateDate() === $b->getCreateDate()) { |
|
| 102 | return 0; |
||
| 103 | } |
||
| 104 | |||
| 105 | 3 | return $a->getCreateDate() < $b->getCreateDate() ? -1 : 1; |
|
| 106 | 3 | }); |
|
| 107 | |||
| 108 | 3 | $output->writeln("<info>✓ Reordered all migration classes according to their create date</info>", $output::VERBOSITY_VERBOSE); |
|
| 109 | |||
| 110 | 3 | $databaseMigrationsLockCollection = $db->selectCollection('DATABASE_MIGRATIONS_LOCK'); |
|
| 111 | |||
| 112 | 3 | $currentLock = $databaseMigrationsLockCollection->findAndModify(['locked' => ['$exists' => true]], ['locked' => true, 'last_locked_date' => new \MongoDate()], [], ['upsert' => true]); |
|
| 113 | 3 | if ($currentLock !== null && $currentLock['locked'] === true) { |
|
| 114 | 1 | throw new Console\Exception\RuntimeException('Concurrent migrations are not allowed'); |
|
| 115 | } |
||
| 116 | |||
| 117 | try { |
||
| 118 | 2 | $output->writeln("<info>✓ Successfully acquired migration lock</info>", $output::VERBOSITY_VERBOSE); |
|
| 119 | |||
| 120 | 2 | $databaseMigrationsCollection = $db->selectCollection('DATABASE_MIGRATIONS'); |
|
| 121 | |||
| 122 | 2 | if (count($databaseMigrationsCollection->getIndexInfo()) <= 1) { |
|
| 123 | 1 | $databaseMigrationsCollection->createIndex(['migration_id' => 1], |
|
| 124 | 1 | ['unique' => true, 'background' => false]); |
|
| 125 | 1 | } |
|
| 126 | |||
| 127 | 2 | $progress = new Console\Helper\ProgressBar($output, count($migrations)); |
|
| 128 | |||
| 129 | 2 | switch ($output->getVerbosity()) { |
|
| 130 | 2 | case $output::VERBOSITY_VERBOSE: |
|
| 131 | $format = 'verbose'; |
||
| 132 | break; |
||
| 133 | 2 | case $output::VERBOSITY_VERY_VERBOSE: |
|
| 134 | $format = 'very_verbose'; |
||
| 135 | break; |
||
| 136 | 2 | case $output::VERBOSITY_DEBUG: |
|
| 137 | $format = 'debug'; |
||
| 138 | break; |
||
| 139 | 2 | default: |
|
| 140 | 2 | $format = 'normal'; |
|
| 141 | 2 | } |
|
| 142 | |||
| 143 | 2 | $progress->setFormat($format); |
|
| 144 | 2 | $progress->start(); |
|
| 145 | 2 | $executedMigrations = 0; |
|
| 146 | |||
| 147 | 2 | foreach ($migrations as $id => $migration) { |
|
| 148 | 2 | $progress->advance(); |
|
| 149 | |||
| 150 | 2 | if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface && $input->getOption('contexts') !== []) { |
|
| 151 | 2 | if (array_intersect($migration->getContexts(), $input->getOption('contexts')) === []) { |
|
| 152 | continue; |
||
| 153 | } |
||
| 154 | 2 | } |
|
| 155 | |||
| 156 | 2 | if (!$migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface) { |
|
| 157 | 2 | if ($databaseMigrationsCollection->count(['migration_id' => $id]) > 0) { |
|
| 158 | 1 | continue; |
|
| 159 | } |
||
| 160 | 1 | } |
|
| 161 | |||
| 162 | 2 | $migration->execute($db); |
|
| 163 | 2 | $executedMigrations++; |
|
| 164 | |||
| 165 | $migrationInfo = [ |
||
| 166 | 2 | 'migration_id' => $id, |
|
| 167 | 2 | 'migration_class' => get_class($migration), |
|
| 168 | 2 | 'last_execution_date' => new \MongoDate(), |
|
| 169 | 2 | 'run_always' => $migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface, |
|
| 170 | 2 | ]; |
|
| 171 | |||
| 172 | 2 | if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface) { |
|
| 173 | 1 | $migrationInfo['contexts'] = $migration->getContexts(); |
|
| 174 | 1 | } |
|
| 175 | |||
| 176 | 2 | $databaseMigrationsCollection->update( |
|
| 177 | 2 | ['migration_id' => $id], |
|
| 178 | 2 | $migrationInfo, |
|
| 179 | 2 | ['upsert' => true] |
|
| 180 | 2 | ); |
|
| 181 | 2 | } |
|
| 182 | |||
| 183 | 2 | $progress->finish(); |
|
| 184 | 2 | $output->writeln(''); |
|
| 185 | |||
| 186 | 2 | $output->writeln("<info>✓ Successfully executed {$executedMigrations} migrations</info>"); |
|
| 187 | 2 | } catch (\Exception $e) { |
|
| 188 | throw new Console\Exception\RuntimeException('Error while executing migrations', $e->getCode(), $e); |
||
| 189 | 2 | } finally { |
|
| 190 | 2 | $databaseMigrationsLockCollection->update(['locked' => true], ['$set' => ['locked' => false]]); |
|
| 191 | 2 | $output->writeln("<info>✓ Successfully released migration lock</info>", $output::VERBOSITY_VERBOSE); |
|
| 192 | } |
||
| 193 | 2 | } |
|
| 194 | } |
||
| 195 |