Completed
Push — master ( 9a81fe...55bc64 )
by Joel
10:18 queued 03:19
created

GenerateCommand::resolveConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Joli\Jane\OpenApi\Command;
4
5
use Joli\Jane\OpenApi\JaneOpenApi;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
class GenerateCommand extends Command
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 6
    public function configure()
19
    {
20 6
        $this->setName('generate');
21 6
        $this->setDescription('Generate an api client: class, normalizers and resources given a specific Json OpenApi file');
22 6
        $this->addOption('config-file', 'c', InputOption::VALUE_OPTIONAL, 'File to use for Jane OpenAPI configuration', '.jane-openapi');
23 6
        $this->addOption('reference', null, InputOption::VALUE_NONE, 'Use the JSON Reference specification in your generated library');
24 6
        $this->addOption('date-format', 'd', InputOption::VALUE_OPTIONAL, 'Date time format to use for date time field');
25 6
        $this->addArgument('openapi-file', InputArgument::OPTIONAL, 'Location of the OpenApi (Swagger) Schema file');
26 6
        $this->addArgument('namespace', InputArgument::OPTIONAL, 'Namespace prefix to use for generated files');
27 6
        $this->addArgument('directory', InputArgument::OPTIONAL, 'Directory where to generate files');
28 6
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 6
    public function execute(InputInterface $input, OutputInterface $output)
34
    {
35 6
        $options = [];
36
37 6
        if ($input->hasOption('config-file')) {
38 6
            $configFile = $input->getOption('config-file');
39
40 6
            if (!file_exists($configFile)) {
41
                throw new \RuntimeException(sprintf('Config file %s does not exist', $configFile));
42
            }
43
44 6
            $options = require $configFile;
45
46 6
            if (!is_array($options)) {
47
                throw new \RuntimeException(sprintf('Invalid config file specified or invalid return type in file %s', $configFile));
48
            }
49 6
        } else {
50 View Code Duplication
            if ($input->hasArgument('openapi-file') && null !== $input->getArgument('openapi-file')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                $options['openapi-file'] = $input->getArgument('openapi-file');
52
            }
53
54 View Code Duplication
            if ($input->hasArgument('namespace') && null !== $input->getArgument('namespace')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
                $options['namespace'] = $input->getArgument('namespace');
56
            }
57
58 View Code Duplication
            if ($input->hasArgument('directory') && null !== $input->getArgument('directory')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
                $options['directory'] = $input->getArgument('directory');
60
            }
61
62
            if ($input->hasOption('date-format') && null !== $input->getOption('date-format')) {
63
                $options['date-format'] = $input->getOption('date-format');
64
            }
65
66
            if ($input->hasOption('no-reference') && null !== $input->getOption('no-reference')) {
67
                $options['reference'] = $input->getOption('reference');
68
            }
69
        }
70
71 6
        $options = $this->resolveConfiguration($options);
72
73 6
        $janeOpenApi = JaneOpenApi::build($options);
74 6
        $files       = $janeOpenApi->generate($options['openapi-file'], $options['namespace'], $options['directory']);
75 6
        $janeOpenApi->printFiles($files, $options['directory']);
76
77 6
        foreach ($files as $file) {
78 6
            $output->writeln(sprintf("Generate %s", $file->getFilename()));
79 6
        }
80 6
    }
81
82 6
    protected function resolveConfiguration(array $options = [])
83
    {
84 6
        $optionsResolver = new OptionsResolver();
85 6
        $optionsResolver->setDefaults([
86 6
            'reference' => false,
87 6
            'date-format' => \DateTime::RFC3339,
88 6
        ]);
89
90 6
        $optionsResolver->setRequired([
91 6
            'openapi-file',
92 6
            'namespace',
93 6
            'directory',
94 6
        ]);
95
96 6
        return $optionsResolver->resolve($options);
97
    }
98
}
99