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

BaseMigrationCommand::findMigrations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Command\Migration;
6
7
use Cycle\Migrations\MigrationImage;
8
use Spiral\Migrations\Exception\RepositoryException;
9
use Spiral\Migrations\MigrationInterface;
10
use Spiral\Migrations\State;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Yiisoft\Yii\Cycle\Command\CycleDependencyPromise;
14
15
abstract class BaseMigrationCommand extends Command
16
{
17
    protected CycleDependencyPromise $promise;
18
19
    protected const MIGRATION_STATUS = [
20
        State::STATUS_UNDEFINED => 'undefined',
21
        State::STATUS_PENDING => 'pending',
22
        State::STATUS_EXECUTED => 'executed',
23
    ];
24
25
    public function __construct(CycleDependencyPromise $promise)
26
    {
27
        $this->promise = $promise;
28
        parent::__construct();
29
    }
30
31
    protected function createEmptyMigration(
32
        OutputInterface $output,
33
        string $name,
34
        ?string $database = null
35
    ): ?MigrationImage {
36
        if ($database === null) {
37
            // get default database
38
            $database = $this->promise->getDatabaseManager()->database()->getName();
39
        }
40
        $migrator = $this->promise->getMigrator();
41
42
        $migrationSkeleton = new MigrationImage($this->promise->getMigrationConfig(), $database);
43
        $migrationSkeleton->setName($name);
44
        try {
45
            $migrationFile = $migrator->getRepository()->registerMigration(
46
                $migrationSkeleton->buildFileName(),
47
                $migrationSkeleton->getClass()->getName(),
48
                $migrationSkeleton->getFile()->render()
49
            );
50
        } catch (RepositoryException $e) {
51
            $output->writeln('<fg=yellow>Can not create migration</>');
52
            $output->writeln('<fg=red>' . $e->getMessage() . '</>');
53
            return null;
54
        }
55
        $output->writeln('<info>New migration file has been created</info>');
56
        $output->writeln("<fg=cyan>{$migrationFile}</>");
57
        return $migrationSkeleton;
58
    }
59
60
    /**
61
     * @param OutputInterface $output
62
     * @return MigrationInterface[]
63
     */
64
    protected function findMigrations(OutputInterface $output): array
65
    {
66
        $list = $this->promise->getMigrator()->getMigrations();
67
        $output->writeln(
68
            sprintf(
69
                '<info>Total %d migration(s) found in %s</info>',
70
                count($list),
71
                $this->promise->getMigrationConfig()->getDirectory()
72
            )
73
        );
74
        return $list;
75
    }
76
}
77