DataCommand::createSeedRunner()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Commands;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Doctrine\DBAL\DBALException;
22
use Doctrine\DBAL\Exception\InvalidArgumentException;
23
use Limoncello\Application\Data\BaseMigrationRunner;
24
use Limoncello\Application\Data\FileMigrationRunner;
25
use Limoncello\Application\Data\FileSeedRunner;
26
use Limoncello\Application\Packages\Data\DataSettings;
27
use Limoncello\Contracts\Commands\CommandInterface;
28
use Limoncello\Contracts\Commands\IoInterface;
29
use Limoncello\Contracts\Settings\SettingsProviderInterface;
30
use Psr\Container\ContainerExceptionInterface;
31
use Psr\Container\ContainerInterface;
32
use Psr\Container\NotFoundExceptionInterface;
33
34
/**
35
 * @package Limoncello\Application
36
 */
37
class DataCommand implements CommandInterface
38
{
39
    /**
40
     * Command name.
41
     */
42
    const NAME = 'l:db';
43
44
    /** Argument name */
45
    const ARG_ACTION = 'action';
46
47
    /** Command action */
48
    const ACTION_MIGRATE = 'migrate';
49
50
    /** Command action */
51
    const ACTION_SEED = 'seed';
52
53
    /** Command action */
54
    const ACTION_ROLLBACK = 'rollback';
55
56
    /** Option name */
57
    const OPT_PATH = 'path';
58
59
    /**
60 1
     * @inheritdoc
61
     */
62 1
    public static function getName(): string
63
    {
64
        return static::NAME;
65
    }
66
67
    /**
68 1
     * @inheritdoc
69
     */
70 1
    public static function getDescription(): string
71
    {
72
        return 'Migrates and seeds application data.';
73
    }
74
75
    /**
76 1
     * @inheritdoc
77
     */
78 1
    public static function getHelp(): string
79
    {
80
        return 'This command migrates, seeds and resets application data.';
81
    }
82
83
    /**
84 1
     * @inheritdoc
85
     */
86 1
    public static function getArguments(): array
87 1
    {
88 1
        $migrate  = static::ACTION_MIGRATE;
89
        $seed     = static::ACTION_SEED;
90
        $rollback = static::ACTION_ROLLBACK;
91
92 1
        return [
93 1
            [
94 1
                static::ARGUMENT_NAME        => static::ARG_ACTION,
95
                static::ARGUMENT_DESCRIPTION => "Action such as `$migrate`, `$seed` or `$rollback` data.",
96
                static::ARGUMENT_MODE        => static::ARGUMENT_MODE__REQUIRED,
97
            ],
98
        ];
99
    }
100
101
    /**
102 1
     * @inheritdoc
103
     */
104
    public static function getOptions(): array
105
    {
106 1
        return [
107 1
            [
108
                static::OPTION_NAME        => static::OPT_PATH,
109 1
                static::OPTION_DESCRIPTION => 'Path to a list of migrations or seeds. ' .
110 1
                    'If not given a path from settings will be used.',
111
                static::OPTION_SHORTCUT    => 'i',
112
                static::OPTION_MODE        => static::OPTION_MODE__REQUIRED,
113
            ],
114
        ];
115
    }
116
117
    /**
118 1
     * @inheritdoc
119
     */
120 1
    public static function execute(ContainerInterface $container, IoInterface $inOut): void
121
    {
122
        (new static())->run($container, $inOut);
123
    }
124
125
    /**
126
     * @param ContainerInterface $container
127
     * @param IoInterface $inOut
128
     *
129
     * @return void
130
     *
131
     * @throws ContainerExceptionInterface
132
     * @throws NotFoundExceptionInterface
133
     * @throws InvalidArgumentException
134 4
     * @throws DBALException
135
     */
136 4
    protected function run(ContainerInterface $container, IoInterface $inOut): void
137 4
    {
138
        $arguments = $inOut->getArguments();
139
        $options   = $inOut->getOptions();
140 4
141 4
        /** @var SettingsProviderInterface $provider */
142
        $provider = $container->get(SettingsProviderInterface::class);
143 4
        $settings = $provider->get(DataSettings::class);
144 4
145
        $path   = $options[static::OPT_PATH] ?? false;
146 4
        $action = $arguments[static::ARG_ACTION];
147 1
        switch ($action) {
148 1 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...
149 1
                $path = $path !== false ? $path : $settings[DataSettings::KEY_MIGRATIONS_LIST_FILE] ?? '';
150 3
                $this->createMigrationRunner($inOut, $path)->migrate($container);
151 1
                break;
152 1 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...
153 1
                $path = $path !== false ? $path : $settings[DataSettings::KEY_MIGRATIONS_LIST_FILE] ?? '';
154 2
                $this->createMigrationRunner($inOut, $path)->rollback($container);
155 1
                break;
156 1
            case static::ACTION_SEED:
157 1
                $path     = $path !== false ? $path : $settings[DataSettings::KEY_SEEDS_LIST_FILE] ?? '';
158 1
                $seedInit = $settings[DataSettings::KEY_SEED_INIT] ?? null;
159
                $this->createSeedRunner($inOut, $path, $seedInit)->run($container);
160 1
                break;
161 1
            default:
162
                $inOut->writeError("Unsupported action `$action`." . PHP_EOL);
163
                break;
164
        }
165
    }
166
167
    /**
168
     * @param IoInterface $inOut
169
     * @param string      $path
170
     *
171 1
     * @return FileMigrationRunner
172
     */
173 1
    protected function createMigrationRunner(IoInterface $inOut, string $path): FileMigrationRunner
174
    {
175
        return new FileMigrationRunner($inOut, $path);
176
    }
177
178
    /**
179
     * @param IoInterface   $inOut
180
     * @param string        $seedsPath
181
     * @param callable|null $seedInit
182
     * @param string        $seedsTable
183
     *
184 1
     * @return FileSeedRunner
185
     */
186
    protected function createSeedRunner(
187
        IoInterface $inOut,
188
        string $seedsPath,
189
        callable $seedInit = null,
190 1
        string $seedsTable = BaseMigrationRunner::SEEDS_TABLE
191
    ): FileSeedRunner {
192
        return new FileSeedRunner($inOut, $seedsPath, $seedInit, $seedsTable);
193
    }
194
}
195