SendtoallCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 35
nc 6
nop 0
dl 0
loc 56
ccs 0
cts 30
cp 0
crap 42
rs 8.7377
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the TelegramBot 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
namespace Longman\TelegramBot\Commands\AdminCommands;
13
14
use Longman\TelegramBot\Commands\AdminCommand;
15
use Longman\TelegramBot\Entities\Message;
16
use Longman\TelegramBot\Entities\ServerResponse;
17
use Longman\TelegramBot\Exception\TelegramException;
18
use Longman\TelegramBot\Request;
19
20
/**
21
 * Admin "/sendtoall" command
22
 */
23
class SendtoallCommand extends AdminCommand
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $name = 'sendtoall';
29
30
    /**
31
     * @var string
32
     */
33
    protected $description = 'Send the message to all of the bot\'s users';
34
35
    /**
36
     * @var string
37
     */
38
    protected $usage = '/sendtoall <message to send>';
39
40
    /**
41
     * @var string
42
     */
43
    protected $version = '1.5.0';
44
45
    /**
46
     * @var bool
47
     */
48
    protected $need_mysql = true;
49
50
    /**
51
     * Execute command
52
     *
53
     * @return ServerResponse
54
     * @throws TelegramException
55
     */
56
    public function execute(): ServerResponse
57
    {
58
        $text = $this->getMessage()->getText(true);
59
60
        if ($text === '') {
61
            return $this->replyToChat('Usage: ' . $this->getUsage());
62
        }
63
64
        /** @var ServerResponse[] $results */
65
        $results = Request::sendToActiveChats(
66
            'sendMessage',     //callback function to execute (see Request.php methods)
67
            ['text' => $text], //Param to evaluate the request
68
            [
69
                'groups'      => true,
70
                'supergroups' => true,
71
                'channels'    => false,
72
                'users'       => true,
73
            ]
74
        );
75
76
        if (empty($results)) {
77
            return $this->replyToChat('No users or chats found.');
78
        }
79
80
        $total  = 0;
81
        $failed = 0;
82
83
        $text = 'Message sent to:' . PHP_EOL;
84
85
        foreach ($results as $result) {
86
            $name = '';
87
            $type = '';
88
            if ($result->isOk()) {
89
                $status = '✔️';
90
91
                /** @var Message $message */
92
                $message = $result->getResult();
93
                $chat    = $message->getChat();
94
                if ($chat->isPrivateChat()) {
95
                    $name = $chat->getFirstName();
96
                    $type = 'user';
97
                } else {
98
                    $name = $chat->getTitle();
99
                    $type = 'chat';
100
                }
101
            } else {
102
                $status = '✖️';
103
                ++$failed;
104
            }
105
            ++$total;
106
107
            $text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL;
108
        }
109
        $text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL;
110
111
        return $this->replyToChat($text);
112
    }
113
}
114