Passed
Push — master ( 4bf53d...ce4836 )
by Sys
09:47
created

ExportSchemaCommand::execute()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 30
rs 8.9137
cc 6
nc 18
nop 2
1
<?php
2
3
4
namespace TgScraper\Commands;
5
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Logger\ConsoleLogger;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use TgScraper\Constants\Versions;
14
use TgScraper\Generator;
15
use Throwable;
16
17
class ExportSchemaCommand extends Command
18
{
19
20
    protected static $defaultName = 'app:export-schema';
21
22
    protected function configure(): void
23
    {
24
        $this
25
            ->setDescription('Export schema as JSON or YAML.')
26
            ->setHelp('This command allows you to create a schema for a specific version of the Telegram bot API.')
27
            ->addArgument('destination', InputArgument::REQUIRED, 'Destination file')
28
            ->addOption(
29
                'yaml',
30
                null,
31
                InputOption::VALUE_NONE,
32
                'Export schema as YAML instead of JSON (this option takes precedence over "--postman")'
33
            )
34
            ->addOption('postman', null, InputOption::VALUE_NONE, 'Export schema as a Postman-compatible JSON')
35
            ->addOption('options', 'o', InputOption::VALUE_REQUIRED, 'Encoder options', 0)
36
            ->addOption('readable', 'r', InputOption::VALUE_NONE, '(JSON only) Generate a human-readable JSON')
37
            ->addOption('inline', null, InputOption::VALUE_REQUIRED, '(YAML only) Inline level', 6)
38
            ->addOption('indent', null, InputOption::VALUE_REQUIRED, '(YAML only) Indent level', 4)
39
            ->addOption('layer', 'l', InputOption::VALUE_REQUIRED, 'Bot API version to use', 'latest');
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output): int
43
    {
44
        $logger = new ConsoleLogger($output);
45
        $url = Versions::getVersionFromText($input->getOption('layer'));
46
        $logger->info('Using URL: ' . $url);
47
        try {
48
            $output->writeln('Fetching data from URL...');
49
            $generator = new Generator($logger, $url);
50
        } catch (Throwable) {
51
            return Command::FAILURE;
52
        }
53
        $output->writeln('Exporting schema from data...');
54
        $options = $input->getOption('options');
55
        $useYaml = $input->getOption('yaml');
56
        if ($useYaml) {
57
            $data = $generator->toYaml($input->getOption('inline'), $input->getOption('indent'), $options);
58
        }
59
        $destination = $input->getArgument('destination');
60
        if ($input->getOption('readable')) {
61
            $options |= JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
62
        }
63
        $data ??= $input->getOption('postman') ? $generator->toPostman($options) : $generator->toJson($options);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
64
        $output->writeln('Saving scheme to file...');
65
        $result = file_put_contents($destination, $data);
1 ignored issue
show
Bug introduced by
It seems like $destination can also be of type null and string[]; however, parameter $filename of file_put_contents() 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
        $result = file_put_contents(/** @scrutinizer ignore-type */ $destination, $data);
Loading history...
66
        if (false === $result) {
67
            $logger->critical('Unable to save file to ' . $destination);
1 ignored issue
show
Bug introduced by
Are you sure $destination of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            $logger->critical('Unable to save file to ' . /** @scrutinizer ignore-type */ $destination);
Loading history...
68
            return Command::FAILURE;
69
        }
70
        $output->writeln('Done!');
71
        return Command::SUCCESS;
72
    }
73
74
}