CycleCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A defineOption() 0 3 1
A defineDescription() 0 3 1
A __construct() 0 6 1
A execute() 0 27 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.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 Biurad\Cycle\Commands\Migrations;
19
20
use Biurad\Cycle\Compiler;
21
use Biurad\Cycle\Generators\ShowChanges;
22
use Biurad\Cycle\Migrator;
23
use Cycle\Migrations\GenerateMigrations;
24
use Cycle\Schema\Registry;
25
use Spiral\Migrations\Config\MigrationConfig;
26
use Spiral\Migrations\State;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
final class CycleCommand extends AbstractCommand
32
{
33
    protected static $defaultName = 'migrations:cycle';
34
35
    /** @var Registry */
36
    private $registry;
37
38
    /** @var Compiler */
39
    private $compiler;
40
41
    public function __construct(Migrator $migrator, MigrationConfig $config, Registry $registry, Compiler $compiler)
42
    {
43
        $this->compiler = $compiler;
44
        $this->registry = $registry;
45
46
        parent::__construct($migrator, $config);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function defineDescription(): string
53
    {
54
        return 'Generate ORM schema migrations';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function defineOption(): array
61
    {
62
        return [new InputOption('run', 'r', InputOption::VALUE_NONE, 'Automatically run generated migration.')];
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function execute(InputInterface $input, OutputInterface $output): int
69
    {
70
        if (!$this->verifyConfigured($output)) {
71
            return 1;
72
        }
73
74
        foreach ($this->migrator->getMigrations() as $migration) {
75
            if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) {
76
                $output->writeln('<fg=red>Outstanding migrations found, run `migrations:start` first.</fg=red>');
77
78
                return 1;
79
            }
80
        }
81
82
        $this->compiler->addGenerator($show = new ShowChanges($output));
83
        $this->compiler->compile($this->registry);
84
85
        if ($show->hasChanges()) {
86
            (new GenerateMigrations($this->migrator->getRepository(), $this->config))->run($this->registry);
87
88
            if ($input->getOption('run')) {
89
                return $this->getApplication()->find('migrations:start')
90
                    ->run($input, $output);
91
            }
92
        }
93
94
        return 0;
95
    }
96
}
97