Test Setup Failed
Push — master ( ac281b...979859 )
by Alexey
04:58 queued 02:46
created

TelegramSendMessageCommand::execute()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.0968
c 0
b 0
f 0
cc 5
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\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\{InputArgument, InputInterface, InputOption};
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class TelegramSendMessageCommand extends Command
11
{
12
    /** @var MessageSender */
13
    private $messenger;
14
15
    public function __construct(MessageSender $messenger)
16
    {
17
        $this->messenger = $messenger;
18
19
        parent::__construct();
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('telegram:send-message')
29
            ->setDescription('Send message via Telegram')
30
            ->addOption('chat-id', 'c', InputOption::VALUE_OPTIONAL, 'ID of the chat')
31
            ->addOption('stdin', 'i', InputOption::VALUE_NONE, 'Read message from stdin instead of option')
32
            ->addArgument('message', InputArgument::OPTIONAL, 'Text of the message')
33
        ;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $output->writeln('Sending message...');
42
43
        if ($input->getOption('stdin')) {
44
            $message = file_get_contents('php://stdin');
45
        } elseif (null !== $input->getArgument('message')) {
46
            $message = $input->getArgument('message');
47
        } else {
48
            $output->writeln('<error>Either \'--stdin\' option or \'message\' argument should be specified.</error>');
49
50
            return 1;
51
        }
52
53
        if (mb_strlen($message) > 4096) {
54
            $output->writeln('<comment>Message is too long (>4096). Cutting the tail...</comment>');
55
            $message = mb_substr($message, 0, 4090).PHP_EOL.'...';
56
        }
57
58
        try {
59
            $this->messenger->sendMessageToChat(
60
                (int) $input->getOption('chat-id'),
61
                $message
0 ignored issues
show
Bug introduced by
It seems like $message defined by $input->getArgument('message') on line 46 can also be of type array<integer,string>; however, Skobkin\Bundle\PointTool...er::sendMessageToChat() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
62
            );
63
        } catch (\Exception $e) {
64
            $output->writeln('Error: '.$e->getMessage());
65
66
            return 1;
67
        }
68
69
        return 0;
70
    }
71
}
72