Passed
Push — master ( 636d9d...66682c )
by Alexander
01:58
created

UpCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
dl 0
loc 83
ccs 0
cts 41
cp 0
rs 10
c 1
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C execute() 0 65 13
A configure() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Command\Migration;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Spiral\Migrations\MigrationInterface;
9
use Spiral\Migrations\State;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\ConfirmationQuestion;
13
use Yiisoft\Yii\Console\ExitCode;
14
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
15
use Yiisoft\Yii\Cycle\Event\AfterMigrate;
16
use Yiisoft\Yii\Cycle\Event\BeforeMigrate;
17
18
final class UpCommand extends BaseMigrationCommand
19
{
20
    protected static $defaultName = 'migrate/up';
21
22
    private EventDispatcherInterface $eventDispatcher;
23
24
    public function __construct(CycleDependencyProxy $promise, EventDispatcherInterface $eventDispatcher)
25
    {
26
        $this->eventDispatcher = $eventDispatcher;
27
        parent::__construct($promise);
28
    }
29
30
    public function configure(): void
31
    {
32
        $this
33
            ->setDescription('Execute all new migrations');
34
    }
35
36
    protected function execute(InputInterface $input, OutputInterface $output): int
37
    {
38
        $migrations = $this->findMigrations($output);
39
        // check any not executed migration
40
        $exist = false;
41
        foreach ($migrations as $migration) {
42
            if ($migration->getState()->getStatus() === State::STATUS_PENDING) {
43
                $exist = true;
44
                break;
45
            }
46
        }
47
        if (!$exist) {
48
            $output->writeln('<fg=red>No migration found for execute</>');
49
            return ExitCode::OK;
50
        }
51
52
        $migrator = $this->promise->getMigrator();
53
54
        // Confirm
55
        if (!$migrator->getConfig()->isSafe()) {
56
            $newMigrations = [];
57
            foreach ($migrations as $migration) {
58
                if ($migration->getState()->getStatus() === State::STATUS_PENDING) {
59
                    $newMigrations[] = $migration;
60
                }
61
            }
62
            $countNewMigrations = count($newMigrations);
63
            $output->writeln(
64
                '<fg=yellow>' .
65
                ($countNewMigrations === 1 ? 'Migration' : $countNewMigrations . ' migrations') .
66
                ' ' .
67
                'to be applied:</>'
68
            );
69
            foreach ($newMigrations as $migration) {
70
                $output->writeln('— <fg=cyan>' . $migration->getState()->getName() . '</>');
71
            }
72
            $question = new ConfirmationQuestion(
73
                'Apply the above ' .
74
                ($countNewMigrations === 1 ? 'migration' : 'migrations') .
75
                '? (yes|no) ',
76
                false
77
            );
78
            if (!$this->getHelper('question')->ask($input, $output, $question)) {
79
                return ExitCode::OK;
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
                    . (static::MIGRATION_STATUS[$status] ?? $status));
96
            } while (--$limit > 0);
97
        } finally {
98
            $this->eventDispatcher->dispatch(new AfterMigrate());
99
        }
100
        return ExitCode::OK;
101
    }
102
}
103