GenerateCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 41

Duplication

Lines 5
Ratio 12.2 %

Code Coverage

Tests 23
CRAP Score 4.0245

Importance

Changes 0
Metric Value
dl 5
loc 41
ccs 23
cts 26
cp 0.8846
rs 9.264
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 4.0245
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Exception;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use function assert;
12
use function is_string;
13
use function key;
14
use function sprintf;
15
16
/**
17
 * The GenerateCommand class is responsible for generating a blank migration class for you to modify to your needs.
18
 */
19
final class GenerateCommand extends DoctrineCommand
20
{
21
    /** @var string */
22
    protected static $defaultName = 'migrations:generate';
23
24 1
    protected function configure() : void
25
    {
26
        $this
27 1
            ->setAliases(['generate'])
28 1
            ->setDescription('Generate a blank migration class.')
29 1
            ->addOption(
30 1
                'namespace',
31 1
                null,
32 1
                InputOption::VALUE_REQUIRED,
33 1
                'The namespace to use for the migration (must be in the list of configured namespaces)'
34
            )
35 1
            ->setHelp(<<<EOT
36 1
The <info>%command.name%</info> command generates a blank migration class:
37
38
    <info>%command.full_name%</info>
39
40
EOT
41
            );
42
43 1
        parent::configure();
44 1
    }
45
46 1
    protected function execute(InputInterface $input, OutputInterface $output) : int
47
    {
48 1
        $configuration = $this->getDependencyFactory()->getConfiguration();
49
50 1
        $migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator();
51
52 1
        $namespace = $input->getOption('namespace');
53 1
        if ($namespace === '') {
54
            $namespace = null;
55
        }
56
57 1
        $dirs = $configuration->getMigrationDirectories();
58 1 View Code Duplication
        if ($namespace === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59 1
            $namespace = key($dirs);
60
        } elseif (! isset($dirs[$namespace])) {
61
            throw new Exception(sprintf('Path not defined for the namespace %s', $namespace));
62
        }
63
64 1
        assert(is_string($namespace));
65
66 1
        $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
67
68 1
        $path = $migrationGenerator->generateMigration($fqcn);
69
70 1
        $this->io->text([
71 1
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
72 1
            '',
73 1
            sprintf(
74 1
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>',
75 1
                $fqcn
76
            ),
77 1
            '',
78 1
            sprintf(
79 1
                'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
80 1
                $fqcn
81
            ),
82 1
            '',
83
        ]);
84
85 1
        return 0;
86
    }
87
}
88