GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#8)
by Robert
04:55
created

MigrationsCommand   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 93.81%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 11
dl 0
loc 188
ccs 106
cts 113
cp 0.9381
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 32 1
F execute() 0 146 24
1
<?php declare(strict_types=1);
2
3
namespace Gruberro\MongoDbMigrations\Console\Command;
4
5
use Gruberro\MongoDbMigrations;
6
use MongoDB;
7
use Symfony\Component\Console;
8
9
class MigrationsCommand extends Console\Command\Command
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 7
    protected function configure()
15
    {
16
        $this
17 7
            ->setName('php-mongodb-migrations:migrate')
18 7
            ->setDescription('Execute all open migrations')
19 7
            ->addOption(
20 7
                'server',
21 7
                's',
22 7
                Console\Input\InputOption::VALUE_REQUIRED,
23 7
                'The connection string (e.g. mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db)',
24 7
                'mongodb://localhost:27017'
25
            )
26 7
            ->addOption(
27 7
                'contexts',
28 7
                'c',
29 7
                Console\Input\InputOption::VALUE_IS_ARRAY | Console\Input\InputOption::VALUE_REQUIRED,
30 7
                'A list of contexts evaluated with each migration of type ContextualMigrationInterface',
31 7
                []
32
            )
33 7
            ->addArgument(
34 7
                'database',
35 7
                Console\Input\InputArgument::REQUIRED,
36 7
                'The database to connect to'
37
            )
38 7
            ->addArgument(
39 7
                'migration-directories',
40 7
                Console\Input\InputArgument::IS_ARRAY,
41 7
                'List of directories containing migration classes',
42 7
                []
43
            )
44
        ;
45 7
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 8
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
51
    {
52 8
        $client = new MongoDB\Client($input->getOption('server'));
53 8
        $db = $client->selectDatabase($input->getArgument('database'));
54 8
        $output->writeln("<info>✓ Successfully established database connection</info>", $output::VERBOSITY_VERBOSE);
55
56 8
        $directories = $input->getArgument('migration-directories');
57 8
        foreach ($directories as $directory) {
58 8
            if (! is_dir($directory)) {
59 1
                throw new Console\Exception\InvalidArgumentException("'{$directory}' is no valid directory");
60
            }
61
62 8
            $output->writeln("<comment>Iterating '{$directory}' for potential migrations classes</comment>", $output::VERBOSITY_DEBUG);
63 8
            $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::LEAVES_ONLY);
64
65
            /** @var \SplFileInfo $file */
66 8
            foreach ($iterator as $file) {
67 8
                if ($file->getBasename('.php') === $file->getBasename()) {
68 8
                    continue;
69
                }
70
71 8
                require_once $file->getRealPath();
72
73 8
                $output->writeln("<comment>Loaded potential migration '{$file->getRealPath()}'</comment>", $output::VERBOSITY_DEBUG);
74
            }
75
        }
76
77
        /** @var MongoDbMigrations\MigrationInterface[] $migrations */
78 7
        $migrations = [];
79 7
        foreach (get_declared_classes() as $className) {
80 7
            $reflectionClass = new \ReflectionClass($className);
81
82 7
            if ($reflectionClass->implementsInterface(MongoDbMigrations\MigrationInterface::class)) {
83
                /** @var MongoDbMigrations\MigrationInterface $newInstance */
84 7
                $newInstance = $reflectionClass->newInstance();
85 7
                $id = md5($newInstance->getId());
86
87 7
                if (isset($migrations[$id])) {
88 1
                    $existingMigrationClass = get_class($migrations[$id]);
89 1
                    throw new Console\Exception\RuntimeException("Found a non unique migration id '{$newInstance->getId()}' in '{$reflectionClass->getName()}', already defined by migration class '{$existingMigrationClass}'");
90
                }
91
92 7
                $migrations[$id] = $newInstance;
93
94 7
                $output->writeln("<comment>Found valid migration class '{$reflectionClass->getName()}'</comment>", $output::VERBOSITY_DEBUG);
95
            }
96
        }
97
98 6
        $migrationClassesCount = count($migrations);
99 6
        $output->writeln("<info>✓ Found {$migrationClassesCount} valid migration classes</info>", $output::VERBOSITY_VERBOSE);
100
101 6
        uasort($migrations, function (MongoDbMigrations\MigrationInterface $a, MongoDbMigrations\MigrationInterface $b) {
102 6
            if ($a->getCreateDate() === $b->getCreateDate()) {
103
                return 0;
104
            }
105
106 6
            return $a->getCreateDate() < $b->getCreateDate() ? -1 : 1;
107 6
        });
108
109 6
        $output->writeln("<info>✓ Reordered all migration classes according to their create date</info>", $output::VERBOSITY_VERBOSE);
110
111 6
        $databaseMigrationsLockCollection = $db->selectCollection('DATABASE_MIGRATIONS_LOCK');
112 6
        $databaseMigrationsLockCollection->createIndex(['locked' => 1]);
113
114 6
        $currentLock = $databaseMigrationsLockCollection->findOneAndReplace(['locked' => ['$exists' => true]], ['locked' => true, 'last_locked_date' => new MongoDB\BSON\UTCDatetime((new \DateTime())->getTimestamp() * 1000)], ['upsert' => true]);
115 6
        if ($currentLock !== null && $currentLock->locked === true) {
116 1
            throw new Console\Exception\RuntimeException('Concurrent migrations are not allowed');
117
        }
118
119
        try {
120 5
            $output->writeln("<info>✓ Successfully acquired migration lock</info>", $output::VERBOSITY_VERBOSE);
121
122 5
            $databaseMigrationsCollection = $db->selectCollection('DATABASE_MIGRATIONS');
123 5
            $databaseMigrationsCollection->createIndex(['migration_id' => 1], ['unique' => true, 'background' => false]);
124
125 5
            $progress = new Console\Helper\ProgressBar($output, count($migrations));
126
127 5
            switch ($output->getVerbosity()) {
128 5
                case $output::VERBOSITY_VERBOSE:
129
                    $format = 'verbose';
130
                    break;
131 5
                case $output::VERBOSITY_VERY_VERBOSE:
132
                    $format = 'very_verbose';
133
                    break;
134 5
                case $output::VERBOSITY_DEBUG:
135
                    $format = 'debug';
136
                    break;
137
                default:
138 5
                    $format = 'normal';
139
            }
140
141 5
            $progress->setFormat($format);
142 5
            $progress->start();
143 5
            $executedMigrations = 0;
144
145 5
            foreach ($migrations as $id => $migration) {
146 5
                $progress->advance();
147
148 5
                if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface && $input->getOption('contexts') !== []) {
149 4
                    if ($migration->getContexts() === []) {
150 1
                        throw new \InvalidArgumentException('An empty context specification is not allowed');
151
                    }
152
153 4
                    if (array_intersect($migration->getContexts(), $input->getOption('contexts')) === []) {
154 2
                        continue;
155
                    }
156
                }
157
158 5
                if (!$migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface) {
159 4
                    if ($databaseMigrationsCollection->count(['migration_id' => $id]) > 0) {
160 1
                        continue;
161
                    }
162
                }
163
164 5
                $migration->execute($db);
165 5
                $executedMigrations++;
166
167
                $migrationInfo = [
168 5
                    'migration_id' => $id,
169 5
                    'migration_class' => get_class($migration),
170 5
                    'last_execution_date' => new MongoDB\BSON\UTCDatetime((new \DateTime())->getTimestamp() * 1000),
171 5
                    'run_always' => $migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface,
172
                ];
173
174 5
                if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface) {
175 2
                    $migrationInfo['contexts'] = $migration->getContexts();
176
                }
177
178 5
                $databaseMigrationsCollection->updateOne(
179 5
                    ['migration_id' => $id],
180 5
                    ['$set' => $migrationInfo],
181 5
                    ['upsert' => true]
182
                );
183
            }
184
185 3
            $progress->finish();
186 3
            $output->writeln('');
187
188 3
            $output->writeln("<info>✓ Successfully executed {$executedMigrations} migrations</info>");
189 2
        } catch (\Exception $e) {
190 2
            throw new Console\Exception\RuntimeException('Error while executing migrations', $e->getCode(), $e);
191 3
        } finally {
192 5
            $databaseMigrationsLockCollection->updateOne(['locked' => true], ['$set' => ['locked' => false]]);
193 5
            $output->writeln("<info>✓ Successfully released migration lock</info>", $output::VERBOSITY_VERBOSE);
194
        }
195 3
    }
196
}
197