Passed
Pull Request — master (#2996)
by
unknown
04:46
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
     * {@inheritdoc}
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        $io = new SymfonyStyle($input, $output);
57
58
        $resource = $input->getArgument('resource');
59
        $itemOperation = $input->getOption('itemOperation');
60
        $collectionOperation = $input->getOption('collectionOperation');
61
        $format = $input->getOption('format');
62
        $outputType = $input->getOption('output');
63
64
        if (!isset($this->formats[$format])) {
65
            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

65
            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...
66
        }
67
68
        $operationType = null;
69
        $operationName = null;
70
71
        if ($itemOperation && $collectionOperation) {
72
            throw new InvalidOptionException('You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.');
73
        }
74
75
        if (null !== $itemOperation || null !== $collectionOperation) {
76
            $operationType = $itemOperation ? OperationType::ITEM : OperationType::COLLECTION;
77
            $operationName = $itemOperation ?? $collectionOperation;
78
        }
79
80
        $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

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