Total Complexity | 43 |
Total Lines | 404 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like Command often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Command, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
51 | abstract class Command |
||
52 | { |
||
53 | /** |
||
54 | * Auth level for user commands |
||
55 | */ |
||
56 | public const AUTH_USER = 'User'; |
||
57 | |||
58 | /** |
||
59 | * Auth level for system commands |
||
60 | */ |
||
61 | public const AUTH_SYSTEM = 'System'; |
||
62 | |||
63 | /** |
||
64 | * Auth level for admin commands |
||
65 | */ |
||
66 | public const AUTH_ADMIN = 'Admin'; |
||
67 | |||
68 | /** |
||
69 | * Telegram object |
||
70 | * |
||
71 | * @var Telegram |
||
72 | */ |
||
73 | protected $telegram; |
||
74 | |||
75 | /** |
||
76 | * Update object |
||
77 | * |
||
78 | * @var Update |
||
79 | */ |
||
80 | protected $update; |
||
81 | |||
82 | /** |
||
83 | * Name |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $name = ''; |
||
88 | |||
89 | /** |
||
90 | * Description |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $description = 'Command description'; |
||
95 | |||
96 | /** |
||
97 | * Usage |
||
98 | * |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $usage = 'Command usage'; |
||
102 | |||
103 | /** |
||
104 | * Show in Help |
||
105 | * |
||
106 | * @var bool |
||
107 | */ |
||
108 | protected $show_in_help = true; |
||
109 | |||
110 | /** |
||
111 | * Version |
||
112 | * |
||
113 | * @var string |
||
114 | */ |
||
115 | protected $version = '1.0.0'; |
||
116 | |||
117 | /** |
||
118 | * If this command is enabled |
||
119 | * |
||
120 | * @var bool |
||
121 | */ |
||
122 | protected $enabled = true; |
||
123 | |||
124 | /** |
||
125 | * If this command needs mysql |
||
126 | * |
||
127 | * @var bool |
||
128 | */ |
||
129 | protected $need_mysql = false; |
||
130 | |||
131 | /** |
||
132 | * Make sure this command only executes on a private chat. |
||
133 | * |
||
134 | * @var bool |
||
135 | */ |
||
136 | protected $private_only = false; |
||
137 | |||
138 | /** |
||
139 | * Command config |
||
140 | * |
||
141 | * @var array |
||
142 | */ |
||
143 | protected $config = []; |
||
144 | |||
145 | /** |
||
146 | * Constructor |
||
147 | * |
||
148 | * @param Telegram $telegram |
||
149 | * @param Update|null $update |
||
150 | */ |
||
151 | public function __construct(Telegram $telegram, ?Update $update = null) |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Set update object |
||
162 | * |
||
163 | * @param Update $update |
||
164 | * |
||
165 | * @return Command |
||
166 | */ |
||
167 | public function setUpdate(Update $update): Command |
||
168 | { |
||
169 | $this->update = $update; |
||
170 | |||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Pre-execute command |
||
176 | * |
||
177 | * @return ServerResponse |
||
178 | * @throws TelegramException |
||
179 | */ |
||
180 | public function preExecute(): ServerResponse |
||
181 | { |
||
182 | if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) { |
||
183 | return $this->executeNoDb(); |
||
184 | } |
||
185 | |||
186 | if ($this->isPrivateOnly() && $this->removeNonPrivateMessage()) { |
||
187 | $message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost(); |
||
188 | |||
189 | if ($user = $message->getFrom()) { |
||
190 | return Request::sendMessage([ |
||
191 | 'chat_id' => $user->getId(), |
||
192 | 'parse_mode' => 'Markdown', |
||
193 | 'text' => sprintf( |
||
194 | "/%s command is only available in a private chat.\n(`%s`)", |
||
195 | $this->getName(), |
||
196 | $message->getText() |
||
197 | ), |
||
198 | ]); |
||
199 | } |
||
200 | |||
201 | return Request::emptyResponse(); |
||
202 | } |
||
203 | |||
204 | return $this->execute(); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Execute command |
||
209 | * |
||
210 | * @return ServerResponse |
||
211 | * @throws TelegramException |
||
212 | */ |
||
213 | abstract public function execute(): ServerResponse; |
||
214 | |||
215 | /** |
||
216 | * Execution if MySQL is required but not available |
||
217 | * |
||
218 | * @return ServerResponse |
||
219 | * @throws TelegramException |
||
220 | */ |
||
221 | public function executeNoDb(): ServerResponse |
||
222 | { |
||
223 | return $this->replyToChat('Sorry no database connection, unable to execute "' . $this->name . '" command.'); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Get update object |
||
228 | * |
||
229 | * @return Update|null |
||
230 | */ |
||
231 | public function getUpdate(): ?Update |
||
232 | { |
||
233 | return $this->update; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Relay any non-existing function calls to Update object. |
||
238 | * |
||
239 | * This is purely a helper method to make requests from within execute() method easier. |
||
240 | * |
||
241 | * @param string $name |
||
242 | * @param array $arguments |
||
243 | * |
||
244 | * @return Command |
||
245 | */ |
||
246 | public function __call(string $name, array $arguments) |
||
247 | { |
||
248 | if ($this->update === null) { |
||
249 | return null; |
||
250 | } |
||
251 | return call_user_func_array([$this->update, $name], $arguments); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Get command config |
||
256 | * |
||
257 | * Look for config $name if found return it, if not return $default. |
||
258 | * If $name is not set return all set config. |
||
259 | * |
||
260 | * @param string|null $name |
||
261 | * @param mixed $default |
||
262 | * |
||
263 | * @return mixed |
||
264 | */ |
||
265 | public function getConfig(?string $name = null, $default = null) |
||
266 | { |
||
267 | if ($name === null) { |
||
268 | return $this->config; |
||
269 | } |
||
270 | return $this->config[$name] ?? $default; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get telegram object |
||
275 | * |
||
276 | * @return Telegram |
||
277 | */ |
||
278 | public function getTelegram(): Telegram |
||
279 | { |
||
280 | return $this->telegram; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Get usage |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getUsage(): string |
||
289 | { |
||
290 | return $this->usage; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Get version |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | public function getVersion(): string |
||
299 | { |
||
300 | return $this->version; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Get description |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getDescription(): string |
||
309 | { |
||
310 | return $this->description; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Get name |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | public function getName(): string |
||
319 | { |
||
320 | return $this->name; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Get Show in Help |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | public function showInHelp(): bool |
||
329 | { |
||
330 | return $this->show_in_help; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Check if command is enabled |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | public function isEnabled(): bool |
||
339 | { |
||
340 | return $this->enabled; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * If this command is intended for private chats only. |
||
345 | * |
||
346 | * @return bool |
||
347 | */ |
||
348 | public function isPrivateOnly(): bool |
||
349 | { |
||
350 | return $this->private_only; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * If this is a SystemCommand |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function isSystemCommand(): bool |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * If this is an AdminCommand |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | public function isAdminCommand(): bool |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * If this is a UserCommand |
||
375 | * |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function isUserCommand(): bool |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Delete the current message if it has been called in a non-private chat. |
||
385 | * |
||
386 | * @return bool |
||
387 | */ |
||
388 | protected function removeNonPrivateMessage(): bool |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Helper to reply to a chat directly. |
||
411 | * |
||
412 | * @param string $text |
||
413 | * @param array $data |
||
414 | * |
||
415 | * @return ServerResponse |
||
416 | * @throws TelegramException |
||
417 | */ |
||
418 | public function replyToChat(string $text, array $data = []): ServerResponse |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Helper to reply to a user directly. |
||
438 | * |
||
439 | * @param string $text |
||
440 | * @param array $data |
||
441 | * |
||
442 | * @return ServerResponse |
||
443 | * @throws TelegramException |
||
444 | */ |
||
445 | public function replyToUser(string $text, array $data = []): ServerResponse |
||
457 |