WebhookCommand::setWebhook()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 8
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
/*
4
 * This file is part of the PhpTelegramBot/Laravel package.
5
 *
6
 * (c) Avtandil Kikabidze aka LONGMAN <[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 PhpTelegramBot\Laravel\Commands;
15
16
use Illuminate\Console\Command;
17
use Longman\TelegramBot\Exception\TelegramException;
18
use Longman\TelegramBot\Request;
19
use PhpTelegramBot\Laravel\PhpTelegramBotContract;
20
21
use function json_encode;
22
23
use const JSON_PRETTY_PRINT;
24
25
class WebhookCommand extends Command
26
{
27
    protected $signature = 'telegram:webhook {webhook?}
28
    {--delete : Delete webhook}
29
    {--info : Get webhook info}';
30
31
    protected $description = 'Set, delete, or get webhook info for Telegram Bot';
32
33
    /** @var \PhpTelegramBot\Laravel\PhpTelegramBotContract */
34
    protected $telegramBot;
35
36
    public function __construct(PhpTelegramBotContract $telegramBot)
37
    {
38
        parent::__construct();
39
40
        $this->telegramBot = $telegramBot;
41
    }
42
43
    public function handle(): void
44
    {
45
        $webhook = $this->argument('webhook');
46
        $delete  = $this->option('delete');
47
        $info    = $this->option('info');
48
49
        if (! ($webhook || $delete || $info)) {
50
            $this->error('Not enough arguments!');
51
            $this->error('php artisan telegram:webhook {webhook?} {--delete} {--info}');
52
            return;
53
        }
54
55
        if ($delete && ! $this->deleteWebhook()) {
56
            return;
57
        }
58
59
        if ($webhook && ! $this->setWebhook($webhook)) {
0 ignored issues
show
Bug introduced by
It seems like $webhook can also be of type array; however, parameter $webhook of PhpTelegramBot\Laravel\C...okCommand::setWebhook() 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

59
        if ($webhook && ! $this->setWebhook(/** @scrutinizer ignore-type */ $webhook)) {
Loading history...
60
            return;
61
        }
62
63
        if ($info && ! $this->showWebhookInfo()) {
64
            return;
65
        }
66
67
        $this->info('All done!');
68
    }
69
70
    private function setWebhook(string $webhook): bool
71
    {
72
        try {
73
            $this->telegramBot->setWebhook($webhook);
0 ignored issues
show
Bug introduced by
The method setWebhook() does not exist on PhpTelegramBot\Laravel\PhpTelegramBotContract. Since it exists in all sub-types, consider adding an abstract or default implementation to PhpTelegramBot\Laravel\PhpTelegramBotContract. ( Ignorable by Annotation )

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

73
            $this->telegramBot->/** @scrutinizer ignore-call */ 
74
                                setWebhook($webhook);
Loading history...
74
            $this->info('Webhook set successfully!');
75
        } catch (TelegramException $e) {
76
            $this->error("Couldn't set webhook");
77
            $this->error($e->getMessage());
78
            return false;
79
        }
80
81
        return true;
82
    }
83
84
    private function deleteWebhook(): bool
85
    {
86
        try {
87
            $this->telegramBot->deleteWebhook();
0 ignored issues
show
Bug introduced by
The method deleteWebhook() does not exist on PhpTelegramBot\Laravel\PhpTelegramBotContract. Since it exists in all sub-types, consider adding an abstract or default implementation to PhpTelegramBot\Laravel\PhpTelegramBotContract. ( Ignorable by Annotation )

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

87
            $this->telegramBot->/** @scrutinizer ignore-call */ 
88
                                deleteWebhook();
Loading history...
88
            $this->info('Webhook deleted successfully!');
89
        } catch (TelegramException $e) {
90
            $this->error("Couldn't delete webhook");
91
            $this->error($e->getMessage());
92
            return false;
93
        }
94
95
        return true;
96
    }
97
98
    private function showWebhookInfo(): bool
99
    {
100
        try {
101
            $request = Request::getWebhookInfo();
102
            if (! $request->isOk()) {
103
                throw new TelegramException($request->getDescription());
104
            }
105
            $this->info('Current webhook info:');
106
            $this->info(json_encode($request->getResult(), JSON_PRETTY_PRINT));
107
        } catch (TelegramException $e) {
108
            $this->error("Couldn't get webhook info");
109
            $this->error($e->getMessage());
110
            return false;
111
        }
112
113
        return true;
114
    }
115
}
116