Test Failed
Push — develop ( 12e168...c96bee )
by nguereza
02:42
created

MigrationExecuteCommand::execute()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 37
c 1
b 0
f 0
nc 14
nop 0
dl 0
loc 52
rs 7.7724

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file MigrationExecuteCommand.php
34
 *
35
 *  The migration execute command class
36
 *
37
 *  @package    Platine\Framework\Migration\Command
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Migration\Command;
49
50
use Platine\Config\Config;
51
use Platine\Filesystem\Filesystem;
52
use Platine\Framework\App\Application;
53
use Platine\Framework\Migration\MigrationRepository;
54
use RuntimeException;
55
56
/**
57
 * class MigrationExecuteCommand
58
 * @package Platine\Framework\Migration\Command
59
 */
60
class MigrationExecuteCommand extends AbstractCommand
61
{
62
63
    /**
64
     * Create new instance
65
     * {@inheritodc}
66
     */
67
    public function __construct(
68
        Application $app,
69
        MigrationRepository $repository,
70
        Config $config,
71
        Filesystem $filesystem
72
    ) {
73
        parent::__construct($app, $repository, $config, $filesystem);
74
        $this->setName('migration:exec')
75
             ->setDescription('Execute the migration up/down for one version');
76
77
        $this->addArgument('type', 'type of migration [up|down]', 'up', true, true, false, function ($val) {
78
            if (!in_array($val, ['up', 'down'])) {
79
                throw new RuntimeException(sprintf(
80
                    'Invalid argument type [%s], must be one of [up, down]',
81
                    $val
82
                ));
83
            }
84
85
             return $val;
86
        });
87
88
        $this->addOption('-i|--id', 'the migration version', null, false, true);
89
    }
90
91
    /**
92
     * {@inheritodc}
93
     */
94
    public function execute()
95
    {
96
        $type = $this->getArgumentValue('type');
97
98
        $io = $this->io();
99
        $writer = $io->writer();
100
        $writer->boldYellow('MIGRATION EXECUTION', true)->eol();
101
102
        $migrations = $this->getMigrations();
103
        $executed = $this->getExecuted('DESC');
104
105
        $version = $this->getOptionValue('id');
106
107
        if ($type === 'up') {
108
            $diff = array_diff_key($migrations, $executed);
109
            if (empty($diff)) {
110
                $writer->boldGreen('Migration already up to date');
111
            } else {
112
                if (empty($version)) {
113
                    $version = $io->choice('Choose which version to migrate up', $diff);
114
                }
115
116
                if (!isset($diff[$version])) {
117
                    $writer->boldRed(sprintf(
118
                        'Invalid migration version [%s] or already executed',
119
                        $version
120
                    ));
121
                } else {
122
                    $description = str_replace('_', ' ', $migrations[$version]);
123
                    $this->executeMigrationUp($version, $description);
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type null; however, parameter $version of Platine\Framework\Migrat...d::executeMigrationUp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
                    $this->executeMigrationUp(/** @scrutinizer ignore-type */ $version, $description);
Loading history...
124
                }
125
            }
126
        } else {
127
            if (empty($executed)) {
128
                $writer->boldGreen('No migration to rollback');
129
            } else {
130
                $data = [];
131
                foreach ($executed as $ver => $entity) {
132
                    $data[$ver] = $entity->description;
133
                }
134
                if (empty($version)) {
135
                    $version = $io->choice('Choose which version to rollback', $data);
136
                }
137
138
                if (!isset($data[$version])) {
139
                    $writer->boldRed(sprintf(
140
                        'Invalid migration version [%s] or not yet executed',
141
                        $version
142
                    ));
143
                } else {
144
                    $description = str_replace('_', ' ', $data[$version]);
145
                    $this->executeMigrationDown($version, $description);
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type null; however, parameter $version of Platine\Framework\Migrat...:executeMigrationDown() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
                    $this->executeMigrationDown(/** @scrutinizer ignore-type */ $version, $description);
Loading history...
146
                }
147
            }
148
        }
149
    }
150
151
    /**
152
     * Execute migration up
153
     * @param string $version
154
     * @param string $description
155
     * @return void
156
     */
157
    public function executeMigrationUp(string $version, string $description): void
158
    {
159
        $writer = $this->io()->writer();
160
        $writer->boldGreen(sprintf(
161
            '* Execute migration up for %s: %s',
162
            $version,
163
            $description,
164
        ))->eol();
165
166
        $migration = $this->createMigrationClass($description, $version);
167
        $migration->up();
168
169
        $entity = $this->repository->create([
170
            'version' => $version,
171
            'description' => $description,
172
            'created_at' => date('Y-m-d H:i:s'),
173
        ]);
174
175
        $this->repository->save($entity);
176
    }
177
178
    /**
179
     * Execute migration down
180
     * @param string $version
181
     * @param string $description
182
     * @return void
183
     */
184
    public function executeMigrationDown(string $version, string $description): void
185
    {
186
        $writer = $this->io()->writer();
187
        $writer->boldGreen(sprintf(
188
            '* Execute migration down for %s: %s',
189
            $version,
190
            $description,
191
        ))->eol();
192
193
        $migration = $this->createMigrationClass($description, $version);
194
        $migration->down();
195
196
        $entity = $this->repository->findBy([
197
            'version' => $version
198
        ]);
199
200
        if ($entity) {
201
            $this->repository->delete($entity);
202
        }
203
    }
204
}
205