Completed
Push — command_update_method_shortcut... ( 9b3b6b )
by Armando
02:52
created

Command::execute()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
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\Update;
19
use Longman\TelegramBot\Request;
20
use Longman\TelegramBot\Telegram;
21
22
/**
23
 * Class Command
24
 *
25
 * Base class for commands. It includes some helper methods that can fetch data directly from the Update object.
26
 *
27
 * @method Message             getMessage()            Optional. New incoming message of any kind — text, photo, sticker, etc.
28
 * @method Message             getEditedMessage()      Optional. New version of a message that is known to the bot and was edited
29
 * @method Message             getChannelPost()        Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
30
 * @method Message             getEditedChannelPost()  Optional. New version of a post in the channel that is known to the bot and was edited
31
 * @method InlineQuery         getInlineQuery()        Optional. New incoming inline query
32
 * @method ChosenInlineResult  getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
33
 * @method CallbackQuery       getCallbackQuery()      Optional. New incoming callback query
34
 */
35
abstract class Command
36
{
37
    /**
38
     * Telegram object
39
     *
40
     * @var \Longman\TelegramBot\Telegram
41
     */
42
    protected $telegram;
43
44
    /**
45
     * Update object
46
     *
47
     * @var \Longman\TelegramBot\Entities\Update
48
     */
49
    protected $update;
50
51
    /**
52
     * Name
53
     *
54
     * @var string
55
     */
56
    protected $name = '';
57
58
    /**
59
     * Description
60
     *
61
     * @var string
62
     */
63
    protected $description = 'Command description';
64
65
    /**
66
     * Usage
67
     *
68
     * @var string
69
     */
70
    protected $usage = 'Command usage';
71
72
    /**
73
     * Show in Help
74
     *
75
     * @var bool
76
     */
77
    protected $show_in_help = true;
78
79
    /**
80
     * Version
81
     *
82
     * @var string
83
     */
84
    protected $version = '1.0.0';
85
86
    /**
87
     * If this command is enabled
88
     *
89
     * @var boolean
90
     */
91
    protected $enabled = true;
92
93
    /**
94
     * If this command needs mysql
95
     *
96
     * @var boolean
97
     */
98
    protected $need_mysql = false;
99
100
    /**
101
     * Command config
102
     *
103
     * @var array
104
     */
105
    protected $config = [];
106
107
    /**
108
     * Constructor
109
     *
110
     * @param \Longman\TelegramBot\Telegram        $telegram
111
     * @param \Longman\TelegramBot\Entities\Update $update
112
     */
113 15
    public function __construct(Telegram $telegram, Update $update = null)
114
    {
115 15
        $this->telegram = $telegram;
116 15
        $this->setUpdate($update);
117 15
        $this->config = $telegram->getCommandConfig($this->name);
118 15
    }
119
120
    /**
121
     * Set update object
122
     *
123
     * @param \Longman\TelegramBot\Entities\Update $update
124
     *
125
     * @return \Longman\TelegramBot\Commands\Command
126
     */
127 15
    public function setUpdate(Update $update = null)
128
    {
129 15
        if ($update !== null) {
130
            $this->update = $update;
131
        }
132
133 15
        return $this;
134
    }
135
136
    /**
137
     * Pre-execute command
138
     *
139
     * @return \Longman\TelegramBot\Entities\ServerResponse
140
     * @throws \Longman\TelegramBot\Exception\TelegramException
141
     */
142
    public function preExecute()
143
    {
144
        if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) {
145
            return $this->executeNoDb();
146
        }
147
148
        return $this->execute();
149
    }
150
151
    /**
152
     * Execute command
153
     *
154
     * @return \Longman\TelegramBot\Entities\ServerResponse
155
     * @throws \Longman\TelegramBot\Exception\TelegramException
156
     */
157
    abstract public function execute();
158
159
    /**
160
     * Execution if MySQL is required but not available
161
     *
162
     * @return \Longman\TelegramBot\Entities\ServerResponse
163
     * @throws \Longman\TelegramBot\Exception\TelegramException
164
     */
165
    public function executeNoDb()
166
    {
167
        //Preparing message
168
        $message = $this->getMessage();
169
        $chat_id = $message->getChat()->getId();
170
171
        $data = [
172
            'chat_id' => $chat_id,
173
            'text'    => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
174
        ];
175
176
        return Request::sendMessage($data);
177
    }
178
179
    /**
180
     * Get update object
181
     *
182
     * @return \Longman\TelegramBot\Entities\Update
183
     */
184 1
    public function getUpdate()
185
    {
186 1
        return $this->update;
187
    }
188
189
    /**
190
     * Relay any non-existing function calls to Update object.
191
     *
192
     * This is purely a helper method to make requests from within execute() method easier.
193
     *
194
     * @param string $name
195
     * @param array  $arguments
196
     *
197
     * @return Command
198
     */
199 1
    public function __call($name, array $arguments)
200
    {
201 1
        return call_user_func_array([$this->update, $name], $arguments);
202
    }
203
204
    /**
205
     * Get command config
206
     *
207
     * Look for config $name if found return it, if not return null.
208
     * If $name is not set return all set config.
209
     *
210
     * @param string|null $name
211
     *
212
     * @return array|mixed|null
213
     */
214 1
    public function getConfig($name = null)
215
    {
216 1
        if ($name === null) {
217 1
            return $this->config;
218
        }
219 1
        if (isset($this->config[$name])) {
220 1
            return $this->config[$name];
221
        }
222
223 1
        return null;
224
    }
225
226
    /**
227
     * Get telegram object
228
     *
229
     * @return \Longman\TelegramBot\Telegram
230
     */
231 1
    public function getTelegram()
232
    {
233 1
        return $this->telegram;
234
    }
235
236
    /**
237
     * Get usage
238
     *
239
     * @return string
240
     */
241 1
    public function getUsage()
242
    {
243 1
        return $this->usage;
244
    }
245
246
    /**
247
     * Get version
248
     *
249
     * @return string
250
     */
251 1
    public function getVersion()
252
    {
253 1
        return $this->version;
254
    }
255
256
    /**
257
     * Get description
258
     *
259
     * @return string
260
     */
261 1
    public function getDescription()
262
    {
263 1
        return $this->description;
264
    }
265
266
    /**
267
     * Get name
268
     *
269
     * @return string
270
     */
271 1
    public function getName()
272
    {
273 1
        return $this->name;
274
    }
275
276
    /**
277
     * Get Show in Help
278
     *
279
     * @return bool
280
     */
281 1
    public function showInHelp()
282
    {
283 1
        return $this->show_in_help;
284
    }
285
286
    /**
287
     * Check if command is enabled
288
     *
289
     * @return bool
290
     */
291 1
    public function isEnabled()
292
    {
293 1
        return $this->enabled;
294
    }
295
296
    /**
297
     * If this is a SystemCommand
298
     *
299
     * @return bool
300
     */
301
    public function isSystemCommand()
302
    {
303
        return ($this instanceof SystemCommand);
304
    }
305
306
    /**
307
     * If this is an AdminCommand
308
     *
309
     * @return bool
310
     */
311
    public function isAdminCommand()
312
    {
313
        return ($this instanceof AdminCommand);
314
    }
315
316
    /**
317
     * If this is a UserCommand
318
     *
319
     * @return bool
320
     */
321
    public function isUserCommand()
322
    {
323
        return ($this instanceof UserCommand);
324
    }
325
}
326