Completed
Pull Request — 2.4 (#2749)
by GRASSIOT
07:56
created

JsonSchemaGenerateCommand::execute()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 52
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
eloc 25
c 1
b 0
f 1
nc 9
nop 2
dl 0
loc 52
rs 7.3166

How to fix   Long Method    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\Schema;
18
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Exception\InvalidOptionException;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
27
/**
28
 * Generates a resource JSON Schema.
29
 *
30
 * @author Jacques Lefebvre <[email protected]>
31
 */
32
final class JsonSchemaGenerateCommand extends Command
33
{
34
    private $schemaFactory;
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_REQUIRED, 'The item operation')
55
            ->addOption('collectionOperation', null, InputOption::VALUE_REQUIRED, 'The collection operation')
56
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The response format', (string) $this->formats[0])
57
            ->addOption('type', null, InputOption::VALUE_REQUIRED, sprintf('The type of schema to generate (%s or %s)', Schema::TYPE_INPUT, Schema::TYPE_OUTPUT), Schema::TYPE_INPUT);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        $io = new SymfonyStyle($input, $output);
66
67
        /** @var string $resource */
68
        $resource = $input->getArgument('resource');
69
        /** @var ?string $itemOperation */
70
        $itemOperation = $input->getOption('itemOperation');
71
        /** @var ?string $collectionOperation */
72
        $collectionOperation = $input->getOption('collectionOperation');
73
        /** @var string $format */
74
        $format = $input->getOption('format');
75
        /** @var string $type */
76
        $type = $input->getOption('type');
77
78
        if (!\in_array($type, [Schema::TYPE_INPUT, Schema::TYPE_OUTPUT], true)) {
79
            $io->error(sprintf('You can only use "%s" or "%s" for the "type" option', Schema::TYPE_INPUT, Schema::TYPE_OUTPUT));
80
81
            return 1;
82
        }
83
84
        if (!\in_array($format, $this->formats, true)) {
85
            throw new InvalidOptionException(sprintf('The response format "%s" is not supported. Supported formats are : %s.', $format, implode(', ', $this->formats)));
86
        }
87
88
        /** @var ?string $operationType */
89
        $operationType = null;
90
        /** @var ?string $operationName */
91
        $operationName = null;
92
93
        if ($itemOperation && $collectionOperation) {
94
            $io->error('You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.');
95
96
            return 1;
97
        }
98
99
        if (null !== $itemOperation || null !== $collectionOperation) {
100
            $operationType = $itemOperation ? OperationType::ITEM : OperationType::COLLECTION;
101
            $operationName = $itemOperation ?? $collectionOperation;
102
        }
103
104
        $schema = $this->schemaFactory->buildSchema($resource, $format, $type, $operationType, $operationName);
105
106
        if (null !== $operationType && null !== $operationName && !$schema->isDefined()) {
107
            $io->error(sprintf('There is no %s defined for the operation "%s" of the resource "%s".', $type, $operationName, $resource));
108
109
            return 1;
110
        }
111
112
        $io->text((string) json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
113
114
        return 0;
115
    }
116
}
117