Passed
Pull Request — main (#27)
by Dimitri
05:06
created

Command::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cli\Commands\Generators;
13
14
use BlitzPHP\Cli\Console\Command as ConsoleCommand;
15
use BlitzPHP\Cli\Traits\GeneratorTrait;
16
17
/**
18
 * Generates a skeleton command file.
19
 */
20
class Command extends ConsoleCommand
21
{
22
    use GeneratorTrait;
23
24
    /**
25
     * @var string Groupe
26
     */
27
    protected $group = 'Generateurs';
28
29
    /**
30
     * @var string Nom
31
     */
32
    protected $name = 'make:command';
33
34
    /**
35
     * @var string Description
36
     */
37
    protected $description = 'Génère une nouvelle commande klinge.';
38
39
    /**
40
     * @var string
41
     */
42
    protected $service = 'Service de génération de code';
43
44
    /**
45
     * @var array Arguments */
46
    protected $arguments = [
47
        'name' => 'Le nom de la classe de commande.',
48
    ];
49
50
    /**
51
     * @var array Options
52
     */
53
    protected $options = [
54
        '--command'   => 'Le nom de la commande. Par défaut: "command:name"',
55
        '--type'      => ['Le type de commande. Options [basic, generator]. Par défault: "basic".', 'basic'],
56
        '--group'     => 'Le groupe de la commande. Par défaut: [basic -> "{APP_NAME}", generator -> "{APP_NAME}:Generateurs"].',
57
        '--namespace' => ['Définissez l\'espace de noms racine. Par défaut: "APP_NAMESPACE".', APP_NAMESPACE],
58
        '--suffix'    => 'Ajouter le titre du composant au nom de la classe (par exemple, User => UserCommand).',
59
        '--force'     => "Forcer l'écrasement du fichier existant.",
60
    ];
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function execute(array $params)
66
    {
67 2
        $this->component = 'Command';
68 2
        $this->directory = 'Commands';
69 2
        $this->template  = 'command.tpl.php';
70
71 2
        $this->classNameLang = 'CLI.generator.className.command';
72 2
        $this->generateClass($params);
73
    }
74
75
    /**
76
     * Préparez les options et effectuez les remplacements nécessaires.
77
     */
78
    protected function prepare(string $class): string
79
    {
80 2
        $command = $this->option('command');
81 2
        $group   = $this->option('group');
82 2
        $type    = $this->option('type');
83
84 2
        $command = is_string($command) ? $command : 'command:name';
85 2
        $type    = is_string($type) ? $type : 'basic';
86
87
        if (! in_array($type, ['basic', 'generator'], true)) {
88
            // @codeCoverageIgnoreStart
89
            $type = $this->choice(lang('CLI.generator.commandType'), ['basic', 'generator'], 'basic');
90
            $this->eol();
91
            // @codeCoverageIgnoreEnd
92
        }
93
94
        if (! is_string($group)) {
95 2
            $group = $type === 'generator' ? config('app.name', 'App') . ':Generateurs' : config('app.name', 'App');
0 ignored issues
show
Bug introduced by
Are you sure config('app.name', 'App') of type BlitzPHP\Config\Config|null can be used in concatenation? ( Ignorable by Annotation )

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

95
            $group = $type === 'generator' ? /** @scrutinizer ignore-type */ config('app.name', 'App') . ':Generateurs' : config('app.name', 'App');
Loading history...
96
        }
97
98
        return $this->parseTemplate(
99
            $class,
100
            ['{group}', '{command}'],
101
            [$group, $command],
102
            ['type' => $type]
103 2
        );
104
    }
105
}
106