Test Failed
Push — develop ( 815500...0204ba )
by nguereza
02:34
created

MigrationExecuteCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
c 1
b 0
f 0
dl 0
loc 125
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 2
A execute() 0 34 3
A executeMigrationDown() 0 11 2
A executeMigrationUp() 0 30 2
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\Database\Schema;
52
use Platine\Filesystem\Filesystem;
53
use Platine\Framework\App\Application;
54
use Platine\Framework\Migration\MigrationRepository;
55
use RuntimeException;
56
57
/**
58
 * class MigrationExecuteCommand
59
 * @package Platine\Framework\Migration\Command
60
 */
61
class MigrationExecuteCommand extends AbstractCommand
62
{
63
64
    /**
65
     * Create new instance
66
     * {@inheritodc}
67
     */
68
    public function __construct(
69
        Application $app,
70
        MigrationRepository $repository,
71
        Schema $schema,
72
        Config $config,
73
        Filesystem $filesystem
74
    ) {
75
        parent::__construct($app, $repository, $schema, $config, $filesystem);
76
        $this->setName('migration:exec')
77
             ->setDescription('Upgrade the migration up/down for the given version');
78
79
        $this->addArgument('migration version', 'Version to execute', '', true);
80
        $this->addArgument('type', 'type of migration [up|down]', 'up', true, true, false, function ($val) {
81
            if (!in_array($val, ['up', 'down'])) {
82
                throw new RuntimeException(sprintf(
83
                    'Invalid argument type [%s], must be one of [up, down]',
84
                    $val
85
                ));
86
            }
87
88
             return $val;
89
        });
90
    }
91
92
    /**
93
     * {@inheritodc}
94
     */
95
    public function execute()
96
    {
97
        $version = $this->getArgumentValue('migrationVersion');
98
        $type = $this->getArgumentValue('type');
99
100
        $writer = $this->io()->writer();
101
        $writer->boldYellow('MIGRATION EXECUTION', true)->eol();
102
        $writer->bold('Version: ');
103
        $writer->boldBlueBgBlack($version, true);
104
        $writer->bold('Type: ');
105
        $writer->boldBlueBgBlack($type, true);
106
107
        $migrations = $this->getMigrations();
108
        if (!array_key_exists($version, $migrations)) {
109
            throw new RuntimeException(sprintf(
110
                'Invalid migration version [%s]',
111
                $version
112
            ));
113
        }
114
        $description = str_replace('_', ' ', $migrations[$version]);
115
116
        $writer->bold('Description: ');
117
        $writer->boldBlueBgBlack($description, true)->eol();
118
119
        if ($type === 'up') {
120
            $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

120
            $this->executeMigrationUp(/** @scrutinizer ignore-type */ $version, $description);
Loading history...
121
        } else {
122
            $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

122
            $this->executeMigrationDown(/** @scrutinizer ignore-type */ $version, $description);
Loading history...
123
        }
124
125
        $writer->boldGreen(sprintf(
126
            'Migration [%s] executed successfully',
127
            $description
128
        ))->eol();
129
    }
130
131
    /**
132
     * Execute migration up
133
     * @param string $version
134
     * @param string $description
135
     * @return void
136
     */
137
    public function executeMigrationUp(string $version, string $description): void
138
    {
139
        $exists = $this->repository->findBy([
140
            'version' => $version
141
        ]);
142
143
        if ($exists) {
144
            throw new RuntimeException(sprintf(
145
                'Migration with version [%s], already executed at [%s]',
146
                $version,
147
                $exists->created_at
148
            ));
149
        }
150
        $writer = $this->io()->writer();
151
        $writer->boldGreen(sprintf(
152
            '* Execute migration %s: %s',
153
            $version,
154
            $description,
155
        ))->eol();
156
157
        $migration = $this->createMigrationClass($description, $version);
158
        $migration->up();
159
160
        $entity = $this->repository->create([
161
            'version' => $version,
162
            'description' => $description,
163
            'created_at' => date('Y-m-d H:i:s'),
164
        ]);
165
166
        $this->repository->save($entity);
167
    }
168
169
    /**
170
     * Execute migration down
171
     * @param string $version
172
     * @param string $description
173
     * @return void
174
     */
175
    public function executeMigrationDown(string $version, string $description): void
176
    {
177
        $migration = $this->createMigrationClass($description, $version);
178
        $migration->down();
179
180
        $entity = $this->repository->findBy([
181
            'version' => $version
182
        ]);
183
184
        if ($entity) {
185
            $this->repository->delete($entity);
186
        }
187
    }
188
}
189