Test Failed
Branch issue/#3 (7deea8)
by Koldo
05:23
created

MakeConsoleCommand::execute()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 26
nc 5
nop 2
dl 0
loc 34
rs 9.504
c 0
b 0
f 0
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 Throwable;
11
12
use function sprintf;
13
14
class MakeConsoleCommand extends AbstractMakerCommand
15
{
16
    public const NAME = 'make:console-command';
17
    protected const FQCN_ARGUMENT_DESCRIPTION = 'Add Console Command Full qualified class name';
18
    protected const TEMPLATE = '<?php
19
20
declare(strict_types=1);
21
22
namespace %s;
23
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class %s extends Command
29
{
30
    public const NAME = \'%s\';
31
    
32
    protected function configure(): void
33
    {
34
        $this->setName(self::NAME);
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output): ?int
38
    {
39
        // do your stuff here... ;-D
40
    }
41
}
42
';
43
    protected const SUCCESS_HELP_TEMPLATE = '<comment>
44
To activate the newly created Command you must register it in the configuration. (This examples are valid for Antidot'
45
    . ' Framework and Zend expressive Framework)
46
47
PHP style config (Zend Expressive, Antidot Framework)
48
49
=====================================
50
51
<?php
52
// %1$s/some-file.prod.php
53
54
return [
55
    \'dependencies\' => [
56
        \'invokables\' => [
57
            \'%2$s\' => \'%2$s\'
58
        ]
59
    ],
60
    \'console\' => [
61
        \'commands\' => [
62
            \'%3$s\' => \'%2$s\'
63
        ]
64
    ]
65
];
66
67
======================================
68
69
YAML style config (Zend Expressive, Antidot Framework)
70
71
======================================
72
73
# %1$s/some-file.prod.yaml
74
dependencies:
75
  invokables:
76
    %2$s: %2$s
77
console:
78
    commands:
79
        \'%3$s\': %2$s
80
        
81
======================================
82
83
YAML style config (Antidot Framework Symfony style)
84
85
======================================
86
87
# %1$s/some-file.prod.yaml
88
services:
89
    %2$s:
90
    tags:
91
      - { name: \'console.command\', command: \'%3$s\' }
92
      
93
======================================
94
95
</comment>';
96
97
    protected function configure(): void
98
    {
99
        $this
100
            ->setName(self::NAME)
101
            ->addArgument(
102
                'fqcn',
103
                InputArgument::REQUIRED,
104
                self::FQCN_ARGUMENT_DESCRIPTION
105
            )
106
            ->addArgument(
107
                'command-name',
108
                InputArgument::REQUIRED,
109
                'Add Console Command name'
110
            );
111
    }
112
113
    protected function execute(InputInterface $input, OutputInterface $output): ?int
114
    {
115
        /** @var string $fqcn */
116
        $fqcn = $input->getArgument('fqcn');
117
        /** @var string $commandName */
118
        $commandName = $input->getArgument('command-name');
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
            self::SUCCESS_HELP_TEMPLATE,
144
            $this->config['config_dir'],
145
            $fqcn,
146
            $commandName
147
        ));
148
    }
149
}
150