ExportSchemaCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 71
c 1
b 0
f 0
dl 0
loc 93
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 39 1
B execute() 0 44 10
1
<?php
2
3
4
namespace TgScraper\Commands;
5
6
7
use Exception;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Logger\ConsoleLogger;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use TgScraper\Common\Encoder;
15
use TgScraper\Constants\Versions;
16
use TgScraper\TgScraper;
17
use Throwable;
18
19
class ExportSchemaCommand extends Command
20
{
21
22
    use Common;
23
24
    protected static $defaultName = 'app:export-schema';
25
26
    protected function configure(): void
27
    {
28
        $this
29
            ->setDescription('Export schema as JSON or YAML.')
30
            ->setHelp('This command allows you to create a schema for a specific version of the Telegram bot API.')
31
            ->addArgument('destination', InputArgument::REQUIRED, 'Destination file')
32
            ->addOption(
33
                'yaml',
34
                null,
35
                InputOption::VALUE_NONE,
36
                'Export schema as YAML instead of JSON (does not affect "--postman")'
37
            )
38
            ->addOption(
39
                'postman',
40
                null,
41
                InputOption::VALUE_NONE,
42
                'Export schema as a Postman-compatible JSON'
43
            )
44
            ->addOption(
45
                'openapi',
46
                null,
47
                InputOption::VALUE_NONE,
48
                'Export schema as a OpenAPI-compatible file (takes precedence over "--postman")'
49
            )
50
            ->addOption('options', 'o', InputOption::VALUE_REQUIRED, 'Encoder options', 0)
51
            ->addOption(
52
                'readable',
53
                'r',
54
                InputOption::VALUE_NONE,
55
                'Generate a human-readable file (overrides "--inline" and "--indent")'
56
            )
57
            ->addOption('inline', null, InputOption::VALUE_REQUIRED, '(YAML only) Inline level', 16)
58
            ->addOption('indent', null, InputOption::VALUE_REQUIRED, '(YAML only) Indent level', 4)
59
            ->addOption('layer', 'l', InputOption::VALUE_REQUIRED, 'Bot API version to use', Versions::LATEST)
60
            ->addOption(
61
                'prefer-stable',
62
                null,
63
                InputOption::VALUE_NONE,
64
                'Prefer latest stable version (takes precedence over "--layer")'
65
            );
66
    }
67
68
    protected function execute(InputInterface $input, OutputInterface $output): int
69
    {
70
        $logger = new ConsoleLogger($output);
71
        $version = Versions::getVersionFromText($input->getOption('layer'));
72
        if ($input->getOption('prefer-stable')) {
73
            $version = Versions::STABLE;
74
        }
75
        $logger->info('Using version: ' . $version);
76
        try {
77
            $output->writeln('Fetching data for version...');
78
            $generator = TgScraper::fromVersion($logger, $version);
79
        } catch (Throwable $e) {
80
            $logger->critical((string)$e);
81
            return Command::FAILURE;
82
        }
83
        $output->writeln('Exporting schema from data...');
84
        $destination = $input->getArgument('destination');
85
        try {
86
            TgScraper::getTargetDirectory(pathinfo($destination)['dirname']);
87
        } catch (Exception) {
88
            return Command::FAILURE;
89
        }
90
        $readable = $input->getOption('readable');
91
        $options = $input->getOption('options');
92
        $useYaml = $input->getOption('yaml');
93
        $inline = $readable ? 16 : $input->getOption('inline');
94
        $indent = $readable ? 4 : $input->getOption('indent');
95
        $output->writeln('Saving schema to file...');
96
        if ($input->getOption('openapi')) {
97
            $data = $generator->toOpenApi();
98
            if ($useYaml) {
99
                return $this->saveFile($logger, $output, $destination, Encoder::toYaml($data, $inline, $indent, $options), log: false);
100
            }
101
            return $this->saveFile($logger, $output, $destination, Encoder::toJson($data, $options | JSON_UNESCAPED_SLASHES, $readable), log: false);
102
        }
103
        if ($input->getOption('postman')) {
104
            $data = $generator->toPostman();
105
            return $this->saveFile($logger, $output, $destination, Encoder::toJson($data, $options, $readable), log: false);
106
        }
107
        $data = $generator->toArray();
108
        if ($useYaml) {
109
            return $this->saveFile($logger, $output, $destination, Encoder::toYaml($data, $inline, $indent, $options), log: false);
110
        }
111
        return $this->saveFile($logger, $output, $destination, Encoder::toJson($data, $options, $readable), log: false);
112
    }
113
114
}