1 | <?php |
||
18 | abstract class Command |
||
19 | { |
||
20 | /** |
||
21 | * Telegram object |
||
22 | * |
||
23 | * @var \Longman\TelegramBot\Telegram |
||
24 | */ |
||
25 | protected $telegram; |
||
26 | |||
27 | /** |
||
28 | * Update object |
||
29 | * |
||
30 | * @var \Longman\TelegramBot\Entities\Update |
||
31 | */ |
||
32 | protected $update; |
||
33 | |||
34 | /** |
||
35 | * Message object |
||
36 | * |
||
37 | * @var \Longman\TelegramBot\Entities\Message |
||
38 | */ |
||
39 | protected $message; |
||
40 | |||
41 | /** |
||
42 | * Name |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $name = ''; |
||
47 | |||
48 | /** |
||
49 | * Description |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $description = 'Command description'; |
||
54 | |||
55 | /** |
||
56 | * Usage |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $usage = 'Command usage'; |
||
61 | |||
62 | /** |
||
63 | * Show in Help |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $show_in_help = true; |
||
68 | |||
69 | /** |
||
70 | * Version |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $version = '1.0.0'; |
||
75 | |||
76 | /** |
||
77 | * If this command is enabled |
||
78 | * |
||
79 | * @var boolean |
||
80 | */ |
||
81 | protected $enabled = true; |
||
82 | |||
83 | /** |
||
84 | * If this command needs mysql |
||
85 | * |
||
86 | * @var boolean |
||
87 | */ |
||
88 | protected $need_mysql = false; |
||
89 | |||
90 | /** |
||
91 | * Command config |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $config = []; |
||
96 | |||
97 | /** |
||
98 | * Constructor |
||
99 | * |
||
100 | * @param \Longman\TelegramBot\Telegram $telegram |
||
101 | * @param \Longman\TelegramBot\Entities\Update $update |
||
102 | */ |
||
103 | 15 | public function __construct(Telegram $telegram, Update $update = null) |
|
109 | |||
110 | /** |
||
111 | * Set update object |
||
112 | * |
||
113 | * @param \Longman\TelegramBot\Entities\Update $update |
||
114 | * |
||
115 | * @return \Longman\TelegramBot\Commands\Command |
||
116 | */ |
||
117 | 15 | public function setUpdate(Update $update = null) |
|
126 | |||
127 | /** |
||
128 | * Pre-execute command |
||
129 | * |
||
130 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
131 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
132 | */ |
||
133 | public function preExecute() |
||
141 | |||
142 | /** |
||
143 | * Execute command |
||
144 | * |
||
145 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
146 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
147 | */ |
||
148 | abstract public function execute(); |
||
149 | |||
150 | /** |
||
151 | * Execution if MySQL is required but not available |
||
152 | * |
||
153 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
154 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
155 | */ |
||
156 | public function executeNoDb() |
||
157 | { |
||
158 | //Preparing message |
||
159 | $message = $this->getMessage(); |
||
160 | $chat_id = $message->getChat()->getId(); |
||
161 | |||
162 | $data = [ |
||
163 | 'chat_id' => $chat_id, |
||
164 | 'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.', |
||
165 | ]; |
||
166 | |||
167 | return Request::sendMessage($data); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Get update object |
||
172 | * |
||
173 | * @return \Longman\TelegramBot\Entities\Update |
||
174 | */ |
||
175 | 1 | public function getUpdate() |
|
179 | |||
180 | /** |
||
181 | * Get message object |
||
182 | * |
||
183 | * @return \Longman\TelegramBot\Entities\Message |
||
184 | */ |
||
185 | 1 | public function getMessage() |
|
189 | |||
190 | /** |
||
191 | * Get command config |
||
192 | * |
||
193 | * Look for config $name if found return it, if not return null. |
||
194 | * If $name is not set return all set config. |
||
195 | * |
||
196 | * @param string|null $name |
||
197 | * |
||
198 | * @return array|mixed|null |
||
199 | */ |
||
200 | 1 | public function getConfig($name = null) |
|
211 | |||
212 | /** |
||
213 | * Get telegram object |
||
214 | * |
||
215 | * @return \Longman\TelegramBot\Telegram |
||
216 | */ |
||
217 | 1 | public function getTelegram() |
|
221 | |||
222 | /** |
||
223 | * Get usage |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | 1 | public function getUsage() |
|
231 | |||
232 | /** |
||
233 | * Get version |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 1 | public function getVersion() |
|
241 | |||
242 | /** |
||
243 | * Get description |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | 1 | public function getDescription() |
|
251 | |||
252 | /** |
||
253 | * Get name |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 1 | public function getName() |
|
261 | |||
262 | /** |
||
263 | * Get Show in Help |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | 1 | public function showInHelp() |
|
271 | |||
272 | /** |
||
273 | * Check if command is enabled |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 1 | public function isEnabled() |
|
281 | |||
282 | /** |
||
283 | * If this is a SystemCommand |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function isSystemCommand() |
||
288 | { |
||
289 | return ($this instanceof SystemCommand); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * If this is an AdminCommand |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | public function isAdminCommand() |
||
301 | |||
302 | /** |
||
303 | * If this is a UserCommand |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | public function isUserCommand() |
||
311 | } |
||
312 |