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 (#19)
by Robert
02:31
created

MigrationsCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 75
c 2
b 0
f 0
dl 0
loc 119
ccs 69
cts 75
cp 0.92
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
F execute() 0 78 13
A configure() 0 29 1
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 AbstractCommand
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
        $directories = $input->getArgument('migration-directories');
53 8
        $migrations = $this->getMigrations($directories);
1 ignored issue
show
Bug introduced by
It seems like $directories can also be of type null and string; however, parameter $directories of Gruberro\MongoDbMigratio...ommand::getMigrations() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $migrations = $this->getMigrations(/** @scrutinizer ignore-type */ $directories);
Loading history...
54 6
        $db = $this->connect($input->getOption('server'), $input->getArgument('database'));
55
56 6
        $this->acquireLock($db);
57
58
        try {
59 5
            $databaseMigrationsCollection = $this->getMigrationsCollection($db);
60 5
            $progress = new Console\Helper\ProgressBar($output, count($migrations));
61
62 5
            switch ($output->getVerbosity()) {
63 5
                case $output::VERBOSITY_VERBOSE:
64
                    $format = 'verbose';
65
                    break;
66 5
                case $output::VERBOSITY_VERY_VERBOSE:
67
                    $format = 'very_verbose';
68
                    break;
69 5
                case $output::VERBOSITY_DEBUG:
70
                    $format = 'debug';
71
                    break;
72
                default:
73 5
                    $format = 'normal';
74
            }
75
76 5
            $progress->setFormat($format);
77 5
            $progress->start();
78 5
            $executedMigrations = 0;
79
80 5
            foreach ($migrations as $id => $migration) {
81 5
                $progress->advance();
82
83 5
                if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface && $input->getOption('contexts') !== []) {
84 4
                    if ($migration->getContexts() === []) {
85 1
                        throw new \InvalidArgumentException('An empty context specification is not allowed');
86
                    }
87
88 4
                    if (array_intersect($migration->getContexts(), $input->getOption('contexts')) === []) {
1 ignored issue
show
Bug introduced by
It seems like $input->getOption('contexts') can also be of type boolean and null and string; however, parameter $array2 of array_intersect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                    if (array_intersect($migration->getContexts(), /** @scrutinizer ignore-type */ $input->getOption('contexts')) === []) {
Loading history...
89 2
                        continue;
90
                    }
91
                }
92
93 5
                if (!$migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface) {
94 5
                    if ($databaseMigrationsCollection->countDocuments(['migration_id' => $id]) > 0) {
95 1
                        continue;
96
                    }
97
                }
98
99 5
                $migration->execute($db);
100 5
                $executedMigrations++;
101
102
                $migrationInfo = [
103 5
                    'migration_id' => $id,
104 5
                    'migration_class' => get_class($migration),
105 5
                    'last_execution_date' => new MongoDB\BSON\UTCDatetime((new \DateTime())->getTimestamp() * 1000),
106 5
                    'run_always' => $migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface,
107
                ];
108
109 5
                if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface) {
110 2
                    $migrationInfo['contexts'] = $migration->getContexts();
111
                }
112
113 5
                $databaseMigrationsCollection->updateOne(
114 5
                    ['migration_id' => $id],
115 5
                    ['$set' => $migrationInfo],
116 5
                    ['upsert' => true]
117
                );
118
            }
119
120 3
            $progress->finish();
121 3
            $output->writeln('');
122
123 3
            $output->writeln("<info>✓ Successfully executed {$executedMigrations} migrations</info>");
124 2
        } catch (\Exception $e) {
125 2
            throw new Console\Exception\RuntimeException('Error while executing migrations', $e->getCode(), $e);
126 3
        } finally {
127 5
            $this->releaseLock($db);
128
        }
129 3
    }
130
}
131