Passed
Pull Request — master (#120)
by
unknown
03:02
created

MigrationMigrateCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 61
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B exec() 0 21 7
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum\Console\Commands;
16
17
use Quantum\Exceptions\MigrationException;
18
use Quantum\Migration\MigrationManager;
19
use Quantum\Console\QtCommand;
20
21
/**
22
 * Class MigrationMigrateCommand
23
 * @package Quantum\Console\Command
24
 */
25
class MigrationMigrateCommand extends QtCommand
26
{
27
28
    /**
29
     * The console command name.
30
     * @var string
31
     */
32
    protected $name = 'migration:migrate';
33
34
    /**
35
     * The console command description.
36
     * @var string
37
     */
38
    protected $description = 'Migrates the migrations';
39
40
    /**
41
     * Command arguments
42
     * @var string[][]
43
     */
44
    protected $args = [
45
        ['direction', 'optional', '[up] for upgrade, [down] for downgrade'],
46
    ];
47
48
    /**
49
     * Command options
50
     * @var string[][]
51
     */
52
    protected $options = [
53
        ['step', 's', 'optional', 'Number of migrations to apply'],
54
    ];
55
56
    /**
57
     * Executes the command
58
     * @throws \Quantum\Exceptions\AppException
59
     * @throws \Quantum\Exceptions\ConfigException
60
     * @throws \Quantum\Exceptions\DatabaseException
61
     * @throws \Quantum\Exceptions\DiException
62
     * @throws \Quantum\Exceptions\LangException
63
     * @throws \ReflectionException
64
     */
65
    public function exec()
66
    {
67
68
        $direction = $this->getArgument('direction') ?: MigrationManager::UPGRADE;
69
70
        if ($direction == 'down') {
71
            if (!$this->confirm("This operation will revert all the database changes, including the data. Continue?")) {
72
                $this->info('Operation was canceled!');
73
                return;
74
            }
75
        }
76
77
        $step = (int)$this->getOption('step') ?: null;
78
79
        $migrationManager = new MigrationManager();
80
81
        try {
82
            $migrated = $migrationManager->applyMigrations($direction, $step);
83
            $this->info($migrated . ' migration' . ($migrated > 1 ? 's were' : ' was') . ' applied');
84
        } catch (MigrationException $e) {
85
            $this->info($e->getMessage());
86
        }
87
    }
88
}
89