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

CreateStubsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 20
rs 9.7333
cc 1
nc 1
nop 0
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\Constants\Versions;
15
use TgScraper\Generator;
16
use Throwable;
17
18
class CreateStubsCommand extends Command
19
{
20
21
    protected static $defaultName = 'app:create-stubs';
22
23
    protected function validateData(string|false $data): bool
24
    {
25
        return false !== $data and !empty($data);
26
    }
27
28
    protected function configure(): void
29
    {
30
        $this
31
            ->setDescription('Create stubs from bot API schema.')
32
            ->setHelp('This command allows you to create class stubs for all types of the Telegram bot API.')
33
            ->addArgument('destination', InputArgument::REQUIRED, 'Destination directory')
34
            ->addOption('namespace-prefix', null, InputOption::VALUE_REQUIRED, 'Namespace prefix for stubs', 'TelegramApi')
35
            ->addOption(
36
                'json',
37
                null,
38
                InputOption::VALUE_REQUIRED,
39
                'Path to JSON file to use instead of fetching from URL (this option takes precedence over "--layer")'
40
            )
41
            ->addOption(
42
                'yaml',
43
                null,
44
                InputOption::VALUE_REQUIRED,
45
                'Path to YAML file to use instead of fetching from URL (this option takes precedence over "--layer" and "--json")'
46
            )
47
            ->addOption('layer', 'l', InputOption::VALUE_REQUIRED, 'Bot API version to use', 'latest');
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        $logger = new ConsoleLogger($output);
53
        $url = Versions::getVersionFromText($input->getOption('layer'));
54
        $yamlPath = $input->getOption('yaml');
55
        if (empty($yamlPath)) {
56
            $jsonPath = $input->getOption('json');
57
            if (empty($jsonPath)) {
58
                $logger->info('Using URL: ' . $url);
59
                try {
60
                    $output->writeln('Fetching data from URL...');
61
                    $generator = new Generator($logger, $url);
62
                } catch (Throwable) {
63
                    return Command::FAILURE;
64
                }
65
            } else {
66
                $data = file_get_contents($jsonPath);
67
                if (!$this->validateData($data)) {
68
                    $logger->critical('Invalid JSON file provided');
69
                    return Command::INVALID;
70
                }
71
                $logger->info('Using JSON schema: ' . $jsonPath);
1 ignored issue
show
Bug introduced by
Are you sure $jsonPath of type boolean|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

71
                $logger->info('Using JSON schema: ' . /** @scrutinizer ignore-type */ $jsonPath);
Loading history...
72
                /** @noinspection PhpUnhandledExceptionInspection */
73
                $generator = Generator::fromJson($logger, $data);
74
            }
75
        } else {
76
            $data = file_get_contents($yamlPath);
77
            if (!$this->validateData($data)) {
78
                $logger->critical('Invalid YAML file provided');
79
                return Command::INVALID;
80
            }
81
            $logger->info('Using YAML schema: ' . $yamlPath);
1 ignored issue
show
Bug introduced by
Are you sure $yamlPath of type boolean|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

81
            $logger->info('Using YAML schema: ' . /** @scrutinizer ignore-type */ $yamlPath);
Loading history...
82
            /** @noinspection PhpUnhandledExceptionInspection */
83
            $generator = Generator::fromYaml($logger, $data);
84
        }
85
        try {
86
            $output->writeln('Creating stubs...');
87
            $generator->toStubs($input->getArgument('destination'), $input->getOption('namespace-prefix'));
1 ignored issue
show
Bug introduced by
It seems like $input->getArgument('destination') can also be of type null and string[]; however, parameter $directory of TgScraper\Generator::toStubs() 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

87
            $generator->toStubs(/** @scrutinizer ignore-type */ $input->getArgument('destination'), $input->getOption('namespace-prefix'));
Loading history...
88
        } catch (Exception) {
89
            $logger->critical('Could not create stubs.');
90
            return Command::FAILURE;
91
        }
92
        $output->writeln('Done!');
93
        return Command::SUCCESS;
94
    }
95
96
}