Passed
Push — master ( 227314...d8a269 )
by Alexander
14:02
created

GenerateCommand::execute()   B

Complexity

Conditions 10
Paths 13

Size

Total Lines 60
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 35
nc 13
nop 2
dl 0
loc 60
ccs 0
cts 47
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Command\Migration;
6
7
use Cycle\Migrations\GenerateMigrations;
8
use Cycle\Schema\Compiler;
9
use Cycle\Schema\Registry;
10
use Spiral\Migrations\State;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\StreamableInputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\ConfirmationQuestion;
15
use Symfony\Component\Console\Question\Question;
16
use Yiisoft\Yii\Console\ExitCode;
17
use Yiisoft\Yii\Cycle\Conveyor\SchemaConveyorInterface;
18
use Yiisoft\Yii\Cycle\Generator\PrintChanges;
19
20
final class GenerateCommand extends BaseMigrationCommand
21
{
22
    protected static $defaultName = 'migrate/generate';
23
24
    public function configure(): void
25
    {
26
        $this->setDescription('Generates a migration');
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output): int
30
    {
31
        $migrator = $this->promise->getMigrator();
32
        // check existing unapplied migrations
33
        $listAfter = $migrator->getMigrations();
34
        foreach ($listAfter as $migration) {
35
            if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) {
36
                $output->writeln('<fg=red>Outstanding migrations found, run `migrate/up` first.</>');
37
                return ExitCode::OK;
38
            }
39
        }
40
        $conveyor = $this->promise->getSchemaConveyor();
41
42
        // migrations generator
43
        $conveyor->addGenerator(
44
            SchemaConveyorInterface::STAGE_USERLAND,
45
            new GenerateMigrations($migrator->getRepository(), $this->promise->getMigrationConfig())
46
        );
47
        // show DB changes
48
        $conveyor->addGenerator(SchemaConveyorInterface::STAGE_USERLAND, new PrintChanges($output));
49
        // compile schema and convert diffs to new migrations
50
        (new Compiler())->compile(new Registry($this->promise->getDatabaseManager()), $conveyor->getGenerators());
51
52
        // compare migrations list before and after
53
        $listBefore = $migrator->getMigrations();
54
        $added = count($listBefore) - count($listAfter);
55
        $output->writeln("<info>Added {$added} file(s)</info>");
56
57
        // print added migrations
58
        if ($added > 0) {
59
            foreach ($listBefore as $migration) {
60
                if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) {
61
                    $output->writeln($migration->getState()->getName());
62
                }
63
            }
64
        } else {
65
            $output->writeln(
66
                '<info>If you want to create new empty migration, use <fg=yellow>migrate/create</></info>'
67
            );
68
69
            $qaHelper = $this->getHelper('question');
70
71
            if ($input->isInteractive() && $input instanceof StreamableInputInterface) {
72
                $question = new ConfirmationQuestion('Would you like to create empty migration right now? (Y/n)', true);
73
                $answer = $qaHelper->ask($input, $output, $question);
74
                if (!$answer) {
75
                    return ExitCode::OK;
76
                }
77
                // get the name for a new migration
78
                $question = new Question('Please enter an unique name for the new migration: ');
79
                $name = $qaHelper->ask($input, $output, $question);
80
                if (empty($name)) {
81
                    $output->writeln('<fg=red>You entered an empty name. Exit</>');
82
                    return ExitCode::OK;
83
                }
84
                // create an empty migration
85
                $this->createEmptyMigration($output, $name);
86
            }
87
        }
88
        return ExitCode::OK;
89
    }
90
}
91