Passed
Push — master ( c40d9e...5e9dce )
by Aleksei
03:17
created

BaseMigrationCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 87.88%

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 65
ccs 29
cts 33
cp 0.8788
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findMigrations() 0 11 1
A createEmptyMigration() 0 31 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Command\Migration;
6
7
use Cycle\Migrations\Exception\RepositoryException;
8
use Cycle\Migrations\MigrationInterface;
9
use Cycle\Migrations\State;
10
use Cycle\Schema\Generator\Migrations\MigrationImage;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
14
15
abstract class BaseMigrationCommand extends Command
16
{
17
    protected CycleDependencyProxy $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 9
    public function __construct(CycleDependencyProxy $promise)
26
    {
27 9
        $this->promise = $promise;
28 9
        parent::__construct();
29
    }
30
31 1
    protected function createEmptyMigration(
32
        OutputInterface $output,
33
        string $name,
34
        ?string $database = null
35
    ): ?MigrationImage {
36 1
        if ($database === null) {
37
            // get default database
38 1
            $database = $this->promise->getDatabaseProvider()->database()->getName();
39
        }
40 1
        $migrator = $this->promise->getMigrator();
41
42 1
        $migrationSkeleton = new MigrationImage($this->promise->getMigrationConfig(), $database);
43 1
        $migrationSkeleton->setName($name);
44
45 1
        $className = $migrationSkeleton->getClass()->getName();
46 1
        \assert($className !== null);
47
48
        try {
49 1
            $migrationFile = $migrator->getRepository()->registerMigration(
50 1
                $migrationSkeleton->buildFileName(),
51 1
                $className,
52 1
                $migrationSkeleton->getFile()->render()
53 1
            );
54
        } catch (RepositoryException $e) {
55
            $output->writeln('<fg=yellow>Can not create migration</>');
56
            $output->writeln('<fg=red>' . $e->getMessage() . '</>');
57
            return null;
58
        }
59 1
        $output->writeln('<info>New migration file has been created</info>');
60 1
        $output->writeln("<fg=cyan>{$migrationFile}</>");
61 1
        return $migrationSkeleton;
62
    }
63
64
    /**
65
     * @param OutputInterface $output
66
     *
67
     * @return MigrationInterface[]
68
     */
69 5
    protected function findMigrations(OutputInterface $output): array
70
    {
71 5
        $list = $this->promise->getMigrator()->getMigrations();
72 5
        $output->writeln(
73 5
            sprintf(
74 5
                '<info>Total %d migration(s) found in %s</info>',
75 5
                count($list),
76 5
                $this->promise->getMigrationConfig()->getDirectory()
77 5
            )
78 5
        );
79 5
        return $list;
80
    }
81
}
82