Completed
Push — dev ( 6500dc...8bb13e )
by Zach
02:12
created

Migrate::addSeed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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 Yarak\DB\Seeders\SeedRunner;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class Migrate extends YarakCommand
14
{
15
    /**
16
     * Configure the command.
17
     */
18
    protected function configure()
19
    {
20
        $this->setName('migrate')
21
            ->setDescription('Run the database migrations.')
22
            ->setHelp('This command allows you to run migrations.')
23
            ->addRollback()
24
            ->addSteps()
25
            ->addReset()
26
            ->addRefresh()
27
            ->addSeed()
28
            ->addSeedClass();
29
    }
30
31
    /**
32
     * Add rollback option.
33
     */
34
    protected function addRollback()
35
    {
36
        return $this->addOption(
37
            'rollback',
38
            null,
39
            InputOption::VALUE_NONE,
40
            'Rollback migrations by given number of steps.'
41
        );
42
    }
43
44
    /**
45
     * Add steps option.
46
     */
47
    protected function addSteps()
48
    {
49
        return $this->addOption(
50
            'steps',
51
            null,
52
            InputOption::VALUE_OPTIONAL,
53
            'Number of steps to rollback.',
54
            1
55
        );
56
    }
57
58
    /**
59
     * Add reset option.
60
     */
61
    protected function addReset()
62
    {
63
        return $this->addOption(
64
            'reset',
65
            null,
66
            InputOption::VALUE_NONE,
67
            'Rollback all migrations.'
68
        );
69
    }
70
71
    /**
72
     * Add refresh option.
73
     */
74
    protected function addRefresh()
75
    {
76
        return $this->addOption(
77
            'refresh',
78
            null,
79
            InputOption::VALUE_NONE,
80
            'Rollback and re-run all migrations.'
81
        );
82
    }
83
84
    /**
85
     * Add seed option.
86
     */
87
    protected function addSeed()
88
    {
89
        return $this->addOption(
90
            'seed',
91
            null,
92
            InputOption::VALUE_NONE,
93
            'Seed the database after refreshing.'
94
        );
95
    }
96
97
    protected function addSeedClass()
98
    {
99
        return $this->addOption(
100
            'class',
101
            null,
102
            InputOption::VALUE_OPTIONAL,
103
            'The name of the seeder class to run.',
104
            'DatabaseSeeder'
105
        );
106
    }
107
108
    /**
109
     * Execute the command.
110
     *
111
     * @param InputInterface  $input
112
     * @param OutputInterface $output
113
     */
114
    protected function execute(InputInterface $input, OutputInterface $output)
115
    {
116
        $migrator = $this->getMigrator();
117
118
        if ($input->getOption('rollback')) {
119
            $migrator->rollback($input->getOption('steps'));
120
        } elseif ($input->getOption('reset')) {
121
            $migrator->reset();
122
        } elseif ($input->getOption('refresh')) {
123
            $this->preformRefresh($migrator, $input);
124
        } else {
125
            $migrator->run();
126
        }
127
128
        foreach ($migrator->getLog() as $message) {
129
            $output->writeln($message);
130
        }
131
    }
132
133
    /**
134
     * Get an instance of the migrator.
135
     *
136
     * @return Migrator
137
     */
138
    protected function getMigrator()
139
    {
140
        $config = Config::getInstance($this->configArray);
141
142
        $migratorClassName = $this->getMigratorClassName($config);
143
144
        return new $migratorClassName(
145
            $config,
146
            new ConnectionResolver(),
147
            $this->getRepository($config)
148
        );
149
    }
150
151
    /**
152
     * Get the name of the migrator class.
153
     *
154
     * @param Config $config
155
     *
156
     * @return string
157
     */
158
    protected function getMigratorClassName(Config $config)
159
    {
160
        $migratorType = ucfirst($config->get('migratorType'));
161
162
        return "Yarak\\Migrations\\$migratorType\\".
163
            $migratorType.'Migrator';
164
    }
165
166
    /**
167
     * Get an instance of MigrationRepository.
168
     *
169
     * @param Config $config
170
     *
171
     * @return Yarak\Migrations\MigrationRepository
172
     */
173
    protected function getRepository(Config $config)
174
    {
175
        $repositoryType = ucfirst($config->get('migrationRepository'));
176
177
        $repositoryClass = 'Yarak\\Migrations\\Repositories\\'.
178
            $repositoryType.'MigrationRepository';
179
180
        return new $repositoryClass();
181
    }
182
183
    /**
184
     * Perform the database refresh.
185
     *
186
     * @param  Migrator       $migrator
187
     * @param  InputInterface $input
188
     */
189
    protected function preformRefresh(Migrator $migrator, InputInterface $input)
190
    {
191
        $migrator->refresh();
192
193
        if ($input->getOption('seed')) {
194
            $seedRunner = new SeedRunner();
195
196
            $seedRunner->run($input->getOption('class'));
197
        }
198
199
        foreach ($seedRunner->getLog() as $message) {
0 ignored issues
show
Bug introduced by
The variable $seedRunner does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
200
            $output->writeln($message);
0 ignored issues
show
Bug introduced by
The variable $output does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
201
        }
202
    }
203
}
204