GenerateCommand::resolveConfiguration()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Joli\Jane\OpenApi\Command;
4
5
use Joli\Jane\OpenApi\JaneOpenApi;
6
use Joli\Jane\Registry;
7
use Joli\Jane\Schema;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
class GenerateCommand extends Command
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 13
    public function configure()
21
    {
22 13
        $this->setName('generate');
23 13
        $this->setDescription('Generate an api client: class, normalizers and resources given a specific Json OpenApi file');
24 13
        $this->addOption('config-file', 'c', InputOption::VALUE_OPTIONAL, 'File to use for Jane OpenAPI configuration');
25 13
        $this->addOption('reference', null, InputOption::VALUE_NONE, 'Use the JSON Reference specification in your generated library');
26 13
        $this->addOption('date-format', 'd', InputOption::VALUE_OPTIONAL, 'Date time format to use for date time field');
27 13
        $this->addArgument('openapi-file', InputArgument::OPTIONAL, 'Location of the OpenApi (Swagger) Schema file');
28 13
        $this->addArgument('namespace', InputArgument::OPTIONAL, 'Namespace prefix to use for generated files');
29 13
        $this->addArgument('directory', InputArgument::OPTIONAL, 'Directory where to generate files');
30 13
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 13
    public function execute(InputInterface $input, OutputInterface $output)
36
    {
37 13
        $options = [];
38
39 13
        $configFile = null;
40
41 13
        if (!$input->hasOption('config-file') && file_exists('.jane-openapi')) {
42
            $configFile = '.jane-openapi';
43 13
        } elseif($input->hasOption('config-file') && null !== $input->getOption('config-file')) {
44 13
            $configFile = $input->getOption('config-file');
45
        }
46
47 13
        if ($configFile) {
48 13
            $configFile = $input->getOption('config-file');
49
50 13
            if (!file_exists($configFile)) {
51
                throw new \RuntimeException(sprintf('Config file %s does not exist', $configFile));
52
            }
53
54 13
            $options = require $configFile;
55
56 13
            if (!is_array($options)) {
57 13
                throw new \RuntimeException(sprintf('Invalid config file specified or invalid return type in file %s', $configFile));
58
            }
59
        } else {
60 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...
61
                $options['openapi-file'] = $input->getArgument('openapi-file');
62
            }
63
64 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...
65
                $options['namespace'] = $input->getArgument('namespace');
66
            }
67
68 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...
69
                $options['directory'] = $input->getArgument('directory');
70
            }
71
72
            if ($input->hasOption('date-format') && null !== $input->getOption('date-format')) {
73
                $options['date-format'] = $input->getOption('date-format');
74
            }
75
76
            if ($input->hasOption('no-reference') && null !== $input->getOption('no-reference')) {
77
                $options['reference'] = $input->getOption('reference');
78
            }
79
        }
80
81 13
        $options = $this->resolveConfiguration($options);
82 13
        $registry = new Registry();
83
84 13
        if (array_key_exists('openapi-file', $options)) {
85 12
            $registry->addSchema($this->resolveSchema($options['openapi-file'], $options));
86
        } else {
87 1
            foreach ($options['mapping'] as $schema => $schemaOptions) {
88 1
                $registry->addSchema($this->resolveSchema($schema, $schemaOptions));
89
            }
90
        }
91
92 13
        $janeOpenApi = JaneOpenApi::build($options);
93 13
        $files       = $janeOpenApi->generate($registry);
94 13
        $janeOpenApi->printFiles($files, $registry);
95
96 13
        foreach ($files as $file) {
97 13
            $output->writeln(sprintf("Generate %s", $file->getFilename()));
98
        }
99 13
    }
100
101 13
    protected function resolveConfiguration(array $options = [])
102
    {
103 13
        $optionsResolver = new OptionsResolver();
104 13
        $optionsResolver->setDefaults([
105 13
            'reference' => false,
106
            'date-format' => \DateTime::RFC3339,
107
        ]);
108
109 13
        if (array_key_exists('openapi-file', $options)) {
110 12
            $optionsResolver->setRequired([
111 12
                'openapi-file',
112
                'namespace',
113
                'directory',
114
            ]);
115
        } else {
116 1
            $optionsResolver->setRequired([
117 1
                'mapping'
118
            ]);
119
        }
120
121 13
        return $optionsResolver->resolve($options);
122
    }
123
124 13
    protected function resolveSchema($schema, array $options = [])
125
    {
126 13
        $optionsResolver = new OptionsResolver();
127
128
        // To support old schema
129 13
        $optionsResolver->setDefined([
130 13
            'openapi-file',
131
            'reference',
132
            'date-format',
133
        ]);
134
135 13
        $optionsResolver->setRequired([
136 13
            'namespace',
137
            'directory',
138
        ]);
139
140 13
        $options = $optionsResolver->resolve($options);
141
142 13
        return new Schema($schema, $options['namespace'], $options['directory'], '');
143
    }
144
}
145