Passed
Pull Request — master (#2996)
by
unknown
04:56
created

JsonSchemaGenerateCommand::execute()   B

Complexity

Conditions 11
Paths 8

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
eloc 20
c 1
b 0
f 1
nc 8
nop 2
dl 0
loc 34
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = $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', array_key_first($this->formats))
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
        $itemOperation = $input->getOption('itemOperation');
69
        $collectionOperation = $input->getOption('collectionOperation');
70
        $format = $input->getOption('format');
71
        $outputType = $input->getOption('output');
72
73
        if (!isset($this->formats[$format])) {
74
            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

74
            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...
75
        }
76
77
        $operationType = null;
78
        $operationName = null;
79
80
        if ($itemOperation && $collectionOperation) {
81
            throw new InvalidOptionException('You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.');
82
        }
83
84
        if (null !== $itemOperation || null !== $collectionOperation) {
85
            $operationType = $itemOperation ? OperationType::ITEM : OperationType::COLLECTION;
86
            $operationName = $itemOperation ?? $collectionOperation;
87
        }
88
89
        $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

89
        $schema = $this->schemaFactory->buildSchema(/** @scrutinizer ignore-type */ $resource, $format, $outputType , $operationType, $operationName);
Loading history...
90
91
        if (null !== $operationType && null !== $operationName && !$schema->isDefined()) {
92
            $io->error(sprintf('There is no %s defined for the operation "%s" of the resource "%s".', $outputType ? 'outputs': 'inputs', $operationName, $resource));
93
            return;
94
        }
95
96
        $io->text(json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
97
    }
98
}
99