Test Setup Failed
Push — master ( eb5aa8...93071b )
by Alexey
03:12
created

TelegramWebHookCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 7
dl 0
loc 94
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setClient() 0 4 1
A setRouter() 0 4 1
A setToken() 0 4 1
A setMaxConnections() 0 4 1
A configure() 0 8 1
B execute() 0 33 3
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Bundle\FrameworkBundle\Routing\Router;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
use unreal4u\TelegramAPI\Telegram\Methods\DeleteWebhook;
12
use unreal4u\TelegramAPI\Telegram\Methods\SetWebhook;
13
use unreal4u\TelegramAPI\TgLog;
14
15
/**
16
 * Sets or deletes Telegram bot Web-Hook
17
 * @see https://core.telegram.org/bots/api#setwebhook
18
 */
19
class TelegramWebHookCommand extends ContainerAwareCommand
20
{
21
    private const MODE_SET = 'set';
22
    private const MODE_DELETE = 'delete';
23
24
    /**
25
     * @var TgLog
26
     */
27
    private $client;
28
29
    /**
30
     * @var Router
31
     */
32
    private $router;
33
34
    /**
35
     * @var string
36
     */
37
    private $token;
38
39
    /**
40
     * @var int
41
     */
42
    private $maxConnections;
43
44
    public function setClient(TgLog $client): void
45
    {
46
        $this->client = $client;
47
    }
48
49
    public function setRouter(Router $router): void
50
    {
51
        $this->router = $router;
52
    }
53
54
    public function setToken(string $token): void
55
    {
56
        $this->token = $token;
57
    }
58
59
    public function setMaxConnections(int $maxConnections)
60
    {
61
        $this->maxConnections = $maxConnections;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function configure()
68
    {
69
        $this
70
            ->setName('telegram:webhook')
71
            ->setDescription('Set webhook')
72
            ->addArgument('mode', InputArgument::REQUIRED, 'Command mode (set or delete)')
73
        ;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        if (self::MODE_SET === strtolower($input->getArgument('mode'))) {
82
83
            $url = $this->router->generate(
84
                'telegram_webhook',
85
                ['token' => $this->token],
86
                UrlGeneratorInterface::ABSOLUTE_URL
87
            );
88
89
            $output->writeln('Setting webhook: '.$url);
90
91
            $setWebHook = new SetWebhook();
92
            $setWebHook->url = $url;
93
            $setWebHook->max_connections = $this->maxConnections;
94
95
            $this->client->performApiRequest($setWebHook);
96
97
            $output->writeln('Done');
98
        } elseif (self::MODE_DELETE === strtolower($input->getArgument('mode'))) {
99
            $output->writeln('Deleting webhook');
100
101
            $deleteWebHook = new DeleteWebhook();
102
103
            $this->client->performApiRequest($deleteWebHook);
104
105
            $output->writeln('Done');
106
        } else {
107
            throw new \InvalidArgumentException(sprintf('Mode must be exactly one of: %s', implode(', ', [self::MODE_SET, self::MODE_DELETE])));
108
        }
109
110
        return 0;
111
    }
112
}
113