Passed
Push — master ( a4e384...6a021e )
by Divine Niiquaye
03:11
created

DatabaseOrmCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\Commands\CycleORM;
19
20
use Cycle\Database\DatabaseProviderInterface;
0 ignored issues
show
Bug introduced by
The type Cycle\Database\DatabaseProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Cycle\Migrations\Migrator;
0 ignored issues
show
Bug introduced by
The type Cycle\Migrations\Migrator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Cycle\Migrations\State;
0 ignored issues
show
Bug introduced by
The type Cycle\Migrations\State was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Cycle\Schema\Compiler;
0 ignored issues
show
Bug introduced by
The type Cycle\Schema\Compiler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Cycle\Schema\Generator\Migrations\GenerateMigrations;
0 ignored issues
show
Bug introduced by
The type Cycle\Schema\Generator\M...ions\GenerateMigrations was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Cycle\Schema\Generator\SyncTables;
0 ignored issues
show
Bug introduced by
The type Cycle\Schema\Generator\SyncTables was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Cycle\Schema\GeneratorInterface;
0 ignored issues
show
Bug introduced by
The type Cycle\Schema\GeneratorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Cycle\Schema\Registry;
0 ignored issues
show
Bug introduced by
The type Cycle\Schema\Registry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Rade\DI\Extensions\CycleORM\Generator\ShowChanges;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
use Symfony\Component\Console\Style\SymfonyStyle;
33
34
/**
35
 * Cycle ORM synchronizer command.
36
 *
37
 * @author Divine Niiquaye Ibok <[email protected]>
38
 */
39
class DatabaseOrmCommand extends Command
40
{
41
    protected static $defaultName = 'cycle:database:orm';
42
43
    protected static $defaultDescription = 'Generate ORM schema migrations and run if possible';
44
45
    /**
46
     * @param array<int,GeneratorInterface> $generators
47
     */
48
    public function __construct(private Migrator $migrator, private DatabaseProviderInterface $provider, private array $generators)
49
    {
50
        parent::__construct();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output): int
57
    {
58
        $this->migrator->configure();
59
        $io = new SymfonyStyle($input, $output);
60
61
        foreach ($this->migrator->getMigrations() as $migration) {
62
            if (State::STATUS_EXECUTED !== $migration->getState()->getStatus()) {
63
                $io->error('Outstanding migrations found, run `cycle:database:migrate` first.');
64
65
                return self::FAILURE;
66
            }
67
        }
68
69
        $this->generators[] = $show = new ShowChanges($output);
70
71
        if (!\class_exists(GenerateMigrations::class)) {
72
            $io->warning('Synchronizing ORM schema into database is a ricky operation, instead "composer require cycle/schema-migrations-generator".');
73
74
            if (!$io->confirm('Do you want to proceed now?', false)) {
75
                return self::SUCCESS;
76
            }
77
78
            $this->generators[] = new SyncTables();
79
            $autoMigrate = false;
80
        }
81
82
        (new Compiler())->compile($registry = new Registry($this->provider), $this->generators);
83
84
        if ($show->hasChanges()) {
85
            if (!isset($autoMigrate)) {
86
                (new GenerateMigrations($this->migrator->getRepository(), $this->migrator->getConfig()))->run($registry);
87
88
                if ($io->confirm('Do you want to run migrations now?', false)) {
89
                    return $this->getApplication()->find('cycle:database:migrate')->run($input, $output);
90
                }
91
            } else {
92
                $io->writeln("\n<info>ORM Schema has been synchronized</info>");
93
            }
94
        }
95
96
        return self::SUCCESS;
97
    }
98
}
99