Completed
Push — dev ( 348067...b33ea3 )
by Zach
02:21
created

Migrate::execute()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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