Passed
Push — master ( ee13c0...fd5ab9 )
by Koldo
02:31
created

MakeConsoleCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 100
dl 0
loc 151
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 37 2
A getCommandName() 0 13 2
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 and Zend expressive Framework)
51
52
PHP style config (Zend Expressive, Antidot Framework)
53
54
=====================================
55
56
<?php
57
// %1$s/some-file.prod.php
58
59
return [
60
    \'dependencies\' => [
61
        \'invokables\' => [
62
            \'%2$s\' => \'%2$s\'
63
        ]
64
    ],
65
    \'console\' => [
66
        \'commands\' => [
67
            \'%3$s\' => \'%2$s\'
68
        ]
69
    ]
70
];
71
72
======================================
73
74
YAML style config (Zend Expressive, Antidot Framework)
75
76
======================================
77
78
# %1$s/some-file.prod.yaml
79
dependencies:
80
  invokables:
81
    %2$s: %2$s
82
console:
83
    commands:
84
        \'%3$s\': %2$s
85
        
86
======================================
87
88
YAML style config (Antidot Framework Symfony style)
89
90
======================================
91
92
# %1$s/some-file.prod.yaml
93
services:
94
    %2$s:
95
    tags:
96
      - { name: \'console.command\', command: \'%3$s\' }
97
      
98
======================================
99
100
</comment>';
101
102
    protected function configure(): void
103
    {
104
        parent::configure();
105
        $this
106
            ->addArgument(
107
                'command-name',
108
                InputArgument::OPTIONAL,
109
                'Add Console Command name'
110
            );
111
    }
112
113
    protected function execute(InputInterface $input, OutputInterface $output): ?int
114
    {
115
        /** @var string $fqcn */
116
        $fqcn = $this->getFQCN($input, $output);
117
        /** @var string $commandName */
118
        $commandName = $this->getCommandName($input, $output);
119
        $getClassNameFromFQCN = $this->getClassNameFromFQCN;
120
        $getNamespaceFromFQCN = $this->getNamespaceFromFQCN;
121
        $getRealPathFromNamespace = $this->getRealPathFromNamespace;
122
        $createClassFile = $this->createClassFile;
123
        try {
124
            $className = $getClassNameFromFQCN($fqcn);
125
            $namespace = $getNamespaceFromFQCN($fqcn);
126
            $classDir = $getRealPathFromNamespace($namespace);
127
            $realFilePath = $createClassFile(
128
                $classDir,
129
                $className,
130
                sprintf(static::TEMPLATE, $namespace, $className, $commandName)
131
            );
132
        } catch (Throwable $exception) {
133
            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
134
            return 1;
135
        }
136
137
        $output->writeln(sprintf(
138
            '<info>Command %s successfully created in file %s</info>',
139
            $commandName,
140
            $realFilePath
141
        ));
142
        $output->writeln(sprintf(
143
            static::SUCCESS_HELP_TEMPLATE,
144
            $this->config['config_dir'],
145
            $fqcn,
146
            $commandName
147
        ));
148
149
        return 0;
150
    }
151
152
153
    protected function getCommandName(InputInterface $input, OutputInterface $output): string
154
    {
155
        $commandName = $input->getArgument('command-name');
156
        if (null === $commandName) {
157
            $questionHelper = $this->getHelper('question');
158
            $question = new Question(
159
                '<fg=blue>Please enter the name of the command <info>[app:my:command]</info>: </>',
160
                'app:my:command'
161
            );
162
            $commandName = $questionHelper->ask($input, $output, $question);
163
        }
164
165
        return $commandName;
166
    }
167
}
168