Completed
Push — master ( a52247...91a49a )
by Zach
02:12
created

Migrate::getSteps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Yarak\Commands;
4
5
use Yarak\Config\Config;
6
use Yarak\Migrations\Migrator;
7
use Yarak\DB\ConnectionResolver;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class Migrate extends YarakCommand
13
{
14
    /**
15
     * Configure the command.
16
     */
17
    protected function configure()
18
    {
19
        $this->setName('migrate')
20
         ->setDescription('Run the database migrations.')
21
         ->setHelp('This command allows you to run migrations.')
22
         ->addOption(
23
            'rollback',
24
            null,
25
            InputOption::VALUE_NONE,
26
            'Rollback migrations by given number of steps.'
27
        )
28
         ->addOption(
29
            'steps',
30
            null,
31
            InputOption::VALUE_OPTIONAL,
32
            'Number of steps to rollback.',
33
            1
34
        )
35
        ->addOption(
36
            'reset',
37
            null,
38
            InputOption::VALUE_NONE,
39
            'Rollback all migrations.'
40
        )
41
        ->addOption(
42
            'refresh',
43
            null,
44
            InputOption::VALUE_NONE,
45
            'Rollback and re-run all migrations.'
46
        );
47
    }
48
49
    /**
50
     * Execute the command.
51
     *
52
     * @param InputInterface  $input
53
     * @param OutputInterface $output
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $migrator = $this->getMigrator();
58
59
        if ($input->getOption('rollback')) {
60
            $migrator->rollback($input->getOption('steps'));
61
        } elseif ($input->getOption('reset')) {
62
            $migrator->reset();
63
        } elseif ($input->getOption('refresh')) {
64
            $migrator->refresh();
65
        } else {
66
            $migrator->run();
67
        }
68
69
        foreach ($migrator->getLog() as $message) {
70
            $output->writeln($message);
71
        }
72
    }
73
74
    /**
75
     * Get an instance of the migrator.
76
     *
77
     * @return Migrator
78
     */
79
    protected function getMigrator()
80
    {
81
        $config = Config::getInstance($this->configArray);
82
83
        return new Migrator(
84
            $config,
85
            new ConnectionResolver(),
86
            $this->getRepository($config)
87
        );
88
    }
89
90
    /**
91
     * Get an instance of MigrationRepository.
92
     *
93
     * @param Config $config
94
     *
95
     * @return Yarak\Migrations\MigrationRepository
96
     */
97
    protected function getRepository(Config $config)
98
    {
99
        $repositoryType = ucfirst($config->get(['yarak', 'migrationRepository']));
100
101
        $repositoryClass = 'Yarak\\Migrations\\'.$repositoryType.'MigrationRepository';
102
103
        return new $repositoryClass();
104
    }
105
}
106