Completed
Pull Request — master (#4226)
by Craig
04:58
created

SetNamespaceMaker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 57
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configureDependencies() 0 9 1
A getCommandName() 0 3 1
A configureCommand() 0 6 1
A generate() 0 19 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreBundle\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;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Zikula\Bundle\CoreBundle\DynamicConfigDumper;
25
26
class SetNamespaceMaker extends AbstractMaker
27
{
28
    /**
29
     * @var DynamicConfigDumper
30
     */
31
    private $configDumper;
32
33
    public function __construct(
34
        DynamicConfigDumper $configDumper
35
    ) {
36
        $this->configDumper = $configDumper;
37
    }
38
39
    public static function getCommandName(): string
40
    {
41
        return 'make:set-namespace';
42
    }
43
44
    public function configureCommand(Command $command, InputConfiguration $inputConfig)
45
    {
46
        $command
47
            ->setDescription('Sets the maker namespace config')
48
            ->addArgument('namespace', InputArgument::OPTIONAL, 'Choose a namespace (e.g. <fg=yellow>Acme\\BlogModule</>)')
49
            ->setHelp('Set the maker namespace config <info>php %command.full_name% Acme/BlogModule</info>')
50
        ;
51
    }
52
53
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
54
    {
55
        try {
56
            $namespace = Validators::validateBundleNamespace($input->getArgument('namespace'), true);
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('namespace') can also be of type null and string[]; however, parameter $namespace of Zikula\Bundle\CoreBundle...lidateBundleNamespace() 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
            $namespace = Validators::validateBundleNamespace(/** @scrutinizer ignore-type */ $input->getArgument('namespace'), true);
Loading history...
57
        } catch (\InvalidArgumentException $exception) {
58
            $io->error($exception->getMessage());
59
60
            return 1;
61
        }
62
        $this->configDumper->setConfiguration(
63
            'maker',
64
            [
65
                'root_namespace' => $namespace,
66
            ],
67
            true
68
        );
69
        $io->success(sprintf('The `config/generated_dev.yaml` file has been updated to set `maker:root_namespace` value to %s.', $namespace));
70
71
        return 0;
72
    }
73
74
    public function configureDependencies(DependencyBuilder $dependencies)
75
    {
76
        $dependencies->addClassDependency(
77
            Command::class,
78
            'console'
79
        );
80
        $dependencies->addClassDependency(
81
            DynamicConfigDumper::class,
82
            'zikula/core-bundle'
83
        );
84
    }
85
}
86