Passed
Pull Request — master (#148)
by Rustam
12:16
created

UpCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
eloc 53
c 2
b 0
f 0
dl 0
loc 82
ccs 0
cts 50
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C execute() 0 69 14
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
    public function __construct(CycleDependencyProxy $promise, EventDispatcherInterface $eventDispatcher)
27
    {
28
        $this->eventDispatcher = $eventDispatcher;
29
        parent::__construct($promise);
30
    }
31
32
    protected function execute(InputInterface $input, OutputInterface $output): int
33
    {
34
        $migrations = $this->findMigrations($output);
35
        // check any not executed migration
36
        $exist = false;
37
        foreach ($migrations as $migration) {
38
            if ($migration->getState()->getStatus() === State::STATUS_PENDING) {
39
                $exist = true;
40
                break;
41
            }
42
        }
43
        if (!$exist) {
44
            $output->writeln('<fg=red>No migration found for execute</>');
45
            return ExitCode::OK;
46
        }
47
48
        $migrator = $this->promise->getMigrator();
49
50
        // Confirm
51
        if (!$migrator->getConfig()->isSafe()) {
52
            $newMigrations = [];
53
            foreach ($migrations as $migration) {
54
                if ($migration->getState()->getStatus() === State::STATUS_PENDING) {
55
                    $newMigrations[] = $migration;
56
                }
57
            }
58
            $countNewMigrations = count($newMigrations);
59
            $output->writeln(
60
                '<fg=yellow>' .
61
                ($countNewMigrations === 1 ? 'Migration' : $countNewMigrations . ' migrations') .
62
                ' ' .
63
                'to be applied:</>'
64
            );
65
            foreach ($newMigrations as $migration) {
66
                $output->writeln('— <fg=cyan>' . $migration->getState()->getName() . '</>');
67
            }
68
            if ($input->isInteractive()) {
69
                $question = new ConfirmationQuestion(
70
                    'Apply the above ' .
71
                    ($countNewMigrations === 1 ? 'migration' : 'migrations') .
72
                    '? (yes|no) ',
73
                    false
74
                );
75
                /** @var QuestionHelper $qaHelper*/
76
                $qaHelper = $this->getHelper('question');
77
                if (!$qaHelper->ask($input, $output, $question)) {
78
                    return ExitCode::OK;
79
                }
80
            }
81
        }
82
83
        $limit = PHP_INT_MAX;
84
        $this->eventDispatcher->dispatch(new BeforeMigrate());
85
        try {
86
            do {
87
                $migration = $migrator->run();
88
                if (!$migration instanceof MigrationInterface) {
89
                    break;
90
                }
91
92
                $state = $migration->getState();
93
                $status = $state->getStatus();
94
                $output->writeln('<fg=cyan>' . $state->getName() . '</>: '
95
                    . (self::MIGRATION_STATUS[$status] ?? $status));
96
            } while (--$limit > 0);
97
        } finally {
98
            $this->eventDispatcher->dispatch(new AfterMigrate());
99
        }
100
        return ExitCode::OK;
101
    }
102
}
103