Test Setup Failed
Push — master ( 5633a5...21224c )
by Alexey
03:00
created

TelegramSendMessageCommand::execute()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 9
nop 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Command;
4
5
use Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class TelegramSendMessageCommand extends ContainerAwareCommand
13
{
14
    /**
15
     * @var MessageSender
16
     */
17
    private $messenger;
18
19
    /**
20
     * @var int
21
     */
22
    private $logChatId;
23
24
    public function setMessenger(MessageSender $messenger): void
25
    {
26
        $this->messenger = $messenger;
27
    }
28
29
    /**
30
     * @param int $logChatId
31
     */
32
    public function setLogChatId(int $logChatId): void
33
    {
34
        $this->logChatId = $logChatId;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function configure()
41
    {
42
        $this
43
            ->setName('telegram:send-message')
44
            ->setDescription('Send message via Telegram')
45
            ->addOption('chat-id', 'c', InputOption::VALUE_OPTIONAL, 'ID of the chat')
46
            ->addOption('stdin', 'i', InputOption::VALUE_NONE, 'Read message from stdin instead of option')
47
            ->addArgument('message', InputArgument::OPTIONAL, 'Text of the message')
48
        ;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        $output->writeln('Sending message...');
57
58
        if ($input->getOption('stdin')) {
59
            $message = file_get_contents('php://stdin');
60
        } elseif (null !== $input->getArgument('message')) {
61
            $message = $input->getArgument('message');
62
        } else {
63
            $output->writeln('<error>Either \'--stdin\' option or \'message\' argument should be specified.</error>');
64
65
            return 1;
66
        }
67
68
        if (mb_strlen($message) > 4096) {
69
            $output->writeln('<comment>Message is too long (>4096). Cutting the tail...</comment>');
70
            $message = mb_substr($message, 0, 4090).PHP_EOL.'...';
71
        }
72
73
        try {
74
            $this->messenger->sendMessageToChat(
75
                (int) $input->getOption('chat-id') ?: $this->logChatId,
76
                $message
77
            );
78
        } catch (\Exception $e) {
79
            $output->writeln('Error: '.$e->getMessage());
80
81
            return 1;
82
        }
83
84
        return 0;
85
    }
86
}
87