Test Failed
Push — develop ( 0ede07...29e3d8 )
by nguereza
02:43
created

SeedCreateCommand::interact()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
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 SeedCreateCommand.php
34
 *
35
 *  The seed generation 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\Filesystem\Filesystem;
54
use Platine\Framework\App\Application;
55
use Platine\Framework\Migration\Seed\Command\AbstractSeedCommand;
56
use Platine\Stdlib\Helper\Str;
57
58
/**
59
 * @class SeedCreateCommand
60
 * @package Platine\Framework\Migration\Seed\Command
61
 * @template T
62
 * @extends AbstractSeedCommand<T>
63
 */
64
class SeedCreateCommand extends AbstractSeedCommand
65
{
66
67
    /**
68
     * The seed name
69
     * @var string
70
     */
71
    protected string $name = '';
72
73
    /**
74
     * Create new instance
75
     * {@inheritodc}
76
     */
77
    public function __construct(
78
        Application $app,
79
        Config $config,
80
        Filesystem $filesystem
81
    ) {
82
        parent::__construct($app, $config, $filesystem);
83
        $this->setName('seed:create')
84
             ->setDescription('Create a new seed');
85
86
        $this->addArgument('name', 'name of seed', null, false, true);
87
    }
88
89
    /**
90
     * {@inheritodc}
91
     */
92
    public function execute()
93
    {
94
        $writer = $this->io()->writer();
95
96
        $className = $this->getSeedClassName($this->name);
97
        $filename = $this->getFilenameFromClass($className);
98
        $fullPath = $this->seedPath . $filename;
99
100
        $writer->boldGreen('Seed detail: ')->eol();
101
        $writer->bold('Name: ');
102
        $writer->boldBlueBgBlack($this->name, true);
103
        $writer->bold('Class name: ');
104
        $writer->boldBlueBgBlack($className, true);
105
        $writer->bold('Filename: ');
106
        $writer->boldBlueBgBlack($filename, true);
107
        $writer->bold('Path: ');
108
        $writer->boldBlueBgBlack($fullPath, true)->eol();
109
110
111
        $io = $this->io();
112
113
        if ($io->confirm('Are you confirm the generation of new seed?', 'n')) {
114
            $this->checkSeedPath();
115
            $this->generateClass($fullPath, $className);
116
            $writer->boldGreen(sprintf(
117
                'Seed [%s] generated successfully',
118
                $this->name
119
            ))->eol();
120
        }
121
    }
122
123
    /**
124
     * {@inheritodc}
125
     */
126
    public function interact(Reader $reader, Writer $writer): void
127
    {
128
        $writer->boldYellow('SEED GENERATION', true)->eol();
129
130
        $name = $this->getArgumentValue('name');
131
        if (!$name) {
132
            $io = $this->io();
133
            $name = $io->prompt('Enter the name of the seed', 'Seed description');
134
        }
135
        $this->name = $name;
136
    }
137
138
    /**
139
     * Generate the migration class
140
     * @param string $path
141
     * @param string $className
142
     * @return void
143
     */
144
    protected function generateClass(string $path, string $className): void
145
    {
146
        $template = $this->getTemplateClass();
147
        $content = Str::replaceFirst('%classname%', $className, $template);
148
149
        $file = $this->filesystem->file($path);
150
        $file->write($content);
151
    }
152
153
    /**
154
     * Return the seed template class
155
     * @return string
156
     */
157
    private function getTemplateClass(): string
158
    {
159
        return <<<EOF
160
        <?php
161
        namespace Platine\Framework\Migration\Seed;
162
163
        use Platine\Framework\Migration\Seed\AbstractSeed;
164
165
        class %classname% extends AbstractSeed
166
        {
167
168
            public function run(): void
169
            {
170
              //Action when run the seed
171
172
            }
173
        }
174
        EOF;
175
    }
176
}
177