Passed
Pull Request — master (#2302)
by GRASSIOT
03:07
created

SwaggerCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 39
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 2
A __construct() 0 11 1
B execute() 0 24 8
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Command;
15
16
use ApiPlatform\Core\Documentation\Documentation;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Exception\InvalidOptionException;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Style\SymfonyStyle;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25
use Symfony\Component\Yaml\Yaml;
26
27
/**
28
 * Console command to dump Swagger API documentations.
29
 *
30
 * @author Amrouche Hamza <[email protected]>
31
 */
32
final class SwaggerCommand extends Command
33
{
34
    private $v2documentationNormalizer;
35
    private $v3documentationNormalizer;
36
    private $resourceNameCollectionFactory;
37
    private $apiTitle;
38
    private $apiDescription;
39
    private $apiVersion;
40
    private $apiFormats;
41
42
    public function __construct(NormalizerInterface $v2documentationNormalizer, ResourceNameCollectionFactoryInterface $resourceNameCollection, string $apiTitle, string $apiDescription, string $apiVersion, array $apiFormats, NormalizerInterface $v3documentationNormalizer = null)
43
    {
44
        $this->v2documentationNormalizer = $v2documentationNormalizer;
45
        $this->v3documentationNormalizer = $v3documentationNormalizer;
46
        $this->resourceNameCollectionFactory = $resourceNameCollection;
47
        $this->apiTitle = $apiTitle;
48
        $this->apiDescription = $apiDescription;
49
        $this->apiVersion = $apiVersion;
50
        $this->apiFormats = $apiFormats;
51
52
        parent::__construct();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function configure()
59
    {
60
        $this
61
            ->setName('api:openapi:export')
62
            ->setAliases(['api:swagger:export'])
63
            ->setDescription('Dump the OpenAPI documentation')
64
            ->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML')
65
            ->addOption('spec-version', null, InputOption::VALUE_OPTIONAL, 'OpenAPI version to use ("2" or "3")', $this->v3documentationNormalizer ? '3' : '2')
66
            ->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file');
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        $io = new SymfonyStyle($input, $output);
75
        /** @var string $version */
76
        $version = $input->getOption('spec-version');
77
        if ('3' === $version && null === $this->v3documentationNormalizer) {
78
            $io->note("You don't have a defined normalizer supporting version 3 of the specification, falling back to version 2.");
79
            $version = '2';
80
        }
81
82
        if (!\in_array($version, ['2', '3'], true)) {
83
            throw new InvalidOptionException(sprintf('This tool only support version 2 and 3 of the OpenAPI specification ("%s" given).', $version));
84
        }
85
86
        $documentationNormalizer = sprintf('v%sdocumentationNormalizer', $version);
87
        $documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
88
        $data = $this->$documentationNormalizer->normalize($documentation);
89
        $content = $input->getOption('yaml') ? Yaml::dump($data, 10, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE) : (json_encode($data, JSON_PRETTY_PRINT) ?: '');
90
91
        if (!empty($filename = $input->getOption('output')) && \is_string($filename)) {
92
            file_put_contents($filename, $content);
93
            $io->success(sprintf('Data written to %s (specification version %s).', $filename, $version));
94
        } else {
95
            $output->writeln($content);
96
        }
97
    }
98
}
99