MigrateDownCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 34 5
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 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
        if (!$this->isRowExist($migrationName, $this->migrationsTable)) {
48
            throw new \RunTimeException('This migration not applied');
49
        }
50
51
        $fileNamePieces = explode('_', $migrationName);
52
53
        require_once($file);
54
55
        $class = '';
56
        foreach ($fileNamePieces as $key => $item) {
57
            if ($key == 0) {
58
                continue;
59
            }
60
            $class .= ucfirst($item);
61
        }
62
63
        $obj = new $class();
64
        $obj->down();
65
66
        $this->deleteRow($migrationName, $this->migrationsTable);
67
68
        $output->writeln([sprintf('<info>Rollback `%s` migration - done</info>', $migrationName)]);
69
70
        return;
71
    }
72
}
73