UpCommand::execute()   C
last analyzed

Complexity

Conditions 14
Paths 60

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 47
CRAP Score 14

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 47
nc 60
nop 2
dl 0
loc 69
ccs 47
cts 47
cp 1
crap 14
rs 6.2666
c 2
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\MigrationInterface;
8
use Cycle\Migrations\State;
9
use Psr\EventDispatcher\EventDispatcherInterface;
10
use Symfony\Component\Console\Helper\QuestionHelper;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\ConfirmationQuestion;
14
use Yiisoft\Yii\Console\ExitCode;
15
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
16
use Yiisoft\Yii\Cycle\Event\AfterMigrate;
17
use Yiisoft\Yii\Cycle\Event\BeforeMigrate;
18
19
final class UpCommand extends BaseMigrationCommand
20
{
21
    protected static $defaultName = 'migrate/up';
22
    protected static $defaultDescription = 'Executes all new migrations';
23
24
    private EventDispatcherInterface $eventDispatcher;
25
26 7
    public function __construct(CycleDependencyProxy $promise, EventDispatcherInterface $eventDispatcher)
27
    {
28 7
        $this->eventDispatcher = $eventDispatcher;
29 7
        parent::__construct($promise);
30
    }
31
32 7
    protected function execute(InputInterface $input, OutputInterface $output): int
33
    {
34 7
        $migrations = $this->findMigrations($output);
35
        // check any not executed migration
36 7
        $exist = false;
37 7
        foreach ($migrations as $migration) {
38 6
            if ($migration->getState()->getStatus() === State::STATUS_PENDING) {
39 6
                $exist = true;
40 6
                break;
41
            }
42
        }
43 7
        if (!$exist) {
44 1
            $output->writeln('<fg=red>No migration found for execute</>');
45 1
            return ExitCode::OK;
46
        }
47
48 6
        $migrator = $this->promise->getMigrator();
49
50
        // Confirm
51 6
        if (!$migrator->getConfig()->isSafe()) {
52 6
            $newMigrations = [];
53 6
            foreach ($migrations as $migration) {
54 6
                if ($migration->getState()->getStatus() === State::STATUS_PENDING) {
55 6
                    $newMigrations[] = $migration;
56
                }
57
            }
58 6
            $countNewMigrations = count($newMigrations);
59 6
            $output->writeln(
60 6
                '<fg=yellow>' .
61 6
                ($countNewMigrations === 1 ? 'Migration' : $countNewMigrations . ' migrations') .
62 6
                ' ' .
63 6
                'to be applied:</>'
64 6
            );
65 6
            foreach ($newMigrations as $migration) {
66 6
                $output->writeln('— <fg=cyan>' . $migration->getState()->getName() . '</>');
67
            }
68 6
            if ($input->isInteractive()) {
69 2
                $question = new ConfirmationQuestion(
70 2
                    'Apply the above ' .
71 2
                    ($countNewMigrations === 1 ? 'migration' : 'migrations') .
72 2
                    '? (yes|no) ',
73 2
                    false
74 2
                );
75
                /** @var QuestionHelper $qaHelper*/
76 2
                $qaHelper = $this->getHelper('question');
77 2
                if (!$qaHelper->ask($input, $output, $question)) {
78 2
                    return ExitCode::OK;
79
                }
80
            }
81
        }
82
83 4
        $limit = PHP_INT_MAX;
84 4
        $this->eventDispatcher->dispatch(new BeforeMigrate());
85
        try {
86
            do {
87 4
                $migration = $migrator->run();
88 4
                if (!$migration instanceof MigrationInterface) {
89 4
                    break;
90
                }
91
92 4
                $state = $migration->getState();
93 4
                $status = $state->getStatus();
94 4
                $output->writeln('<fg=cyan>' . $state->getName() . '</>: '
95 4
                    . (self::MIGRATION_STATUS[$status] ?? $status));
96 4
            } while (--$limit > 0);
97
        } finally {
98 4
            $this->eventDispatcher->dispatch(new AfterMigrate());
99
        }
100 4
        return ExitCode::OK;
101
    }
102
}
103