Completed
Push — master ( 82ad84...aad44c )
by Maxim
02:32
created

MigrationUpCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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): ', false);
80 View Code Duplication
        if ($class || $helper->ask($input, $output, $question)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
            $migrations[] = $cube->getMigrations();
102
        }
103
        $allMigrations = \array_merge(...$migrations);
0 ignored issues
show
Bug introduced by
$migrations is expanded, but the parameter $array1 of array_merge() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        $allMigrations = \array_merge(/** @scrutinizer ignore-type */ ...$migrations);
Loading history...
104
        \ksort($allMigrations, \SORT_STRING & \SORT_ASC);
105
106
        $newMigrations = [];
107
        foreach ($allMigrations as $k => $migrationClass) {
108
            if (!$this->migrationService->isRegistered($migrationClass)) {
109
                $newMigrations[$k] = $migrationClass;
110
            }
111
        }
112
113
        return $newMigrations;
114
    }
115
}
116