Completed
Push — master ( 2d3818...4cdd36 )
by Jonathan
11s
created

DiffCommand::execute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 4.0058

Importance

Changes 0
Metric Value
cc 4
eloc 27
nc 5
nop 2
dl 0
loc 46
ccs 26
cts 28
cp 0.9286
crap 4.0058
rs 8.6315
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 Doctrine\Migrations\Generator\DiffGenerator;
8
use Doctrine\Migrations\Provider\OrmSchemaProvider;
9
use Doctrine\Migrations\Provider\SchemaProviderInterface;
10
use InvalidArgumentException;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use function class_exists;
15
use function sprintf;
16
17
/**
18
 * The DiffCommand class is responsible for generating a migration by comparing your current database schema to
19
 * your mapping information.
20
 */
21
class DiffCommand extends AbstractCommand
22
{
23
    /** @var null|SchemaProviderInterface */
24
    protected $schemaProvider;
25
26 6
    public function __construct(?SchemaProviderInterface $schemaProvider = null)
27
    {
28 6
        $this->schemaProvider = $schemaProvider;
29
30 6
        parent::__construct();
31 6
    }
32
33 6
    protected function configure() : void
34
    {
35 6
        parent::configure();
36
37
        $this
38 6
            ->setName('migrations:diff')
39 6
            ->setAliases(['diff'])
40 6
            ->setDescription('Generate a migration by comparing your current database to your mapping information.')
41 6
            ->setHelp(<<<EOT
42 6
The <info>%command.name%</info> command generates a migration by comparing your current database to your mapping information:
43
44
    <info>%command.full_name%</info>
45
46
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
47
48
    <info>%command.full_name% --editor-cmd=mate</info>
49
EOT
50
            )
51 6
            ->addOption(
52 6
                'editor-cmd',
53 6
                null,
54 6
                InputOption::VALUE_OPTIONAL,
55 6
                'Open file with this command upon creation.'
56
            )
57 6
            ->addOption(
58 6
                'filter-expression',
59 6
                null,
60 6
                InputOption::VALUE_OPTIONAL,
61 6
                'Tables which are filtered by Regular Expression.'
62
            )
63 6
            ->addOption(
64 6
                'formatted',
65 6
                null,
66 6
                InputOption::VALUE_NONE,
67 6
                'Format the generated SQL.'
68
            )
69 6
            ->addOption(
70 6
                'line-length',
71 6
                null,
72 6
                InputOption::VALUE_OPTIONAL,
73 6
                'Max line length of unformatted lines.',
74 6
                120
75
            )
76
        ;
77 6
    }
78
79 6
    public function execute(
80
        InputInterface $input,
81
        OutputInterface $output
82
    ) : ?int {
83 6
        $filterExpression = $input->getOption('filter-expression') ?? null;
84 6
        $formatted        = (bool) $input->getOption('formatted');
85 6
        $lineLength       = (int) $input->getOption('line-length');
86
87 6
        if ($formatted) {
88 2
            if (! class_exists('SqlFormatter')) {
89
                throw new InvalidArgumentException(
90
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
91
                );
92
            }
93
        }
94
95 6
        $versionNumber = $this->configuration->generateVersionNumber();
96
97 6
        $path = $this->createMigrationDiffGenerator()->generate(
98 6
            $versionNumber,
99 6
            $filterExpression,
100 6
            $formatted,
101 6
            $lineLength
102
        );
103
104 6
        $editorCommand = $input->getOption('editor-cmd');
105
106 6
        if ($editorCommand !== null) {
107 1
            $this->procOpen($editorCommand, $path);
108
        }
109
110 6
        $output->writeln([
111 6
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
112 6
            '',
113 6
            sprintf(
114 6
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>',
115 6
                $versionNumber
116
            ),
117 6
            '',
118 6
            sprintf(
119 6
                'To revert the migration you can use <info>migrations:execute --down %s</info>',
120 6
                $versionNumber
121
            ),
122
        ]);
123
124 6
        return 0;
125
    }
126
127 5
    protected function createMigrationDiffGenerator() : DiffGenerator
128
    {
129 5
        return new DiffGenerator(
130 5
            $this->connection->getConfiguration(),
131 5
            $this->connection->getSchemaManager(),
132 5
            $this->getSchemaProvider(),
133 5
            $this->connection->getDatabasePlatform(),
134 5
            $this->dependencyFactory->getMigrationGenerator(),
135 5
            $this->dependencyFactory->getMigrationSqlGenerator()
136
        );
137
    }
138
139 5
    private function getSchemaProvider() : SchemaProviderInterface
140
    {
141 5
        if ($this->schemaProvider === null) {
142 1
            $this->schemaProvider = new OrmSchemaProvider(
143 1
                $this->getHelper('entityManager')->getEntityManager()
144
            );
145
        }
146
147 5
        return $this->schemaProvider;
148
    }
149
}
150