Completed
Push — master ( 368556...6bf94b )
by Jonathan
11s
created

GenerateCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 21
cts 23
cp 0.913
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
A execute() 0 15 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(sprintf('Generated new migration class to "<info>%s</info>"', $path));
56 3
    }
57
58
    protected function procOpen(string $editorCommand, string $path) : void
59
    {
60
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
61
    }
62
}
63