MigrateDown::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 16
ccs 0
cts 2
cp 0
crap 2
rs 9.8333
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Console\Command\Migrate;
10
11
use Daikon\Dbal\Migration\MigrationInterface;
12
use Daikon\Dbal\Migration\MigrationTargetMap;
13
use Daikon\Interop\InvalidArgumentException;
14
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
final class MigrateDown extends Command
20
{
21
    private MigrationTargetMap $migrationTargetMap;
22
23
    public function __construct(MigrationTargetMap $migrationTargetMap)
24
    {
25
        $this->migrationTargetMap = $migrationTargetMap;
26
27
        parent::__construct();
28
    }
29
30
    protected function configure(): void
31
    {
32
        $this
33
            ->setName('migrate:down')
34
            ->setDescription('Migrate down to a specified migration version.')
35
            ->addOption(
36
                'target',
37
                null,
38
                InputOption::VALUE_REQUIRED,
39
                'Name of the target to migrate (if omitted all enabled targets will be migrated).'
40
            )
41
            ->addOption(
42
                'to',
43
                null,
44
                InputOption::VALUE_REQUIRED,
45
                'The version to migrate towards (if omitted all previous migrations will be reversed if possible).'
46
            );
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output): int
50
    {
51
        $target = $input->getOption('target');
52
        $version = intval($input->getOption('to'));
53
54
        foreach ($this->migrationTargetMap->getEnabledTargets() as $targetKey => $migrationTarget) {
55
            if ($target && $target !== $targetKey) {
56
                continue;
57
            }
58
            $output->writeln(sprintf('Reversing migrations for target <options=bold>%s</>', $targetKey));
59
            try {
60
                $reversedMigrations = $migrationTarget->migrate(MigrationInterface::MIGRATE_DOWN, $version);
61
                if ($reversedMigrations->count() > 0) {
62
                    foreach ($reversedMigrations as $migration) {
63
                        $output->writeln(sprintf(
64
                            '  <info>Reversed migration version %d (%s)</info>',
65
                            $migration->getVersion(),
66
                            $migration->getName()
67
                        ));
68
                    }
69
                } else {
70
                    $output->writeln('  <comment>No reversible migrations found</comment>');
71
                }
72
            } catch (InvalidArgumentException $exception) {
73
                $output->writeln('  <error>'.$exception->getMessage().'</error>');
74
            }
75
        }
76
77
        return 0;
78
    }
79
}
80