Failed Conditions
Push — master ( 98f68e...77cd29 )
by Jonathan
11s
created

GenerateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 23
ccs 11
cts 11
cp 1
crap 1
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use function escapeshellarg;
11
use function proc_open;
12
use function sprintf;
13
14
class GenerateCommand extends AbstractCommand
15
{
16 9
    protected function configure() : void
17
    {
18
        $this
19 9
            ->setName('migrations:generate')
20 9
            ->setDescription('Generate a blank migration class.')
21 9
            ->addOption(
22 9
                'editor-cmd',
23 9
                null,
24 9
                InputOption::VALUE_OPTIONAL,
25 9
                'Open file with this command upon creation.'
26
            )
27 9
            ->setHelp(<<<EOT
28 9
The <info>%command.name%</info> command generates a blank migration class:
29
30
    <info>%command.full_name%</info>
31
32
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
33
34
    <info>%command.full_name% --editor-cmd=mate</info>
35
EOT
36
        );
37
38 9
        parent::configure();
39 9
    }
40
41 3
    public function execute(InputInterface $input, OutputInterface $output) : void
42
    {
43 3
        $versionNumber = $this->configuration->generateVersionNumber();
44
45 3
        $migrationGenerator = $this->dependencyFactory->getMigrationGenerator();
46
47 3
        $path = $migrationGenerator->generateMigration($versionNumber);
48
49 3
        $editorCommand = $input->getOption('editor-cmd');
50
51 3
        if ($editorCommand !== null) {
52 1
            $this->procOpen($editorCommand, $path);
53
        }
54
55 3
        $output->writeln([
56 3
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
57 3
            '',
58 3
            sprintf(
59 3
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>',
60 3
                $versionNumber
61
            ),
62 3
            '',
63 3
            sprintf(
64 3
                'To revert the migration you can use <info>migrations:execute --down %s</info>',
65 3
                $versionNumber
66
            ),
67
        ]);
68 3
    }
69
70
    protected function procOpen(string $editorCommand, string $path) : void
71
    {
72
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
73
    }
74
}
75