SendtoallCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 98
ccs 0
cts 52
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 64 6
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Commands\AdminCommands;
12
13
use Longman\TelegramBot\Commands\AdminCommand;
14
use Longman\TelegramBot\Entities\Message;
15
use Longman\TelegramBot\Entities\ServerResponse;
16
use Longman\TelegramBot\Request;
17
18
/**
19
 * Admin "/sendtoall" command
20
 */
21
class SendtoallCommand extends AdminCommand
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $name = 'sendtoall';
27
28
    /**
29
     * @var string
30
     */
31
    protected $description = 'Send the message to all the user\'s bot';
32
33
    /**
34
     * @var string
35
     */
36
    protected $usage = '/sendtoall <message to send>';
37
38
    /**
39
     * @var string
40
     */
41
    protected $version = '1.3.0';
42
43
    /**
44
     * @var bool
45
     */
46
    protected $need_mysql = true;
47
48
    /**
49
     * Execute command
50
     *
51
     * @return boolean
52
     * @throws \Longman\TelegramBot\Exception\TelegramException
53
     */
54
    public function execute()
55
    {
56
        $message = $this->getMessage();
57
58
        $chat_id = $message->getChat()->getId();
59
        $text    = $message->getText(true);
60
61
        if ($text === '') {
62
            $text = 'Write the message to send: /sendtoall <message>';
63
        } else {
64
            $results = Request::sendToActiveChats(
65
                'sendMessage', //callback function to execute (see Request.php methods)
66
                ['text' => $text], //Param to evaluate the request
67
                true, //Send to groups (group chat)
68
                true, //Send to super groups chats (super group chat)
69
                true, //Send to users (single chat)
70
                null, //'yyyy-mm-dd hh:mm:ss' date range from
71
                null  //'yyyy-mm-dd hh:mm:ss' date range to
72
            );
73
74
            $total  = 0;
75
            $failed = 0;
76
77
            $text = 'Message sent to:' . PHP_EOL;
78
79
            /** @var ServerResponse $result */
80
            foreach ($results as $result) {
81
                $name = '';
82
                $type = '';
83
                if ($result->isOk()) {
84
                    $status = '✔️';
85
86
                    /** @var Message $message */
87
                    $message = $result->getResult();
88
                    $chat    = $message->getChat();
89
                    if ($chat->isPrivateChat()) {
90
                        $name = $chat->getFirstName();
91
                        $type = 'user';
92
                    } else {
93
                        $name = $chat->getTitle();
94
                        $type = 'chat';
95
                    }
96
                } else {
97
                    $status = '✖️';
98
                    ++$failed;
99
                }
100
                ++$total;
101
102
                $text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL;
103
            }
104
            $text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL;
105
106
            if ($total === 0) {
107
                $text = 'No users or chats found..';
108
            }
109
        }
110
111
        $data = [
112
            'chat_id' => $chat_id,
113
            'text'    => $text,
114
        ];
115
116
        return Request::sendMessage($data);
117
    }
118
}
119