Failed Conditions
Pull Request — release/0.2.0 (#12)
by Yo
02:09
created

InitCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 32
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
crap 2
1
<?php
2
namespace Yoanm\InitPhpRepository\Command;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Yoanm\InitPhpRepository\Factory\TemplateListFactory;
10
use Yoanm\InitPhpRepository\Factory\VarFactory;
11
use Yoanm\InitPhpRepository\Helper\CommandHelper;
12
use Yoanm\InitPhpRepository\Helper\TemplateHelper;
13
use Yoanm\InitPhpRepository\Processor\InitRepositoryProcessor;
14
use Yoanm\InitPhpRepository\Processor\ListTemplatesProcessor;
15
16
class InitCommand extends Command
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected function configure()
22
    {
23
        $this->setName('init')
24
            ->setDescription('Will init the current github repository with default file templates')
25
            ->addArgument(
26
                'type',
27
                InputArgument::OPTIONAL,
28
                'Repository type (library/project)',
29
                RepositoryType::PROJECT
30
            )
31
            ->addOption(
32
                'symfony',
33
                null,
34
                InputOption::VALUE_NONE,
35
                'If symfony sub type'
36
            )
37
            ->addOption('list', 'l', InputOption::VALUE_NONE, 'List template ids')
38
            ->addOption(
39
                'id',
40
                null,
41
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
42
                'process only given templates ids'
43
            )
44
            ->addOption(
45
                'ask-before-override',
46
                'a',
47
                InputOption::VALUE_NONE,
48
                'Will ask before overriding an existing file'
49
            )
50
            ->addOption('force-override', 'f', InputOption::VALUE_NONE, 'Override existing files by default')
51
        ;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $repositoryType = $input->getArgument('type');
60
        $repositorySubType = true === $input->getOption('symfony')
61
            ? RepositorySubType::SYMFONY
62
            : RepositorySubType::PHP
63
        ;
64
65
        if (!$this->validateRepositoryType($output, $repositoryType)) {
66
            return 1;
67
        }
68
69
        $this->getProcessor($input, $output, $repositoryType, $repositorySubType)->process();
70
    }
71
72
    /**
73
     * @param OutputInterface $output
74
     * @param string          $repositoryType
75
     *
76
     * @return bool
77
     */
78
    protected function validateRepositoryType(OutputInterface $output, $repositoryType)
79
    {
80
        $availableTypeList = RepositoryType::all();
81
        if (!in_array($repositoryType, $availableTypeList)) {
82
            $output->writeln(sprintf('<error>Unexpected type "%s" !</error>', $repositoryType));
83
            $output->writeln(sprintf(
84
                '<info>Allowed type : %s </info>',
85
                implode(' / ', array_map(
86
                    function ($availableMode) {
87
                        return sprintf('<comment>%s</comment>', $availableMode);
88
                    },
89
                    $availableTypeList
90
                ))
91
            ));
92
93
            return false;
94
        }
95
96
        return true;
97
    }
98
99
    /**
100
     * @param InputInterface  $input
101
     * @param OutputInterface $output
102
     * @param string          $repositoryType
103
     * @param string          $repositorySubType
104
     *
105
     * @return InitRepositoryProcessor|ListTemplatesProcessor
106
     *
107
     * @throws \Twig_Error_Loader
108
     */
109
    protected function getProcessor(InputInterface $input, OutputInterface $output, $repositoryType, $repositorySubType)
110
    {
111
        $forceOverride = $input->getOption('force-override');
112
        $skipExisting = true === $forceOverride
113
            ? false
114
            : false === $input->getOption('ask-before-override')
115
        ;
116
117
        $helper = new CommandHelper(
118
            $input,
119
            $output,
120
            new TemplateHelper(
121
                new \Twig_Environment(null, ['autoescape' => false]),
122
                (new VarFactory())->create(RepositoryType::PROJECT === $repositoryType)
123
            ),
124
            $this->getHelper('question'),
1 ignored issue
show
Compatibility introduced by
$this->getHelper('question') of type object<Symfony\Component...Helper\HelperInterface> is not a sub-type of object<Symfony\Component...\Helper\QuestionHelper>. It seems like you assume a concrete implementation of the interface Symfony\Component\Console\Helper\HelperInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
125
            (new TemplateListFactory())->create($repositoryType, $repositorySubType)
126
        );
127
128
        if (true === $input->getOption('list')) {
129
            $processor = new ListTemplatesProcessor($helper);
130
        } else {
131
            $processor = new InitRepositoryProcessor(
132
                $helper,
133
                $skipExisting,
134
                $forceOverride,
135
                $input->getOption('id')
136
            );
137
        }
138
139
        return $processor;
140
    }
141
}
142