Passed
Push — feature/add-gacela-cli ( 2018fc )
by Chema
05:13 queued 15s
created

MakeModuleCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 4
b 0
f 0
nc 2
nop 2
dl 0
loc 21
ccs 0
cts 11
cp 0
crap 6
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\CodeGenerator\Infrastructure\Command;
6
7
use Gacela\CodeGenerator\CodeGeneratorFacade;
8
use Gacela\CodeGenerator\Domain\FilenameSanitizer\FilenameSanitizer;
9
use Gacela\Framework\DocBlockResolverAwareTrait;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * @method CodeGeneratorFacade getFacade()
18
 */
19
final class MakeModuleCommand extends Command
20
{
21
    use DocBlockResolverAwareTrait;
22
23
    protected function configure(): void
24
    {
25
        $this->setName('make:module')
26
            ->setDescription('Generate a basic module with an empty ' . $this->getExpectedFilenames())
27
            ->addArgument('path', InputArgument::REQUIRED, 'The file path. For example "App/TestModule/TestSubModule"')
28
            ->addOption('short-name', 's', InputOption::VALUE_NONE, 'Remove module prefix to the class name');
29
    }
30
31
    protected function execute(InputInterface $input, OutputInterface $output): int
32
    {
33
        /** @var string $path */
34
        $path = $input->getArgument('path');
35
        $commandArguments = $this->getFacade()->parseArguments($path);
36
        $shortName = (bool)$input->getOption('short-name');
37
38
        foreach (FilenameSanitizer::EXPECTED_FILENAMES as $filename) {
39
            $fullPath = $this->getFacade()->generateFileContent(
40
                $commandArguments,
41
                $filename,
42
                $shortName
43
            );
44
            $output->writeln("> Path '{$fullPath}' created successfully");
45
        }
46
47
        $pieces = explode('/', $commandArguments->directory());
48
        $moduleName = end($pieces);
49
        $output->writeln("Module '{$moduleName}' created successfully");
50
51
        return self::SUCCESS;
52
    }
53
54
    private function getExpectedFilenames(): string
55
    {
56
        return implode(', ', FilenameSanitizer::EXPECTED_FILENAMES);
57
    }
58
}
59