Passed
Pull Request — master (#2996)
by
unknown
05:21
created

JsonSchemaGenerateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 10
rs 10
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\JsonSchema\Command;
15
16
use ApiPlatform\Core\Api\OperationType;
17
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Exception\InvalidOptionException;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Style\SymfonyStyle;
25
26
/**
27
 * Generates a resource JSON Schema.
28
 *
29
 * @author Jacques Lefebvre <[email protected]>
30
 */
31
final class JsonSchemaGenerateCommand extends Command
32
{
33
    private $schemaFactory;
34
35
    private $formats;
36
37
    public function __construct(SchemaFactoryInterface $schemaFactory, array $formats)
38
    {
39
        $this->schemaFactory = $schemaFactory;
40
        $this->formats = array_keys($formats);
41
42
        parent::__construct();
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function configure()
49
    {
50
        $this
51
            ->setName('api:json-schema:generate')
52
            ->setDescription('Generates the JSON Schema for a resource operation.')
53
            ->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource')
54
            ->addOption('itemOperation', null, InputOption::VALUE_OPTIONAL, 'The item operation')
55
            ->addOption('collectionOperation', null, InputOption::VALUE_OPTIONAL, 'The collection operation')
56
            ->addOption('format', null, InputOption::VALUE_OPTIONAL, 'The response format', (string) $this->formats[0])
57
            ->addOption('output', null, InputOption::VALUE_NONE, 'Use this option to get the output version');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        $io = new SymfonyStyle($input, $output);
66
67
        $resource = $input->getArgument('resource');
68
69
        if (!is_string($resource)) {
70
            $io->error('The provided resource cannot be processed.');
71
72
            return 1;
73
        }
74
75
        $itemOperation = $input->getOption('itemOperation');
76
        $collectionOperation = $input->getOption('collectionOperation');
77
78
        $format = $input->getOption('format');
79
80
        $outputType = $input->getOption('output');
81
82
        if (!is_string($format)) {
83
            $io->error('The provided format needs to be a valid string.');
84
85
            return 1;
86
        }
87
88
        if (!in_array($format, $this->formats, true)) {
89
90
            throw new InvalidOptionException(sprintf('The response format "%s" is not supported. Supported formats are : %s.', $format, (string) implode(', ', $this->formats)));
91
        }
92
93
        $operationType = null;
94
        $operationName = null;
95
96
        if ($itemOperation && $collectionOperation) {
97
            throw new InvalidOptionException('You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.');
98
        }
99
100
        if (null !== $itemOperation || null !== $collectionOperation) {
101
            $operationType = $itemOperation ? OperationType::ITEM : OperationType::COLLECTION;
102
            $operationName = $itemOperation ?? $collectionOperation;
103
        }
104
105
        $schema = $this->schemaFactory->buildSchema($resource, $format, (bool) $outputType, $operationType, $operationName);
106
107
        if (null !== $operationType && null !== $operationName && !$schema->isDefined()) {
108
            $io->error(sprintf('There is no %s defined for the operation "%s" of the resource "%s".', $outputType ? 'outputs' : 'inputs', $operationName, $resource));
0 ignored issues
show
Bug introduced by
It seems like $operationName can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
            $io->error(sprintf('There is no %s defined for the operation "%s" of the resource "%s".', $outputType ? 'outputs' : 'inputs', /** @scrutinizer ignore-type */ $operationName, $resource));
Loading history...
109
110
            return 1;
111
        }
112
113
        $io->text((string) json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
114
    }
115
}
116