Passed
Pull Request — master (#176)
by Maxim
17:36 queued 14:52
created

BaseMigrationCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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\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
        try {
45 1
            $migrationFile = $migrator->getRepository()->registerMigration(
46 1
                $migrationSkeleton->buildFileName(),
47 1
                $migrationSkeleton->getClass()->getName(),
48 1
                $migrationSkeleton->getFile()->render()
49 1
            );
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 1
        $output->writeln('<info>New migration file has been created</info>');
56 1
        $output->writeln("<fg=cyan>{$migrationFile}</>");
57 1
        return $migrationSkeleton;
58
    }
59
60
    /**
61
     * @param OutputInterface $output
62
     *
63
     * @return MigrationInterface[]
64
     */
65 5
    protected function findMigrations(OutputInterface $output): array
66
    {
67 5
        $list = $this->promise->getMigrator()->getMigrations();
68 5
        $output->writeln(
69 5
            sprintf(
70 5
                '<info>Total %d migration(s) found in %s</info>',
71 5
                count($list),
72 5
                $this->promise->getMigrationConfig()->getDirectory()
73 5
            )
74 5
        );
75 5
        return $list;
76
    }
77
}
78