Completed
Push — master ( f95233...cf7f94 )
by Andreas
15s queued 10s
created

DiffCommand::execute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 4.0047

Importance

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