Completed
Push — master ( 1c6d46...3dbc46 )
by Joel
10:24
created

GenerateCommand::resolveSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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