MigrateCommand::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Schnittstabil\Dartisan\Commands;
4
5
use Garden\Cli\Cli;
6
use Garden\Cli\Args;
7
use Schnittstabil\Dartisan\Migrator;
8
use Schnittstabil\Dartisan\OutputInterface;
9
10
class MigrateCommand extends Command
11
{
12
    use DatabaseAwareCommandTrait;
13
    use MigrationAwareCommandTrait;
14
15
    public static $name = 'migrate';
16
17
    /**
18
     * @var Migrator
19
     */
20
    protected $migrator;
21
22
    /**
23
     * @var string
24
     */
25
    protected $defaultPath;
26
27
    public function __construct(
28
        Args $args,
29
        OutputInterface $output,
30
        Migrator $migrator,
31
        string $defaultPath
32
    ) {
33
        parent::__construct($args, $output);
34
        $this->migrator = $migrator;
35
        $this->defaultPath = $defaultPath;
36
    }
37
38
    public function run()
39
    {
40
        $path = $this->args->getOpt('path', $this->defaultPath);
41
        $this->migrator->run($path, [
42
            'pretend' => $this->args->getOpt('pretend', null),
43
            'step' => $this->args->getOpt('step', null),
44
        ]);
45
46
        return 0;
47
    }
48
49
    public static function register(Cli $cli)
50
    {
51
        $cli = static::registerDatabaseOpts($cli);
52
        $cli = static::registerMigrationOpts($cli);
53
54
        return $cli
55
            ->command(static::$name)
56
            ->description('Run the database migrations.')
57
            ->opt('path', 'The path of migrations files to be executed.')
58
            ->opt('pretend', 'Dump the SQL queries that would be run.')
59
            ->opt('step', 'Force the migrations to be run so they can be rolled back individually.');
60
    }
61
}
62