Completed
Pull Request — master (#20)
by Pavel
02:51
created

MigrateDownCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 4
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 29 4
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Console\Traits\DbHelper;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * MigrateDownCommand
13
 */
14
class MigrateDownCommand extends Command
15
{
16
    use DbHelper;
17
18
    /**
19
     * Configuration of command
20
     */
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('migrate:down')
25
            ->setDescription('Command for rollback migration')
26
            ->addArgument('migration', InputArgument::REQUIRED, 'What kind of migration do you want to rollback?')
27
        ;
28
    }
29
30
    /**
31
     * Execute method of command
32
     *
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     *
36
     * @return int|null|void
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $migrationName = $input->getArgument('migration');
41
42
        $file = MIGRATIONS_PATH.'/'.$migrationName.'.php';
43
        if (false === file_exists($file)) {
44
            throw new \RunTimeException('This migration not found');
45
        }
46
47
        $fileNamePieces = explode('_', $migrationName);
48
49
        require_once($file);
50
51
        $class = '';
52
        foreach ($fileNamePieces as $key => $item) {
53
            if ($key == 0) {
54
                continue;
55
            }
56
            $class .= ucfirst($item);
57
        }
58
59
        $obj = new $class();
60
        $obj->down();
61
62
        $this->deleteRow($migrationName, $this->migrationsTable);
63
64
        $output->writeln([sprintf('<info>Rollback `%s` migration - done</info>', $migrationName)]);
65
        return;
66
    }
67
}
68