Failed Conditions
Push — master ( 28e23e...6697e2 )
by Yo
01:52
created

InitCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 26
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
crap 2
1
<?php
2
namespace Yoanm\DefaultPhpRepository\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\DefaultPhpRepository\Command\Helper\CommandTemplateHelper;
10
use Yoanm\DefaultPhpRepository\Command\Processor\CommandTemplateProcessor;
11
use Yoanm\DefaultPhpRepository\Factory\TemplatePathBagFactory;
12
use Yoanm\DefaultPhpRepository\Factory\VariableBagFactory;
13
14
class InitCommand extends Command
15
{
16
    const TYPE_INIT = 'template.init';
17
    const TYPE_GIT = 'template.git';
18
    const TYPE_COMPOSER = 'template.composer';
19
    const TYPE_TEST = 'template.test';
20
    const TYPE_CI = 'template.ci';
21
22
    const OUTPUT_LEVEL_SPACE = '    ';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        $this->setName('init')
30
            ->setDescription('Will init the current github repository with default file templates')
31
            ->addArgument(
32
                'type',
33
                InputArgument::OPTIONAL,
34
                'type of repository (library/symfony/project)',
35
                Mode::PHP_LIBRARY
36
            )
37
            ->addOption('list', 'l', InputOption::VALUE_NONE, 'List template file instead of creation them')
38
            ->addOption(
39
                'id',
40
                null,
41
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
42
                'process only given 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
        $mode = $input->getArgument('type');
60
61
        if (!$this->modeIsValid($output, $mode)) {
62
            return 1;
63
        }
64
65
        $skipExistingFile = false === $input->getOption('ask-before-override');
66
        $forceOverride = $input->getOption('force-override');
67
        if (true === $forceOverride) {
68
            $skipExistingFile = false;
69
        }
70
71
        $variableBag = (new VariableBagFactory())->load($mode);
72
        $templatePathList = (new TemplatePathBagFactory())->load($mode);
73
74
        $commandTemplateHelper = new CommandTemplateHelper(
75
            $this->getHelper('question'),
76
            $input,
77
            $output,
78
            $variableBag->all(),
79
            $skipExistingFile,
80
            $forceOverride
81
        );
82
        $commandProcessor = new CommandTemplateProcessor($commandTemplateHelper);
83
84
        $output->writeln(sprintf('<comment>Creating default files for : </comment><info>%s</info>', ucwords($mode)));
85
        if (true === $forceOverride) {
86
            $output->writeln('<fg=red>WARNING :  Existing files will be overriden by default</fg=red>');
87
        } elseif (true === $skipExistingFile) {
88
            $output->writeln('<comment>INFO : Existing files will be skipped !</comment>');
89
        }
90
        try {
91
            $currentType = null;
92
            foreach ($templatePathList as $templateKey => $templatePath) {
93
                if (count($input->getOption('id')) && !in_array($templateKey, $input->getOption('id'))) {
94
                    continue;
95
                }
96
97
                if (null === $currentType || !preg_match(sprintf('#%s#', preg_quote($currentType)), $templateKey)) {
98
                    preg_match('#(template\.[^\.]+)#', $templateKey, $matches);
99
                    $currentType = isset($matches[1]) ? $matches[1] : $templateKey;
100
                    $header = ucwords(str_replace('template.', '', $currentType));
101
                    if ('Init' === $header) {
102
                        $header = 'Init repository';
103
                    } elseif ('Ci' === $header) {
104
                        $header = 'Continuous integration';
105
                    }
106
                    $output->writeln(sprintf('<info>%s%s</info>', self::OUTPUT_LEVEL_SPACE, $header));
107
                }
108
109
                $output->writeln(sprintf(
110
                    '%s* %s : ',
111
                    str_repeat(self::OUTPUT_LEVEL_SPACE, 2),
112
                    ucwords(str_replace('template.', '', str_replace($currentType.'.', '', $templateKey)))
113
                ));
114
                if (true === $input->getOption('list')) {
115
                    $output->writeln(sprintf(
116
                        '%s<comment>Id   : </comment><info>%s</info>',
117
                        str_repeat(self::OUTPUT_LEVEL_SPACE, 3),
118
                        $templateKey
119
                    ));
120
                    $output->writeln(sprintf(
121
                        '%s<comment>File : </comment><info>%s</info>',
122
                        str_repeat(self::OUTPUT_LEVEL_SPACE, 3),
123
                        $templatePath
124
                    ));
125
                } else {
126
                    $commandProcessor->process($templatePath);
127
                }
128
            }
129
            return 0;
130
        } catch (\Exception $e) {
131
            $output->writeln(sprintf('<error>Error -> %s</error>', $e->getMessage()));
132
            throw $e;
133
        }
134
    }
135
136
    protected function modeIsValid(OutputInterface $output, $mode)
137
    {
138
        $availableModeList = Mode::all();
139
        if (!in_array($mode, $availableModeList)) {
140
            $output->writeln(sprintf('<error>Unexpected mode "%s" !</error>', $mode));
141
            $output->writeln(sprintf(
142
                '<info>Allowed mode : %s </info>',
143
                implode(
144
                    ' / ',
145
                    array_map(
146
                        function ($availableMode) {
147
                            return sprintf('<comment>%s</comment>', $availableMode);
148
                        },
149
                        $availableModeList
150
                    )
151
                )
152
            ));
153
154
            return false;
155
        }
156
157
        return true;
158
    }
159
}
160