RollbackCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 125
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 6 1
A fire() 0 16 3
A rollbackMigration() 0 10 2
A hardRollbackMigration() 0 6 1
A markRolledBackWithConfirmation() 0 11 2
A deleteIfNeeded() 0 10 3
1
<?php
2
3
namespace Arrilot\BitrixMigrations\Commands;
4
5
use Arrilot\BitrixMigrations\Migrator;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Question\ConfirmationQuestion;
8
9
class RollbackCommand extends AbstractCommand
10
{
11
    /**
12
     * Migrator instance.
13
     *
14
     * @var Migrator
15
     */
16
    protected $migrator;
17
18
    protected static $defaultName = 'rollback';
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param Migrator    $migrator
24
     * @param string|null $name
25
     */
26
    public function __construct(Migrator $migrator, $name = null)
27
    {
28
        $this->migrator = $migrator;
29
30
        parent::__construct($name);
31
    }
32
33
    /**
34
     * Configures the current command.
35
     */
36
    protected function configure()
37
    {
38
        $this->setDescription('Rollback the last migration')
39
            ->addOption('hard', null, InputOption::VALUE_NONE, 'Rollback without running down()')
40
            ->addOption('delete', null, InputOption::VALUE_NONE, 'Delete migration file after rolling back');
41
    }
42
43
    /**
44
     * Execute the console command.
45
     *
46
     * @return null|int
47
     */
48
    protected function fire()
49
    {
50
        $ran = $this->migrator->getRanMigrations();
51
52
        if (empty($ran)) {
53
            return $this->info('Nothing to rollback');
54
        }
55
56
        $migration = $ran[count($ran) - 1];
57
58
        $this->input->getOption('hard')
59
            ? $this->hardRollbackMigration($migration)
60
            : $this->rollbackMigration($migration);
61
62
        return $this->deleteIfNeeded($migration);
63
    }
64
65
    /**
66
     * Call rollback.
67
     *
68
     * @param $migration
69
     *
70
     * @return null
71
     */
72
    protected function rollbackMigration($migration)
73
    {
74
        if ($this->migrator->doesMigrationFileExist($migration)) {
75
            $this->migrator->rollbackMigration($migration);
76
        } else {
77
            $this->markRolledBackWithConfirmation($migration);
78
        }
79
80
        $this->message("<info>Rolled back:</info> {$migration}.php");
81
    }
82
83
    /**
84
     * Call hard rollback.
85
     *
86
     * @param $migration
87
     *
88
     * @return null
89
     */
90
    protected function hardRollbackMigration($migration)
91
    {
92
        $this->migrator->removeSuccessfulMigrationFromLog($migration);
93
94
        $this->message("<info>Rolled back with --hard:</info> {$migration}.php");
95
    }
96
97
    /**
98
     * Ask a user to confirm rolling back non-existing migration and remove it from log.
99
     *
100
     * @param $migration
101
     *
102
     * @return void
103
     */
104
    protected function markRolledBackWithConfirmation($migration)
105
    {
106
        $helper = $this->getHelper('question');
107
        $question = new ConfirmationQuestion("<error>Migration $migration was not found.\r\nDo you want to mark it as rolled back? (y/n)</error>\r\n", false);
108
109
        if (!$helper->ask($this->input, $this->output, $question)) {
110
            $this->abort();
111
        }
112
113
        $this->migrator->removeSuccessfulMigrationFromLog($migration);
114
    }
115
116
    /**
117
     * Delete migration file if options is set
118
     *
119
     * @param string $migration
120
     *
121
     * @return null
122
     */
123
    protected function deleteIfNeeded($migration)
124
    {
125
        if (!$this->input->getOption('delete')) {
126
            return;
127
        }
128
129
        if ($this->migrator->deleteMigrationFile($migration)) {
130
            $this->message("<info>Deleted:</info> {$migration}.php");
131
        }
132
    }
133
}
134