Completed
Push — master ( 2d0949...d58be5 )
by Neomerx
08:30
created

DataCommand::createMigrationRunner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Limoncello\Application\Commands;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Application\Data\BaseMigrationRunner;
20
use Limoncello\Application\Data\FileMigrationRunner;
21
use Limoncello\Application\Data\FileSeedRunner;
22
use Limoncello\Application\Packages\Data\DataSettings;
23
use Limoncello\Contracts\Commands\CommandInterface;
24
use Limoncello\Contracts\Commands\IoInterface;
25
use Limoncello\Contracts\Settings\SettingsProviderInterface;
26
use Psr\Container\ContainerInterface;
27
28
/**
29
 * @package Limoncello\Application
30
 */
31
class DataCommand implements CommandInterface
32
{
33
    /**
34
     * Command name.
35
     */
36
    const NAME = 'l:db';
37
38
    /** Argument name */
39
    const ARG_ACTION = 'action';
40
41
    /** Command action */
42
    const ACTION_MIGRATE = 'migrate';
43
44
    /** Command action */
45
    const ACTION_SEED = 'seed';
46
47
    /** Command action */
48
    const ACTION_ROLLBACK = 'rollback';
49
50
    /** Option name */
51
    const OPT_PATH = 'path';
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public static function getName(): string
57
    {
58
        return static::NAME;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public static function getDescription(): string
65
    {
66
        return 'Migrates and seeds application data.';
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public static function getHelp(): string
73
    {
74
        return 'This command migrates, seeds and resets application data.';
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public static function getArguments(): array
81
    {
82
        $migrate  = static::ACTION_MIGRATE;
83
        $seed     = static::ACTION_SEED;
84
        $rollback = static::ACTION_ROLLBACK;
85
86
        return [
87
            [
88
                static::ARGUMENT_NAME        => static::ARG_ACTION,
89
                static::ARGUMENT_DESCRIPTION => "Action such as `$migrate`, `$seed` or `$rollback` data.",
90
                static::ARGUMENT_MODE        => static::ARGUMENT_MODE__REQUIRED,
91
            ],
92
        ];
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public static function getOptions(): array
99
    {
100
        return [
101
            [
102
                static::OPTION_NAME        => static::OPT_PATH,
103
                static::OPTION_DESCRIPTION => 'Path to a list of migrations or seeds. ' .
104
                    'If not given a path from settings will be used.',
105
                static::OPTION_SHORTCUT    => 'i',
106
                static::OPTION_MODE        => static::OPTION_MODE__REQUIRED,
107
            ],
108
        ];
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public static function execute(ContainerInterface $container, IoInterface $inOut)
115
    {
116
        (new static())->run($container, $inOut);
117
    }
118
119
    /**
120
     * @param ContainerInterface $container
121
     * @param IoInterface        $inOut
122
     *
123
     * @return void
124
     */
125
    protected function run(ContainerInterface $container, IoInterface $inOut)
126
    {
127
        $arguments = $inOut->getArguments();
128
        $options   = $inOut->getOptions();
129
130
        /** @var SettingsProviderInterface $provider */
131
        $provider = $container->get(SettingsProviderInterface::class);
132
        $settings = $provider->get(DataSettings::class);
133
134
        $path   = $options[static::OPT_PATH] ?? false;
135
        $action = $arguments[static::ARG_ACTION];
136
        switch ($action) {
137 View Code Duplication
            case static::ACTION_MIGRATE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
138
                $path = $path !== false ? $path : $settings[DataSettings::KEY_MIGRATIONS_PATH] ?? '';
139
                $this->createMigrationRunner($path)->migrate($container);
140
                break;
141 View Code Duplication
            case static::ACTION_ROLLBACK:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
142
                $path = $path !== false ? $path : $settings[DataSettings::KEY_MIGRATIONS_PATH] ?? '';
143
                $this->createMigrationRunner($path)->rollback($container);
144
                break;
145
            case static::ACTION_SEED:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
146
                $path     = $path !== false ? $path : $settings[DataSettings::KEY_SEEDS_PATH] ?? '';
147
                $seedInit = $settings[DataSettings::KEY_SEED_INIT] ?? null;
148
                $this->createSeedRunner($path, $seedInit)->run($container);
149
                break;
150
            default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
151
                $inOut->writeError("Unsupported action `$action`." . PHP_EOL);
152
                break;
153
        }
154
    }
155
156
    /**
157
     * @param string $path
158
     *
159
     * @return FileMigrationRunner
160
     */
161
    protected function createMigrationRunner(string $path): FileMigrationRunner
162
    {
163
        return new FileMigrationRunner($path);
164
    }
165
166
    /**
167
     * @param string        $seedsPath
168
     * @param callable|null $seedInit
169
     * @param string        $seedsTable
170
     *
171
     * @return FileSeedRunner
172
     */
173
    protected function createSeedRunner(
174
        string $seedsPath,
175
        callable $seedInit = null,
176
        string $seedsTable = BaseMigrationRunner::SEEDS_TABLE
177
    ): FileSeedRunner {
178
        return new FileSeedRunner($seedsPath, $seedInit, $seedsTable);
179
    }
180
}
181