Passed
Push — master ( ecc0e1...cd7371 )
by Kevin
29:55 queued 16:08
created

MakeStory::configureCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0233

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 10
cc 1
nc 1
nop 2
crap 1.0233
1
<?php
2
3
namespace Zenstruck\Foundry\Bundle\Maker;
4
5
use Symfony\Bundle\MakerBundle\ConsoleStyle;
6
use Symfony\Bundle\MakerBundle\DependencyBuilder;
7
use Symfony\Bundle\MakerBundle\Generator;
8
use Symfony\Bundle\MakerBundle\InputConfiguration;
9
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
10
use Symfony\Bundle\MakerBundle\Validator;
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
16
/**
17
 * @author Kevin Bond <[email protected]>
18
 */
19
final class MakeStory extends AbstractMaker
20
{
21 4
    public static function getCommandName(): string
22
    {
23 4
        return 'make:story';
24
    }
25
26 32
    public static function getCommandDescription(): string
27
    {
28
        return 'Creates a Foundry story';
29 32
    }
30 32
31 32
    public function configureCommand(Command $command, InputConfiguration $inputConfig): void
32
    {
33
        $command
34 32
            ->setDescription(self::getCommandDescription())
35 32
            ->addArgument('name', InputArgument::OPTIONAL, 'The name of the story class (e.g. <fg=yellow>DefaultCategoriesStory</>)')
36
            ->addOption('test', null, InputOption::VALUE_NONE, 'Create in <fg=yellow>tests/</> instead of <fg=yellow>src/</>')
37 32
        ;
38
39 32
        $inputConfig->setArgumentAsNonInteractive('name');
40 16
    }
41
42
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
43 16
    {
44 8
        if ($input->getArgument('name')) {
45 8
            return;
46
        }
47
48 16
        if (!$input->getOption('test')) {
49 16
            $io->text('// Note: pass <fg=yellow>--test</> if you want to generate stories in your <fg=yellow>tests/</> directory');
50 16
            $io->newLine();
51 16
        }
52
53 32
        $argument = $command->getDefinition()->getArgument('name');
54
        $value = $io->ask($argument->getDescription(), $argument->getDefault(), [Validator::class, 'notBlank']);
0 ignored issues
show
Bug introduced by
It seems like $argument->getDefault() can also be of type string[]; however, parameter $default of Symfony\Component\Consol...yle\SymfonyStyle::ask() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $value = $io->ask($argument->getDescription(), /** @scrutinizer ignore-type */ $argument->getDefault(), [Validator::class, 'notBlank']);
Loading history...
55 32
        $input->setArgument($argument->getName(), $value);
56 32
    }
57 32
58 32
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
59
    {
60
        $storyClassNameDetails = $generator->createClassNameDetails(
61 32
            $input->getArgument('name'),
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('name') can also be of type null and string[]; however, parameter $name of Symfony\Bundle\MakerBund...reateClassNameDetails() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            /** @scrutinizer ignore-type */ $input->getArgument('name'),
Loading history...
62 32
            $input->getOption('test') ? 'Tests\\Story' : 'Story',
63 32
            'Story'
64 32
        );
65
66
        $generator->generateClass(
67 32
            $storyClassNameDetails->getFullName(),
68
            __DIR__.'/../Resources/skeleton/Story.tpl.php',
69 32
            []
70
        );
71 32
72 32
        $generator->writeChanges();
73
74
        $this->writeSuccessMessage($io);
75 32
76
        $io->text([
77 32
            'Next: Open your story class and start customizing it.',
78
            'Find the documentation at https://github.com/zenstruck/foundry#stories',
79
        ]);
80 32
    }
81
82
    public function configureDependencies(DependencyBuilder $dependencies): void
83
    {
84
        // noop
85
    }
86
}
87