Passed
Pull Request — master (#2996)
by
unknown
07:56 queued 04:24
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
declare(strict_types=1);
4
5
namespace ApiPlatform\Core\JsonSchema\Command;
6
7
use ApiPlatform\Core\Api\OperationType;
8
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Exception\InvalidOptionException;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
17
/**
18
 * Generates a resource JSON Schema.
19
 *
20
 * @author Jacques Lefebvre <[email protected]>
21
 */
22
final class JsonSchemaGenerateCommand extends Command
23
{
24
    private $schemaFactory;
25
26
    private $formats;
27
28
    public function __construct(SchemaFactoryInterface $schemaFactory, array $formats)
29
    {
30
        $this->schemaFactory = $schemaFactory;
31
        $this->formats = $formats;
32
33
        parent::__construct();
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function configure()
40
    {
41
        $this
42
            ->setName('api:json-schema:generate')
43
            ->setDescription('Generates the JSON Schema for a resource operation.')
44
            ->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource')
45
            ->addOption('itemOperation', null, InputOption::VALUE_OPTIONAL, 'The item operation')
46
            ->addOption('collectionOperation', null, InputOption::VALUE_OPTIONAL, 'The collection operation')
47
            ->addOption('format', null, InputOption::VALUE_OPTIONAL, 'The response format', array_key_first($this->formats))
48
            ->addOption('output', null, InputOption::VALUE_NONE, 'Use this option to get the output version')
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $io = new SymfonyStyle($input, $output);
58
59
        $resource = $input->getArgument('resource');
60
        $itemOperation = $input->getOption('itemOperation');
61
        $collectionOperation = $input->getOption('collectionOperation');
62
        $format = $input->getOption('format');
63
        $outputType = $input->getOption('output');
64
65
        if (!isset($this->formats[$format])) {
66
            throw new InvalidOptionException(\sprintf('The response format "%s" is not supported. Supported formats are : %s.', $format, implode(', ', array_keys($this->formats))));
0 ignored issues
show
Bug introduced by
It seems like $format 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

66
            throw new InvalidOptionException(\sprintf('The response format "%s" is not supported. Supported formats are : %s.', /** @scrutinizer ignore-type */ $format, implode(', ', array_keys($this->formats))));
Loading history...
67
        }
68
69
        $operationType = null;
70
        $operationName = null;
71
72
        if ($itemOperation && $collectionOperation) {
73
            throw new InvalidOptionException('You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.');
74
        }
75
76
        if (null !== $itemOperation || null !== $collectionOperation) {
77
            $operationType = $itemOperation ? OperationType::ITEM : OperationType::COLLECTION;
78
            $operationName = $itemOperation ?? $collectionOperation;
79
        }
80
81
        $schema = $this->schemaFactory->buildSchema($resource, $format,  $outputType , $operationType, $operationName);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type null and string[]; however, parameter $resourceClass of ApiPlatform\Core\JsonSch...nterface::buildSchema() 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

81
        $schema = $this->schemaFactory->buildSchema(/** @scrutinizer ignore-type */ $resource, $format,  $outputType , $operationType, $operationName);
Loading history...
82
83
        if (!$schema->isDefined()) {
84
            $io->error(\sprintf('There is no %s defined for the operation "%s" of the resource "%s".',  $outputType ? 'outputs': 'inputs', $operationName, $resource));
85
            return;
86
        }
87
88
        $io->text(\json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
89
    }
90
}
91