Completed
Push — nln-php7 ( 1a6b54...2856d9 )
by Nicolas
02:57
created

ConfigureActionCommand::processInputs()   B

Complexity

Conditions 9
Paths 52

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 9.015

Importance

Changes 0
Metric Value
dl 0
loc 64
ccs 33
cts 35
cp 0.9429
rs 7.2298
c 0
b 0
f 0
cc 9
nc 52
nop 1
crap 9.015

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma\Console;
6
7
use Karma\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Karma\Application;
13
use Karma\Configuration\FilterInputVariable;
14
use Karma\ConfigurableProcessor;
15
16
abstract class ConfigureActionCommand extends Command
17
{
18
    use FilterInputVariable;
19
20
    private const
21
        ENV_DEV = 'dev',
22
        OPTION_ASSIGNMENT = '=';
23
24
    private bool
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
25
        $dryRun,
26
        $isBackupEnabled;
27
    private ?string
28
        $systemEnvironment;
29
    private string
30
        $name,
31
        $description,
32
        $outputTitle;
33
34
    protected string
35
        $environment;
36
37 36
    public function __construct(Application $app, string $name, string $description, string $outputTitle)
38
    {
39 36
        $this->name = $name;
40 36
        $this->description = $description;
41 36
        $this->outputTitle = $outputTitle;
42
43 36
        parent::__construct($app);
44
45 36
        $this->dryRun = false;
46 36
        $this->isBackupEnabled = false;
47
48 36
        $this->environment = self::ENV_DEV;
49 36
        $this->systemEnvironment = null;
50 36
    }
51
52 36
    protected function configure(): void
53
    {
54 36
        parent::configure();
55
56
        $this
57 36
            ->setName($this->name)
58 36
            ->setDescription($this->description)
59
60 36
            ->addArgument('sourcePath', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'source path to hydrate/generate')
61
62 36
            ->addOption('targetPath', 't', InputOption::VALUE_REQUIRED, 'target path to hydrate/generate', null)
63 36
            ->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Target environment', self::ENV_DEV)
64 36
            ->addOption('system', 's', InputOption::VALUE_REQUIRED, 'Target environment for system variables', null)
65 36
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Simulation mode')
66 36
            ->addOption('backup', 'b', InputOption::VALUE_NONE, 'Backup overwritten files')
67 36
            ->addOption('override', 'o', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Override variable values', array())
68 36
            ->addOption('data', 'd', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Custom data values', array())
69
        ;
70 36
    }
71
72 27
    protected function execute(InputInterface $input, OutputInterface $output): int
73
    {
74 27
        parent::execute($input, $output);
75
76 27
        $this->processInputs($input);
77
78 17
        $processor = $this->getProcessor();
79 17
        $this->configureProcessor($processor);
80 17
        $this->launchConfigurationAction($processor);
81
82 17
        return 0;
83
    }
84
85 27
    private function processInputs(InputInterface $input): void
86
    {
87 27
        $this->environment = (string) $input->getOption('env');
88 27
        $this->systemEnvironment = $input->getOption('system');
89
90 27
        if($input->getOption('dry-run'))
91
        {
92 2
            $this->dryRun = true;
93 2
            $this->output->writeln("<fg=cyan>Run in dry-run mode</fg=cyan>");
94
        }
95
96 27
        if($input->getOption('backup'))
97
        {
98 2
            $this->isBackupEnabled = true;
99 2
            $this->output->writeln("<fg=cyan>Backup enabled</fg=cyan>");
100
        }
101
102 27
        $profile = $this->app['profile'];
103 27
        $sourcePath = $input->getArgument('sourcePath');
104 27
        if(empty($sourcePath))
105
        {
106 5
            if($profile->hasSourcePath() !== true)
107
            {
108 2
                throw new \RuntimeException('Missing argument sourcePath');
109
            }
110
111 3
            $sourcePath = $profile->getSourcePath();
112
        }
113
114 25
        if(! is_array($sourcePath))
115
        {
116 22
            $sourcePath = [$sourcePath];
117
        }
118
119 25
        $targetPath = $input->getOption('targetPath');
120
121 25
        if(is_array($targetPath))
122
        {
123
            throw new \RuntimeException('Invalid argument targetPath : could not be mutiple (single path required as string)');
124
        }
125
126 25
        if(empty($targetPath) && $profile->hasTargetPath() === true)
127
        {
128
            $targetPath = $profile->getTargetPath();
129
        }
130
131 25
        $this->output->writeln(sprintf(
132 25
            "<info>%s <comment>%s</comment> with <comment>%s</comment> values</info>",
133 25
            $this->outputTitle,
134 25
            implode(' ', $sourcePath),
135 25
            $this->environment
136
        ));
137 25
        $this->output->writeln('');
138
139 25
        $this->app['sources.path'] = $sourcePath;
140 25
        $this->app['target.path'] = $targetPath;
141
142 25
        $this->processOverridenVariables(
143 25
            $this->parseOptionWithAssignments($input, 'override')
144
        );
145 21
        $this->processCustomData(
146 21
            $this->parseOptionWithAssignments($input, 'data')
147
        );
148 17
    }
149
150
    abstract protected function launchConfigurationAction(ConfigurableProcessor $processor): void;
151
    abstract protected function getProcessor(): ConfigurableProcessor;
152
153 17
    private function configureProcessor(ConfigurableProcessor $processor)
154
    {
155 17
        if($this->dryRun === true)
156
        {
157 2
            $processor->setDryRun();
158
        }
159
160 17
        if($this->isBackupEnabled === true)
161
        {
162 2
            $processor->enableBackup();
163
        }
164
165 17
        if($this->systemEnvironment !== null)
166
        {
167 2
            $processor->setSystemEnvironment($this->systemEnvironment);
168
169 2
             $this->app['logger']->info(sprintf(
170 2
                'Hydrate <important>system</important> variables with <important>%s</important> values',
171 2
                $this->systemEnvironment
172
            ));
173
        }
174 17
    }
175
176 25
    private function parseOptionWithAssignments(InputInterface $input, $optionName)
177
    {
178 25
        $strings = $input->getOption($optionName);
179
180 25
        if(! is_array($strings))
181
        {
182 4
            $strings = [$strings];
183
        }
184
185 25
        $data = [];
186
187 25
        foreach($strings as $string)
188
        {
189 15
            if(stripos($string, self::OPTION_ASSIGNMENT) === false)
190
            {
191 4
                throw new \InvalidArgumentException(sprintf(
192 4
                    '%s option must contain %c : --%s <variable>=<value>',
193
                    $optionName,
194 4
                    self::OPTION_ASSIGNMENT,
195
                    $optionName
196
                ));
197
            }
198
199 11
            list($variable, $value) = explode(self::OPTION_ASSIGNMENT, $string, 2);
200
201 11
            if(array_key_exists($variable, $data))
202
            {
203 4
                throw new \InvalidArgumentException("Duplicated %s option value : $variable");
204
            }
205
206 11
            $data[$variable] = $value;
207
        }
208
209 21
        return $data;
210
    }
211
212 21
    private function processOverridenVariables(array $overrides): void
213
    {
214 21
        $reader = $this->app['configuration'];
215 21
        $logger = $this->app['logger'];
216
217 21
        foreach($overrides as $variable => $value)
218
        {
219 5
            $logger->info(sprintf(
220 5
               'Override <important>%s</important> with value <important>%s</important>',
221
               $variable,
222
               $value
223
            ));
224
225 5
            $value = $this->parseList($value);
226
227 5
            $reader->overrideVariable($variable, $this->filterValue($value));
228
        }
229 21
    }
230
231 17
    private function processCustomData(array $data): void
232
    {
233 17
        $reader = $this->app['configuration'];
234 17
        $logger = $this->app['logger'];
235
236 17
        foreach($data as $variable => $value)
237
        {
238 2
            $logger->info(sprintf(
239 2
               'Set custom data <important>%s</important> with value <important>%s</important>',
240
               $variable,
241
               $value
242
            ));
243
244 2
            $reader->setCustomData($variable, $this->filterValue($value));
245
        }
246 17
    }
247
}
248