Passed
Pull Request — develop (#234)
by Mikaël
49:21 queued 46:19
created

GeneratePackageCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 191
Code Lines 159

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 159
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 159
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 191
ccs 159
cts 159
cp 1
crap 1
rs 8

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 WsdlToPhp\PackageGenerator\Command;
6
7
use DateTime;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use WsdlToPhp\PackageBase\AbstractSoapClientBase;
12
use WsdlToPhp\PackageBase\AbstractStructArrayBase;
13
use WsdlToPhp\PackageBase\AbstractStructBase;
14
use WsdlToPhp\PackageBase\AbstractStructEnumBase;
15
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions;
16
use WsdlToPhp\PackageGenerator\Generator\Generator;
17
18
final class GeneratePackageCommand extends AbstractCommand
19
{
20
    public const GENERATOR_OPTIONS_CONFIG_OPTION = 'config';
21
    public const PROPER_USER_CONFIGURATION = 'wsdltophp.yml';
22
    public const DEFAULT_CONFIGURATION_FILE = 'wsdltophp.yml.dist';
23
24
    protected Generator $generator;
25
26
    protected GeneratorOptions $generatorOptions;
27
28 6
    public function getGenerator(): Generator
29
    {
30 6
        return $this->generator;
31
    }
32
33 20
    public function getGeneratorOptionsConfigOption()
34
    {
35 20
        return $this->getOptionValue(self::GENERATOR_OPTIONS_CONFIG_OPTION);
36
    }
37
38 20
    public function resolveGeneratorOptionsConfigPath(): ?string
39
    {
40 20
        $path = null;
41 20
        $possibilities = $this->getGeneratorOptionsPossibilities();
42 20
        foreach ($possibilities as $possibility) {
43 20
            if (!empty($possibility) && is_file($possibility)) {
44 20
                $path = $possibility;
45
46 20
                break;
47
            }
48
        }
49
50 20
        return $path;
51
    }
52
53 20
    public function getGeneratorOptionsPossibilities(): array
54
    {
55
        return [
56 20
            $this->getGeneratorOptionsConfigOption(),
57 20
            sprintf('%s/%s', getcwd(), self::PROPER_USER_CONFIGURATION),
58 20
            sprintf('%s/%s', getcwd(), self::DEFAULT_CONFIGURATION_FILE),
59 20
            GeneratorOptions::getDefaultConfigurationPath(),
60
        ];
61
    }
62
63 6
    protected function setGenerator(Generator $generator): self
64
    {
65 6
        $this->generator = $generator;
66
67 6
        return $this;
68
    }
69
70 8
    protected function initGenerator(): self
71
    {
72 8
        return $this->setGenerator(new Generator($this->generatorOptions));
73
    }
74
75 20
    protected function configure(): void
76
    {
77 20
        parent::configure();
78
        $this
79 20
            ->setName('generate:package')
80 20
            ->setDescription('Generate package based on options')
81 20
            ->addOption(
82 20
                'urlorpath',
83 20
                null,
84 20
                InputOption::VALUE_REQUIRED,
85 20
                'Url or path to WSDL'
86
            )
87 20
            ->addOption(
88 20
                'destination',
89 20
                null,
90 20
                InputOption::VALUE_REQUIRED,
91 20
                'Path to destination directory, where the package will be generated'
92
            )
93 20
            ->addOption(
94 20
                'login',
95 20
                null,
96 20
                InputOption::VALUE_OPTIONAL,
97 20
                'Basic authentication login required to access the WSDL url, can be avoided mot of the time'
98
            )
99 20
            ->addOption(
100 20
                'password',
101 20
                null,
102 20
                InputOption::VALUE_OPTIONAL,
103 20
                'Basic authentication password required to access the WSDL url, can be avoided mot of the time'
104
            )
105 20
            ->addOption(
106 20
                'proxy-host',
107 20
                null,
108 20
                InputOption::VALUE_OPTIONAL,
109 20
                'Use proxy url'
110
            )
111 20
            ->addOption(
112 20
                'proxy-port',
113 20
                null,
114 20
                InputOption::VALUE_OPTIONAL,
115 20
                'Use proxy port'
116
            )
117 20
            ->addOption(
118 20
                'proxy-login',
119 20
                null,
120 20
                InputOption::VALUE_OPTIONAL,
121 20
                'Use proxy login'
122
            )
123 20
            ->addOption(
124 20
                'proxy-password',
125 20
                null,
126 20
                InputOption::VALUE_OPTIONAL,
127 20
                'Use proxy password'
128
            )
129 20
            ->addOption(
130 20
                'prefix',
131 20
                null,
132 20
                InputOption::VALUE_REQUIRED,
133 20
                'Prepend generated classes'
134
            )
135 20
            ->addOption(
136 20
                'suffix',
137 20
                null,
138 20
                InputOption::VALUE_REQUIRED,
139 20
                'Append generated classes'
140
            )
141 20
            ->addOption(
142 20
                'namespace',
143 20
                null,
144 20
                InputOption::VALUE_OPTIONAL,
145 20
                'Package classes\' namespace'
146
            )
147 20
            ->addOption(
148 20
                'category',
149 20
                null,
150 20
                InputOption::VALUE_OPTIONAL,
151 20
                'First level directory name generation mode (start, end, cat, none)'
152
            )
153 20
            ->addOption(
154 20
                'gathermethods',
155 20
                null,
156 20
                InputOption::VALUE_OPTIONAL,
157 20
                'Gather methods based on operation name mode (start, end)'
158
            )
159 20
            ->addOption(
160 20
                'gentutorial',
161 20
                null,
162 20
                InputOption::VALUE_OPTIONAL,
163 20
                'Enable/Disable tutorial file, you should enable this option only on dev'
164
            )
165 20
            ->addOption(
166 20
                'genericconstants',
167 20
                null,
168 20
                InputOption::VALUE_OPTIONAL,
169 20
                'Enable/Disable usage of generic constants name (ex : ENUM_VALUE_0, ENUM_VALUE_1, etc) or contextual values (ex : VALUE_STRING, VALUE_YES, VALUES_NO, etc)'
170
            )
171 20
            ->addOption(
172 20
                'addcomments',
173 20
                null,
174 20
                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
175 20
                'Set comments to be used within each generated file'
176
            )
177 20
            ->addOption(
178 20
                'standalone',
179 20
                null,
180 20
                InputOption::VALUE_OPTIONAL,
181 20
                'By default, the generated package can be used as a standalone. Otherwise, you must add wsdltophp/packagebase:dev-master to your main composer.json.'
182
            )
183 20
            ->addOption(
184 20
                'validation',
185 20
                null,
186 20
                InputOption::VALUE_OPTIONAL,
187 20
                'Enable/Disable the generation of validation rules in every generated setter.'
188
            )
189 20
            ->addOption(
190 20
                'struct',
191 20
                null,
192 20
                InputOption::VALUE_OPTIONAL,
193 20
                sprintf('Use this class as parent class for any StructType class. Default class is %s from wsdltophp/packagebase package', AbstractStructBase::class)
194
            )
195 20
            ->addOption(
196 20
                'structarray',
197 20
                null,
198 20
                InputOption::VALUE_OPTIONAL,
199 20
                sprintf('Use this class as parent class for any StructArrayType class. Default class is %s from wsdltophp/packagebase package', AbstractStructArrayBase::class)
200
            )
201 20
            ->addOption(
202 20
                'structenum',
203 20
                null,
204 20
                InputOption::VALUE_OPTIONAL,
205 20
                sprintf('Use this class as parent class for any StructEnumType class. Default class is %s from wsdltophp/packagebase package', AbstractStructEnumBase::class)
206
            )
207 20
            ->addOption(
208 20
                'soapclient',
209 20
                null,
210 20
                InputOption::VALUE_OPTIONAL,
211 20
                sprintf('Use this class as parent class for any ServiceType class. Default class is %s from wsdltophp/packagebase package', AbstractSoapClientBase::class)
212
            )
213 20
            ->addOption(
214 20
                'composer-name',
215 20
                null,
216 20
                InputOption::VALUE_REQUIRED,
217 20
                'Composer name of the generated package'
218
            )
219 20
            ->addOption(
220 20
                'composer-settings',
221 20
                null,
222 20
                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
223 20
                'Composer settings of the generated package'
224
            )
225 20
            ->addOption(
226 20
                'structs-folder',
227 20
                null,
228 20
                InputOption::VALUE_OPTIONAL,
229 20
                'Structs folder name (default: SructType)'
230
            )
231 20
            ->addOption(
232 20
                'arrays-folder',
233 20
                null,
234 20
                InputOption::VALUE_OPTIONAL,
235 20
                'Arrays folder name (default: ArrayType)'
236
            )
237 20
            ->addOption(
238 20
                'enums-folder',
239 20
                null,
240 20
                InputOption::VALUE_OPTIONAL,
241 20
                'Enumerations folder name (default: EnumType)'
242
            )
243 20
            ->addOption(
244 20
                'services-folder',
245 20
                null,
246 20
                InputOption::VALUE_OPTIONAL,
247 20
                'Services class folder name (default: ServiceType)'
248
            )
249 20
            ->addOption(
250 20
                'src-dirname',
251 20
                null,
252 20
                InputOption::VALUE_OPTIONAL,
253 20
                'Source directory subfolder oof destination directory (default: src)'
254
            )
255 20
            ->addOption(
256 20
                'xsd-types-path',
257 20
                null,
258 20
                InputOption::VALUE_OPTIONAL,
259 20
                'Path to the xsd types configuration file to load'
260
            )
261 20
            ->addOption(
262 20
                self::GENERATOR_OPTIONS_CONFIG_OPTION,
263 20
                null,
264 20
                InputOption::VALUE_OPTIONAL,
265 20
                'Path to the generator\'s configuration file to load'
266
            )
267
        ;
268 20
    }
269
270 20
    protected function execute(InputInterface $input, OutputInterface $output): int
271
    {
272 20
        parent::execute($input, $output);
273 20
        $start = new DateTime();
274 20
        $this->writeLn(sprintf(' Start at %s', $start->format('Y-m-d H:i:s')));
275 20
        $this->initGeneratorOptions();
276 20
        if ($this->canExecute()) {
277 8
            $this->initGenerator()->getGenerator()->generatePackage();
278
        } else {
279 12
            $this->writeLn('  Generation not launched, use "--force" option to force generation');
280 12
            $this->writeLn(sprintf("  Generator's option file used: %s", $this->resolveGeneratorOptionsConfigPath()));
281 12
            $this->writeLn("  Used generator's options:");
282 12
            $this->writeLn('    '.implode(PHP_EOL.'    ', $this->formatArrayForConsole($this->generatorOptions->toArray())));
283
        }
284 14
        $end = new DateTime();
285 14
        $this->writeLn(sprintf(' End at %s, duration: %s', $end->format('Y-m-d H:i:s'), $start->diff($end)->format('%H:%I:%S')));
286
287 14
        return self::EXIT_OK;
288
    }
289
290 20
    protected function getPackageGenerationCommandLineOptions(): array
291
    {
292
        return [
293 20
            'addcomments' => 'AddComments',
294
            'arrays-folder' => 'ArraysFolder',
295
            'composer-name' => 'ComposerName',
296
            'composer-settings' => 'ComposerSettings',
297
            'category' => 'Category',
298
            'destination' => 'Destination',
299
            'enums-folder' => 'EnumsFolder',
300
            'gathermethods' => 'GatherMethods',
301
            'genericconstants' => 'GenericConstantsName',
302
            'gentutorial' => 'GenerateTutorialFile',
303
            'login' => 'BasicLogin',
304
            'namespace' => 'Namespace',
305
            'password' => 'BasicPassword',
306
            'prefix' => 'Prefix',
307
            'proxy-host' => 'ProxyHost',
308
            'proxy-login' => 'ProxyLogin',
309
            'proxy-password' => 'ProxyPassword',
310
            'proxy-port' => 'ProxyPort',
311
            'services-folder' => 'ServicesFolder',
312
            'src-dirname' => 'SrcDirname',
313
            'structarray' => 'StructArrayClass',
314
            'structenum' => 'StructEnumClass',
315
            'structs-folder' => 'StructsFolder',
316
            'soapclient' => 'SoapClientClass',
317
            'struct' => 'StructClass',
318
            'standalone' => 'Standalone',
319
            'suffix' => 'Suffix',
320
            'urlorpath' => 'Origin',
321
            'validation' => 'Validation',
322
            'xsd-types-path' => 'XsdTypesPath',
323
        ];
324
    }
325
326 20
    protected function initGeneratorOptions(): self
327
    {
328 20
        $generatorOptions = GeneratorOptions::instance($this->resolveGeneratorOptionsConfigPath());
329
330 20
        foreach ($this->getPackageGenerationCommandLineOptions() as $optionName => $optionMethod) {
331 20
            $optionValue = $this->formatOptionValue($this->input->getOption($optionName));
332 20
            if (null !== $optionValue) {
333 20
                call_user_func_array([
334 20
                    $generatorOptions,
335 20
                    sprintf('set%s', $optionMethod),
336
                ], [
337 20
                    $optionValue,
338
                ]);
339
            }
340
        }
341 20
        $this->generatorOptions = $generatorOptions;
342
343 20
        return $this;
344
    }
345
346 20
    protected function formatOptionValue($optionValue)
347
    {
348 20
        if ('true' === $optionValue || (is_numeric($optionValue) && 1 === (int) $optionValue)) {
349
            return true;
350
        }
351 20
        if ('false' === $optionValue || (is_numeric($optionValue) && 0 === (int) $optionValue)) {
352
            return false;
353
        }
354
355 20
        return $optionValue;
356
    }
357
358
    /**
359
     * Utility method to return readable array based on "key: value".
360
     */
361 12
    protected function formatArrayForConsole(array $array): array
362
    {
363 12
        array_walk($array, function (&$value, $index) {
364 12
            $value = sprintf('%s: %s', $index, json_encode($value));
365 12
        });
366
367 12
        return $array;
368
    }
369
}
370