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

MigrationDownCommand::configure()   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 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 MigrationDownCommand extends Command
14
{
15
16
    /**
17
     * @var MigrationService
18
     */
19
    protected $migrationService;
20
21
    /**
22
     * @param MigrationService $migrationService
23
     *
24
     * @throws \Symfony\Component\Console\Exception\LogicException
25
     */
26
    public function __construct(MigrationService $migrationService)
27
    {
28
        parent::__construct();
29
        $this->migrationService = $migrationService;
30
    }
31
32
    /**
33
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
34
     */
35
    protected function configure()
36
    {
37
        $this->setName('migrate:down')
38
            ->setDescription('Rollback migration[s]')
39
            ->addArgument('class', InputArgument::OPTIONAL, 'Migration class');
40
    }
41
42
    /**
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     *
46
     * @return null|int
47
     * @throws \Symfony\Component\Console\Exception\LogicException
48
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
49
     * @throws \Psr\Container\NotFoundExceptionInterface
50
     * @throws \Psr\Container\ContainerExceptionInterface
51
     * @throws \Exception
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        if ($class = $input->getArgument('class')) {
56
            $rollbackMigrations = [$class];
57
        } else {
58
            $rollbackMigrations = $this->migrationService->getRegistered();
59
            $rollbackMigrations = \array_reverse($rollbackMigrations, true);
60
        }
61
62
        if (!$rollbackMigrations) {
63
            $output->writeln('Migrations not found');
64
            return null;
65
        }
66
67
        $message = "Rollback migrations: \n";
68
        foreach ($rollbackMigrations as $migrationClass) {
69
            $message .= $migrationClass . "\n";
70
        }
71
        $output->writeln($message . "\n");
72
73
        $helper = $this->getHelper('question');
74
        $question = new ConfirmationQuestion('Continue? (Y/N): ', false);
75 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...
76
            foreach ($rollbackMigrations as $migrationClass) {
77
                $output->writeln('Rollback: ' . $migrationClass);
78
                $this->migrationService->down($migrationClass);
79
            }
80
            $output->writeln('Done!');
81
            return null;
82
        }
83
84
        $output->writeln('Cancel');
85
        return null;
86
    }
87
}
88