MakeModuleCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

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