Completed
Push — master ( 4de09a...56e82d )
by Loick
11s
created

GenerateCommand::resolveSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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