AbstractMaker::getArgumentChooseClassHelp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 13
ccs 5
cts 5
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (c) 2022 Ne-Lexa <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 *
11
 * @see https://github.com/Ne-Lexa/roach-php-bundle
12
 */
13
14
namespace Nelexa\RoachPhpBundle\Maker;
15
16
use Symfony\Bundle\MakerBundle\ConsoleStyle;
17
use Symfony\Bundle\MakerBundle\DependencyBuilder;
18
use Symfony\Bundle\MakerBundle\Generator;
19
use Symfony\Bundle\MakerBundle\InputConfiguration;
20
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker as BaseAbstractMaker;
21
use Symfony\Bundle\MakerBundle\Str;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputInterface;
25
26
abstract class AbstractMaker extends BaseAbstractMaker
27
{
28
    abstract protected function getSuffix(): string;
29
30
    abstract protected function getNamespace(): string;
31
32
    abstract protected function getTemplateFilename(): string;
33
34 1
    public static function getCommandDescription(): string
35
    {
36 1
        return sprintf('Create a new roach %s class', static::getDescriptionClass());
37
    }
38
39 8
    public function configureCommand(Command $command, InputConfiguration $inputConfig): void
40
    {
41
        $command
42
            ->addArgument(
43 8
                'class',
44
                InputArgument::OPTIONAL,
45 8
                $this->getArgumentChooseClassDescription()
46
            )
47 8
            ->setHelp($this->getArgumentChooseClassHelp())
48
        ;
49
    }
50
51
    /**
52
     * @throws \Exception
53
     */
54 8
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
55
    {
56 8
        $classNameDetails = $generator->createClassNameDetails(
57 8
            (string) $input->getArgument('class'),
58 8
            $this->getNamespace(),
59 8
            $this->getSuffix()
60
        );
61
62 8
        $generator->generateClass(
63 8
            $classNameDetails->getFullName(),
64 8
            $this->getTemplateFilename()
65
        );
66
67 8
        $generator->writeChanges();
68
69 8
        $this->writeSuccessMessage($io);
70 8
        $io->text($this->getAfterSuccessMessage());
71
    }
72
73
    public function configureDependencies(DependencyBuilder $dependencies): void
74
    {
75
    }
76
77 8
    protected function getArgumentChooseClassDescription(): string
78
    {
79 8
        return sprintf(
80 8
            'Choose a name for your ' . $this->getSuffix() . ' class (e.g. <fg=yellow>%s%s</>)',
81 8
            Str::asClassName(Str::getRandomTerm()),
82 8
            $this->getSuffix(),
83
        );
84
    }
85
86
    abstract protected static function getDescriptionClass(): string;
87
88 8
    protected function getArgumentChooseClassHelp(): string
89
    {
90 8
        $generateName = static::getDescriptionClass();
91
92 8
        return sprintf(
93 8
            'The <info>%%command.name%%</info> command generates a new roach %s class.
94
95
<info>php %%command.full_name%% CoolStuff%s</info>
96
97
If the argument is missing, the command will ask for the %s class name interactively.',
98
            $generateName,
99 8
            $this->getSuffix(),
100
            $generateName
101
        );
102
    }
103
104 7
    protected function getAfterSuccessMessage(): string
105
    {
106 7
        return sprintf('Next: Open your new %s class and write your logic!', static::getDescriptionClass());
107
    }
108
}
109