Completed
Push — cleanup_nodb ( 5017d8...df2166 )
by Armando
03:48
created

Command   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 294
Duplicated Lines 4.42 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 68.63%

Importance

Changes 0
Metric Value
wmc 24
c 0
b 0
f 0
lcom 3
cbo 5
dl 13
loc 294
ccs 35
cts 51
cp 0.6863
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A preExecute() 0 8 4
execute() 0 1 ?
A getUpdate() 0 4 1
A setUpdate() 0 8 2
A __call() 0 7 2
A getConfig() 0 11 3
A getTelegram() 0 4 1
A getUsage() 0 4 1
A getVersion() 0 4 1
A getDescription() 0 4 1
A getName() 0 4 1
A showInHelp() 0 4 1
A isEnabled() 0 4 1
A isSystemCommand() 0 4 1
A isAdminCommand() 0 4 1
A isUserCommand() 0 4 1
A executeNoDb() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 1
            $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 View Code Duplication
    public function executeNoDb()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        if ($this->update === null) {
202 1
            return null;
203
        }
204 1
        return call_user_func_array([$this->update, $name], $arguments);
205
    }
206
207
    /**
208
     * Get command config
209
     *
210
     * Look for config $name if found return it, if not return null.
211
     * If $name is not set return all set config.
212
     *
213
     * @param string|null $name
214
     *
215
     * @return array|mixed|null
216
     */
217 1
    public function getConfig($name = null)
218
    {
219 1
        if ($name === null) {
220 1
            return $this->config;
221
        }
222 1
        if (isset($this->config[$name])) {
223 1
            return $this->config[$name];
224
        }
225
226 1
        return null;
227
    }
228
229
    /**
230
     * Get telegram object
231
     *
232
     * @return \Longman\TelegramBot\Telegram
233
     */
234 1
    public function getTelegram()
235
    {
236 1
        return $this->telegram;
237
    }
238
239
    /**
240
     * Get usage
241
     *
242
     * @return string
243
     */
244 1
    public function getUsage()
245
    {
246 1
        return $this->usage;
247
    }
248
249
    /**
250
     * Get version
251
     *
252
     * @return string
253
     */
254 1
    public function getVersion()
255
    {
256 1
        return $this->version;
257
    }
258
259
    /**
260
     * Get description
261
     *
262
     * @return string
263
     */
264 1
    public function getDescription()
265
    {
266 1
        return $this->description;
267
    }
268
269
    /**
270
     * Get name
271
     *
272
     * @return string
273
     */
274 1
    public function getName()
275
    {
276 1
        return $this->name;
277
    }
278
279
    /**
280
     * Get Show in Help
281
     *
282
     * @return bool
283
     */
284 1
    public function showInHelp()
285
    {
286 1
        return $this->show_in_help;
287
    }
288
289
    /**
290
     * Check if command is enabled
291
     *
292
     * @return bool
293
     */
294 1
    public function isEnabled()
295
    {
296 1
        return $this->enabled;
297
    }
298
299
    /**
300
     * If this is a SystemCommand
301
     *
302
     * @return bool
303
     */
304
    public function isSystemCommand()
305
    {
306
        return ($this instanceof SystemCommand);
307
    }
308
309
    /**
310
     * If this is an AdminCommand
311
     *
312
     * @return bool
313
     */
314
    public function isAdminCommand()
315
    {
316
        return ($this instanceof AdminCommand);
317
    }
318
319
    /**
320
     * If this is a UserCommand
321
     *
322
     * @return bool
323
     */
324
    public function isUserCommand()
325
    {
326
        return ($this instanceof UserCommand);
327
    }
328
}
329