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

MigrationUpCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 7.92 %

Importance

Changes 0
Metric Value
wmc 13
dl 8
loc 101
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewMigrations() 0 18 4
C execute() 8 32 7
A configure() 0 5 1
A __construct() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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