Failed Conditions
Pull Request — master (#673)
by Jonathan
02:30 queued 31s
created

GenerateCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 23 1
A procOpen() 0 3 1
B execute() 0 25 2
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