Passed
Push — mini_fixes ( bd6e43 )
by Armando
02:32
created

Command::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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;
12
13
use Longman\TelegramBot\DB;
14
use Longman\TelegramBot\Entities\CallbackQuery;
15
use Longman\TelegramBot\Entities\ChosenInlineResult;
16
use Longman\TelegramBot\Entities\InlineQuery;
17
use Longman\TelegramBot\Entities\Message;
18
use Longman\TelegramBot\Entities\Payments\PreCheckoutQuery;
19
use Longman\TelegramBot\Entities\Payments\ShippingQuery;
20
use Longman\TelegramBot\Entities\Poll;
21
use Longman\TelegramBot\Entities\ServerResponse;
22
use Longman\TelegramBot\Entities\Update;
23
use Longman\TelegramBot\Exception\TelegramException;
24
use Longman\TelegramBot\Request;
25
use Longman\TelegramBot\Telegram;
26
27
/**
28
 * Class Command
29
 *
30
 * Base class for commands. It includes some helper methods that can fetch data directly from the Update object.
31
 *
32
 * @method Message             getMessage()            Optional. New incoming message of any kind — text, photo, sticker, etc.
33
 * @method Message             getEditedMessage()      Optional. New version of a message that is known to the bot and was edited
34
 * @method Message             getChannelPost()        Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
35
 * @method Message             getEditedChannelPost()  Optional. New version of a post in the channel that is known to the bot and was edited
36
 * @method InlineQuery         getInlineQuery()        Optional. New incoming inline query
37
 * @method ChosenInlineResult  getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
38
 * @method CallbackQuery       getCallbackQuery()      Optional. New incoming callback query
39
 * @method ShippingQuery       getShippingQuery()      Optional. New incoming shipping query. Only for invoices with flexible price
40
 * @method PreCheckoutQuery    getPreCheckoutQuery()   Optional. New incoming pre-checkout query. Contains full information about checkout
41
 * @method Poll                getPoll()               Optional. New poll state. Bots receive only updates about polls, which are sent or stopped by the bot
42
 */
43
abstract class Command
44
{
45
    /**
46
     * Telegram object
47
     *
48
     * @var Telegram
49
     */
50
    protected $telegram;
51
52
    /**
53
     * Update object
54
     *
55
     * @var Update
56
     */
57
    protected $update;
58
59
    /**
60
     * Name
61
     *
62
     * @var string
63
     */
64
    protected $name = '';
65
66
    /**
67
     * Description
68
     *
69
     * @var string
70
     */
71
    protected $description = 'Command description';
72
73
    /**
74
     * Usage
75
     *
76
     * @var string
77
     */
78
    protected $usage = 'Command usage';
79
80
    /**
81
     * Show in Help
82
     *
83
     * @var bool
84
     */
85
    protected $show_in_help = true;
86
87
    /**
88
     * Version
89
     *
90
     * @var string
91
     */
92
    protected $version = '1.0.0';
93
94
    /**
95
     * If this command is enabled
96
     *
97
     * @var boolean
98
     */
99
    protected $enabled = true;
100
101
    /**
102
     * If this command needs mysql
103
     *
104
     * @var boolean
105
     */
106
    protected $need_mysql = false;
107
108
    /*
109
    * Make sure this command only executes on a private chat.
110
    *
111
    * @var bool
112
    */
113
    protected $private_only = false;
114
115
    /**
116
     * Command config
117
     *
118
     * @var array
119
     */
120
    protected $config = [];
121
122
    /**
123
     * Constructor
124
     *
125
     * @param Telegram $telegram
126
     * @param Update   $update
127
     */
128 15
    public function __construct(Telegram $telegram, Update $update = null)
129
    {
130 15
        $this->telegram = $telegram;
131 15
        $this->setUpdate($update);
132 15
        $this->config = $telegram->getCommandConfig($this->name);
133 15
    }
134
135
    /**
136
     * Set update object
137
     *
138
     * @param Update $update
139
     *
140
     * @return Command
141
     */
142 15
    public function setUpdate(Update $update = null)
143
    {
144 15
        if ($update !== null) {
145 1
            $this->update = $update;
146
        }
147
148 15
        return $this;
149
    }
150
151
    /**
152
     * Pre-execute command
153
     *
154
     * @return ServerResponse
155
     * @throws TelegramException
156
     */
157
    public function preExecute()
158
    {
159
        if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) {
160
            return $this->executeNoDb();
161
        }
162
163
        if ($this->isPrivateOnly() && $this->removeNonPrivateMessage()) {
164
            $message = $this->getMessage();
165
166
            if ($user = $message->getFrom()) {
167
                return Request::sendMessage([
168
                    'chat_id'    => $user->getId(),
169
                    'parse_mode' => 'Markdown',
170
                    'text'       => sprintf(
171
                        "/%s command is only available in a private chat.\n(`%s`)",
172
                        $this->getName(),
173
                        $message->getText()
174
                    ),
175
                ]);
176
            }
177
178
            return Request::emptyResponse();
179
        }
180
181
        return $this->execute();
182
    }
183
184
    /**
185
     * Execute command
186
     *
187
     * @return ServerResponse
188
     * @throws TelegramException
189
     */
190
    abstract public function execute();
191
192
    /**
193
     * Execution if MySQL is required but not available
194
     *
195
     * @return ServerResponse
196
     * @throws TelegramException
197
     */
198
    public function executeNoDb()
199
    {
200
        return $this->replyToChat('Sorry no database connection, unable to execute "' . $this->name . '" command.');
201
    }
202
203
    /**
204
     * Get update object
205
     *
206
     * @return Update
207
     */
208 1
    public function getUpdate()
209
    {
210 1
        return $this->update;
211
    }
212
213
    /**
214
     * Relay any non-existing function calls to Update object.
215
     *
216
     * This is purely a helper method to make requests from within execute() method easier.
217
     *
218
     * @param string $name
219
     * @param array  $arguments
220
     *
221
     * @return Command
222
     */
223 1
    public function __call($name, array $arguments)
224
    {
225 1
        if ($this->update === null) {
226 1
            return null;
227
        }
228 1
        return call_user_func_array([$this->update, $name], $arguments);
229
    }
230
231
    /**
232
     * Get command config
233
     *
234
     * Look for config $name if found return it, if not return null.
235
     * If $name is not set return all set config.
236
     *
237
     * @param string|null $name
238
     *
239
     * @return array|mixed|null
240
     */
241 1
    public function getConfig($name = null)
242
    {
243 1
        if ($name === null) {
244 1
            return $this->config;
245
        }
246 1
        if (isset($this->config[$name])) {
247 1
            return $this->config[$name];
248
        }
249
250 1
        return null;
251
    }
252
253
    /**
254
     * Get telegram object
255
     *
256
     * @return Telegram
257
     */
258 1
    public function getTelegram()
259
    {
260 1
        return $this->telegram;
261
    }
262
263
    /**
264
     * Get usage
265
     *
266
     * @return string
267
     */
268 1
    public function getUsage()
269
    {
270 1
        return $this->usage;
271
    }
272
273
    /**
274
     * Get version
275
     *
276
     * @return string
277
     */
278 1
    public function getVersion()
279
    {
280 1
        return $this->version;
281
    }
282
283
    /**
284
     * Get description
285
     *
286
     * @return string
287
     */
288 1
    public function getDescription()
289
    {
290 1
        return $this->description;
291
    }
292
293
    /**
294
     * Get name
295
     *
296
     * @return string
297
     */
298 1
    public function getName()
299
    {
300 1
        return $this->name;
301
    }
302
303
    /**
304
     * Get Show in Help
305
     *
306
     * @return bool
307
     */
308 1
    public function showInHelp()
309
    {
310 1
        return $this->show_in_help;
311
    }
312
313
    /**
314
     * Check if command is enabled
315
     *
316
     * @return bool
317
     */
318 1
    public function isEnabled()
319
    {
320 1
        return $this->enabled;
321
    }
322
323
    /**
324
     * If this command is intended for private chats only.
325
     *
326
     * @return bool
327
     */
328
    public function isPrivateOnly()
329
    {
330
        return $this->private_only;
331
    }
332
333
    /**
334
     * If this is a SystemCommand
335
     *
336
     * @return bool
337
     */
338
    public function isSystemCommand()
339
    {
340
        return ($this instanceof SystemCommand);
341
    }
342
343
    /**
344
     * If this is an AdminCommand
345
     *
346
     * @return bool
347
     */
348
    public function isAdminCommand()
349
    {
350
        return ($this instanceof AdminCommand);
351
    }
352
353
    /**
354
     * If this is a UserCommand
355
     *
356
     * @return bool
357
     */
358
    public function isUserCommand()
359
    {
360
        return ($this instanceof UserCommand);
361
    }
362
363
    /**
364
     * Delete the current message if it has been called in a non-private chat.
365
     *
366
     * @return bool
367
     */
368
    protected function removeNonPrivateMessage()
369
    {
370
        $message = $this->getMessage() ?: $this->getEditedMessage();
371
372
        if ($message) {
0 ignored issues
show
introduced by
$message is of type Longman\TelegramBot\Entities\Message, thus it always evaluated to true.
Loading history...
373
            $chat = $message->getChat();
374
375
            if (!$chat->isPrivateChat()) {
376
                // Delete the falsely called command message.
377
                Request::deleteMessage([
378
                    'chat_id'    => $chat->getId(),
379
                    'message_id' => $message->getMessageId(),
380
                ]);
381
382
                return true;
383
            }
384
        }
385
386
        return false;
387
    }
388
389
    /**
390
     * Helper to reply to a chat directly.
391
     *
392
     * @param string $text
393
     * @param array  $data
394
     *
395
     * @return ServerResponse
396
     * @throws TelegramException
397
     */
398
    public function replyToChat($text, array $data = [])
399
    {
400
        if ($message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost()) {
401
            return Request::sendMessage(array_merge([
402
                'chat_id' => $message->getChat()->getId(),
403
                'text'    => $text,
404
            ], $data));
405
        }
406
407
        return Request::emptyResponse();
408
    }
409
410
    /**
411
     * Helper to reply to a user directly.
412
     *
413
     * @param string $text
414
     * @param array  $data
415
     *
416
     * @return ServerResponse
417
     * @throws TelegramException
418
     */
419
    public function replyToUser($text, array $data = [])
420
    {
421
        if ($message = $this->getMessage() ?: $this->getEditedMessage()) {
422
            return Request::sendMessage(array_merge([
423
                'chat_id' => $message->getFrom()->getId(),
424
                'text'    => $text,
425
            ], $data));
426
        }
427
428
        return Request::emptyResponse();
429
    }
430
}
431