MakeConsoleCommand::getCommandName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\DevTools\Application\Command;
6
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\Question;
11
use Throwable;
12
13
use function sprintf;
14
15
class MakeConsoleCommand extends AbstractMakerCommand
16
{
17
    public const NAME = 'make:console-command';
18
    protected const COMMAND_DESCRIPTION = 'Creates a console command class.';
19
    protected const FQCN_ARGUMENT_DESCRIPTION = 'Add Console Command Full qualified class name';
20
    protected const QUESTION =
21
        '<fg=blue>Please enter the name of the Console Command class <info>[App\Console\MyCommand]</info>: </> ';
22
    protected const DEFAULT_RESPONSE = 'App\Console\MyCommand';
23
    protected const TEMPLATE = '<?php
24
25
declare(strict_types=1);
26
27
namespace %s;
28
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
33
class %s extends Command
34
{
35
    public const NAME = \'%s\';
36
    
37
    protected function configure(): void
38
    {
39
        $this->setName(self::NAME);
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output): ?int
43
    {
44
        // do your stuff here... ;-D
45
    }
46
}
47
';
48
    protected const SUCCESS_HELP_TEMPLATE = '<comment>
49
To activate the newly created Command you must register it in the configuration. (This examples are valid for Antidot'
50
    . ' Framework)
51
52
PHP style config (Antidot Framework)
53
54
=====================================
55
56
<?php
57
// %1$s/some-file.prod.php
58
59
return [
60
    \'services\' => [
61
        \'%2$s\' => \'%2$s\'
62
    ],
63
    \'console\' => [
64
        \'commands\' => [
65
            \'%3$s\' => \'%2$s\'
66
        ]
67
    ]
68
];
69
70
======================================
71
72
YAML style config (Antidot Framework)
73
74
======================================
75
76
# %1$s/some-file.prod.yaml
77
services:
78
  %2$s: %2$s
79
console:
80
    commands:
81
        \'%3$s\': %2$s
82
        
83
======================================
84
85
YAML style config (Antidot Framework Symfony style)
86
87
======================================
88
89
# %1$s/some-file.prod.yaml
90
services:
91
    %2$s:
92
    tags:
93
      - { name: \'console.command\', command: \'%3$s\' }
94
      
95
======================================
96
97
</comment>';
98
99
    protected function configure(): void
100
    {
101
        parent::configure();
102
        $this
103
            ->addArgument(
104
                'command-name',
105
                InputArgument::OPTIONAL,
106
                'Add Console Command name'
107
            );
108
    }
109
110
    protected function execute(InputInterface $input, OutputInterface $output): ?int
111
    {
112
        /** @var string $fqcn */
113
        $fqcn = $this->getFQCN($input, $output);
114
        /** @var string $commandName */
115
        $commandName = $this->getCommandName($input, $output);
116
        $getClassNameFromFQCN = $this->getClassNameFromFQCN;
117
        $getNamespaceFromFQCN = $this->getNamespaceFromFQCN;
118
        $getRealPathFromNamespace = $this->getRealPathFromNamespace;
119
        $createClassFile = $this->createClassFile;
120
        try {
121
            $className = $getClassNameFromFQCN($fqcn);
122
            $namespace = $getNamespaceFromFQCN($fqcn);
123
            $classDir = $getRealPathFromNamespace($namespace);
124
            $realFilePath = $createClassFile(
125
                $classDir,
126
                $className,
127
                sprintf(static::TEMPLATE, $namespace, $className, $commandName)
128
            );
129
        } catch (Throwable $exception) {
130
            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
131
            return 1;
132
        }
133
134
        $output->writeln(sprintf(
135
            '<info>Command %s successfully created in file %s</info>',
136
            $commandName,
137
            $realFilePath
138
        ));
139
        $output->writeln(sprintf(
140
            static::SUCCESS_HELP_TEMPLATE,
141
            $this->config['config_dir'],
142
            $fqcn,
143
            $commandName
144
        ));
145
146
        return 0;
147
    }
148
149
150
    protected function getCommandName(InputInterface $input, OutputInterface $output): string
151
    {
152
        $commandName = $input->getArgument('command-name');
153
        if (null === $commandName) {
154
            $questionHelper = $this->getHelper('question');
155
            $question = new Question(
156
                '<fg=blue>Please enter the name of the command <info>[app:my:command]</info>: </>',
157
                'app:my:command'
158
            );
159
            $commandName = $questionHelper->ask($input, $output, $question);
160
        }
161
162
        return $commandName;
163
    }
164
}
165