MigrationUpCommand::getNewMigrations()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 13
nc 9
nop 0
1
<?php
2
3
namespace WebComplete\core\utils\migration\commands;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
use WebComplete\core\cube\CubeManager;
11
use WebComplete\core\utils\migration\MigrationService;
12
13
class MigrationUpCommand extends Command
14
{
15
16
    /**
17
     * @var CubeManager
18
     */
19
    protected $cubeManager;
20
    /**
21
     * @var MigrationService
22
     */
23
    protected $migrationService;
24
25
    /**
26
     * @param CubeManager $cubeManager
27
     * @param MigrationService $migrationService
28
     *
29
     * @throws \Symfony\Component\Console\Exception\LogicException
30
     */
31
    public function __construct(CubeManager $cubeManager, MigrationService $migrationService)
32
    {
33
        parent::__construct();
34
        $this->cubeManager = $cubeManager;
35
        $this->migrationService = $migrationService;
36
    }
37
38
    /**
39
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
40
     */
41
    protected function configure()
42
    {
43
        $this->setName('migrate:up')
44
            ->setDescription('Execute migration[s]')
45
            ->addArgument('class', InputArgument::OPTIONAL, 'Migration class');
46
    }
47
48
    /**
49
     * @param InputInterface $input
50
     * @param OutputInterface $output
51
     *
52
     * @return null|int
53
     * @throws \Symfony\Component\Console\Exception\LogicException
54
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
55
     * @throws \Psr\Container\NotFoundExceptionInterface
56
     * @throws \Psr\Container\ContainerExceptionInterface
57
     * @throws \Exception
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        if ($class = $input->getArgument('class')) {
62
            $newMigrations = [$class];
63
        } else {
64
            $newMigrations = $this->getNewMigrations();
65
        }
66
67
        if (!$newMigrations) {
68
            $output->writeln('New migrations not found');
69
            return null;
70
        }
71
72
        $message = "New migrations: \n";
73
        foreach ($newMigrations as $migrationClass) {
74
            $message .= $migrationClass . "\n";
75
        }
76
        $output->writeln($message . "\n");
77
78
        $helper = $this->getHelper('question');
79
        $question = new ConfirmationQuestion('Continue? (Y/N): ', true);
80
        if ($class || $helper->ask($input, $output, $question)) {
81
            foreach ($newMigrations as $migrationClass) {
82
                $output->writeln('Run: ' . $migrationClass);
83
                $this->migrationService->up($migrationClass);
84
            }
85
            $output->writeln('Done!');
86
            return null;
87
        }
88
89
        $output->writeln('Cancel');
90
        return null;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    protected function getNewMigrations(): array
97
    {
98
        $cubes = $this->cubeManager->getCubes();
99
        $migrations = [];
100
        foreach ($cubes as $cube) {
101
            foreach ($cube->getMigrations() as $k => $migration) {
102
                $migrations[] = [$k, $migration];
103
            }
104
        }
105
        \uasort($migrations, function (array $data1, array $data2) {
106
            return $data1[0] <=> $data2[0];
107
        });
108
109
        $newMigrations = [];
110
        foreach ($migrations as $migrationData) {
111
            $migrationClass = $migrationData[1];
112
            if (!$this->migrationService->isRegistered($migrationClass)) {
113
                $newMigrations[] = $migrationClass;
114
            }
115
        }
116
117
        return $newMigrations;
118
    }
119
}
120