Failed Conditions
Pull Request — master (#944)
by Asmir
02:33
created

GenerateCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 63
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 20 1
A execute() 0 36 4
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') ?: null;
53
54 1
        $dirs = $configuration->getMigrationDirectories();
55 1
        if ($namespace === null) {
56 1
            $namespace = key($dirs);
57
        } elseif (! isset($dirs[$namespace])) {
58
            throw new Exception(sprintf('Path not defined for the namespace %s', $namespace));
59
        }
60
61 1
        assert(is_string($namespace));
62
63 1
        $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
64
65 1
        $path = $migrationGenerator->generateMigration($fqcn);
66
67 1
        $output->writeln([
68 1
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
69 1
            '',
70 1
            sprintf(
71 1
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>',
72 1
                $fqcn
73
            ),
74 1
            '',
75 1
            sprintf(
76 1
                'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
77 1
                $fqcn
78
            ),
79
        ]);
80
81 1
        return 0;
82
    }
83
}
84