Passed
Push — master ( e23bc1...ee0f92 )
by Kevin
03:07
created

MakeStory::generate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.052

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 12
nc 1
nop 3
dl 0
loc 21
ccs 13
cts 17
cp 0.7647
crap 2.052
rs 9.8666
c 1
b 0
f 1
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 2
    public static function getCommandName(): string
22
    {
23 2
        return 'make:story';
24
    }
25
26 16
    public function configureCommand(Command $command, InputConfiguration $inputConfig): void
27
    {
28
        $command
29 16
            ->setDescription('Creates a Foundry story')
30 16
            ->addArgument('name', InputArgument::OPTIONAL, 'The name of the story class (e.g. <fg=yellow>DefaultCategoriesStory</>)')
31 16
            ->addOption('test', null, InputOption::VALUE_NONE, 'Create in <fg=yellow>tests/</> instead of <fg=yellow>src/</>')
32
        ;
33
34 16
        $inputConfig->setArgumentAsNonInteractive('name');
35 16
    }
36
37 16
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
38
    {
39 16
        if ($input->getArgument('name')) {
40 8
            return;
41
        }
42
43 8
        if (!$input->getOption('test')) {
44 4
            $io->text('// Note: pass <fg=yellow>--test</> if you want to generate stories in your <fg=yellow>tests/</> directory');
45 4
            $io->newLine();
46
        }
47
48 8
        $argument = $command->getDefinition()->getArgument('name');
49 8
        $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

49
        $value = $io->ask($argument->getDescription(), /** @scrutinizer ignore-type */ $argument->getDefault(), [Validator::class, 'notBlank']);
Loading history...
50 8
        $input->setArgument($argument->getName(), $value);
51 8
    }
52
53 16
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
54
    {
55 16
        $storyClassNameDetails = $generator->createClassNameDetails(
56 16
            $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

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