Passed
Push — develop ( 7a6395...0c2b91 )
by nguereza
03:02
created

SeedCreateDbCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 82
c 1
b 0
f 0
dl 0
loc 176
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplateClass() 0 3 1
A __construct() 0 16 1
A execute() 0 39 3
A generateSeedFromTableData() 0 20 2
A interact() 0 10 2
A generateClass() 0 9 1
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 SeedCreateDbCommand.php
34
 *
35
 *  The seed generation using an existing database data command class
36
 *
37
 *  @package    Platine\Framework\Migration\Seed\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\Seed\Command;
49
50
use Platine\Config\Config;
51
use Platine\Console\Input\Reader;
52
use Platine\Console\Output\Writer;
53
use Platine\Database\QueryBuilder;
54
use Platine\Database\Schema;
55
use Platine\Filesystem\Filesystem;
56
use Platine\Framework\App\Application;
57
use Platine\Framework\Migration\Seed\Command\AbstractSeedCommand;
58
use Platine\Stdlib\Helper\Str;
59
60
/**
61
 * @class SeedCreateDbCommand
62
 * @package Platine\Framework\Migration\Seed\Command
63
 * @template T
64
 * @extends AbstractSeedCommand<T>
65
 */
66
class SeedCreateDbCommand extends AbstractSeedCommand
67
{
68
69
    /**
70
     * The seed name
71
     * @var string
72
     */
73
    protected string $name = '';
74
75
    /**
76
     * The table name
77
     * @var string
78
     */
79
    protected string $table = '';
80
81
    /**
82
     * The schema to use
83
     * @var Schema
84
     */
85
    protected Schema $schema;
86
87
    /**
88
     * The instance of query builder
89
     * @var QueryBuilder
90
     */
91
    protected QueryBuilder $queryBuilder;
92
93
    /**
94
     * Create new instance
95
     * {@inheritodc}
96
     */
97
    public function __construct(
98
        Application $app,
99
        Config $config,
100
        Filesystem $filesystem,
101
        Schema $schema,
102
        QueryBuilder $queryBuilder
103
    ) {
104
        parent::__construct($app, $config, $filesystem);
105
        $this->schema = $schema;
106
        $this->queryBuilder = $queryBuilder;
107
108
        $this->setName('seed:createdb')
109
             ->setDescription('Create a new seed using existing data');
110
111
        $this->addArgument('table', 'name of the table', null, true, false);
112
        $this->addArgument('name', 'name of seed', null, false, true);
113
    }
114
115
    /**
116
     * {@inheritodc}
117
     */
118
    public function execute()
119
    {
120
        $writer = $this->io()->writer();
121
122
        if (!$this->schema->hasTable($this->table, true)) {
123
            $writer->boldRed(sprintf(
124
                'Database table [%s] does not exist',
125
                $this->table
126
            ));
127
            return;
128
        }
129
130
        $this->table = $this->getArgumentValue('table');
131
        $className = $this->getSeedClassName($this->name);
132
        $filename = $this->getFilenameFromClass($className);
133
        $fullPath = $this->seedPath . $filename;
134
135
        $writer->boldGreen('Seed detail: ')->eol();
136
        $writer->bold('Name: ');
137
        $writer->boldBlueBgBlack($this->name, true);
138
        $writer->bold('Table : ');
139
        $writer->boldBlueBgBlack($this->table, true);
140
        $writer->bold('Class name: ');
141
        $writer->boldBlueBgBlack($className, true);
142
        $writer->bold('Filename: ');
143
        $writer->boldBlueBgBlack($filename, true);
144
        $writer->bold('Path: ');
145
        $writer->boldBlueBgBlack($fullPath, true)->eol();
146
147
148
        $io = $this->io();
149
150
        if ($io->confirm('Are you confirm the generation of new seed?', 'n')) {
151
            $this->checkSeedPath();
152
            $this->generateClass($fullPath, $className);
153
            $writer->boldGreen(sprintf(
154
                'Seed [%s] generated successfully',
155
                $this->name
156
            ))->eol();
157
        }
158
    }
159
160
    /**
161
     * {@inheritodc}
162
     */
163
    public function interact(Reader $reader, Writer $writer): void
164
    {
165
        $writer->boldYellow('SEED GENERATION USING EXISTING DATA', true)->eol();
166
167
        $name = $this->getArgumentValue('name');
168
        if (!$name) {
169
            $io = $this->io();
170
            $name = $io->prompt('Enter the name of the seed', 'Seed description');
171
        }
172
        $this->name = $name;
173
    }
174
175
    /**
176
     * Generate the migration class
177
     * @param string $path
178
     * @param string $className
179
     * @return void
180
     */
181
    protected function generateClass(string $path, string $className): void
182
    {
183
        $template = $this->getTemplateClass();
184
        $seedDefinition = $this->generateSeedFromTableData();
185
        $content = Str::replaceFirst('%classname%', $className, $template);
186
        $seedContent = Str::replaceFirst('%seed_content%', $seedDefinition, $content);
187
188
        $file = $this->filesystem->file($path);
189
        $file->write($seedContent);
190
    }
191
192
    /**
193
     * Return the seed template class
194
     * @return string
195
     */
196
    private function getTemplateClass(): string
197
    {
198
        return <<<EOF
199
        <?php
200
        declare(strict_types=1);
201
        
202
        namespace Platine\Framework\Migration\Seed;
203
204
        use Platine\Framework\Migration\Seed\AbstractSeed;
205
206
        class %classname% extends AbstractSeed
207
        {
208
209
            public function run(): void
210
            {
211
              //Action when run the seed
212
              %seed_content%
213
            }
214
        }
215
        EOF;
216
    }
217
218
    /**
219
     * Generate the seed content using the current table data
220
     * @return string
221
     */
222
    private function generateSeedFromTableData(): string
223
    {
224
        $content = '';
225
        $data = $this->queryBuilder->from($this->table)
226
                    ->select()
227
                    ->fetchAssoc()
228
                    ->all();
229
230
        if (empty($data)) {
231
            return sprintf('// No data found on table "%s"', $this->table);
232
        }
233
234
        $content .= '
235
        $data = ' . var_export($data, true) . ';
236
        foreach ($data as $row) {
237
            $this->insert($row)->into(' . $this->table . ');
238
        }
239
        ';
240
241
        return $content;
242
    }
243
}
244