Completed
Push — master ( b0833a...3fdc3c )
by Armando
03:48 queued 01:33
created

Command::showInHelp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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\Request;
15
use Longman\TelegramBot\Telegram;
16
use Longman\TelegramBot\Entities\Update;
17
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 26
    public function __construct(Telegram $telegram, Update $update = null)
104
    {
105 26
        $this->telegram = $telegram;
106 26
        $this->setUpdate($update);
107 26
        $this->config = $telegram->getCommandConfig($this->name);
108 26
    }
109
110
    /**
111
     * Set update object
112
     *
113
     * @param \Longman\TelegramBot\Entities\Update $update
114
     *
115
     * @return \Longman\TelegramBot\Commands\Command
116
     */
117 26
    public function setUpdate(Update $update = null)
118
    {
119 26
        if ($update !== null) {
120 7
            $this->update  = $update;
121 7
            $this->message = $this->update->getMessage();
122
        }
123
124 26
        return $this;
125
    }
126
127
    /**
128
     * Pre-execute command
129
     *
130
     * @return \Longman\TelegramBot\Entities\ServerResponse
131
     * @throws \Longman\TelegramBot\Exception\TelegramException
132
     */
133
    public function preExecute()
134
    {
135
        if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) {
136
            return $this->executeNoDb();
137
        }
138
139
        return $this->execute();
140
    }
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 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...
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()
176
    {
177 1
        return $this->update;
178
    }
179
180
    /**
181
     * Get message object
182
     *
183
     * @return \Longman\TelegramBot\Entities\Message
184
     */
185 7
    public function getMessage()
186
    {
187 7
        return $this->message;
188
    }
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)
201
    {
202 1
        if ($name === null) {
203 1
            return $this->config;
204
        }
205 1
        if (isset($this->config[$name])) {
206 1
            return $this->config[$name];
207
        }
208
209 1
        return null;
210
    }
211
212
    /**
213
     * Get telegram object
214
     *
215
     * @return \Longman\TelegramBot\Telegram
216
     */
217 1
    public function getTelegram()
218
    {
219 1
        return $this->telegram;
220
    }
221
222
    /**
223
     * Get usage
224
     *
225
     * @return string
226
     */
227 3
    public function getUsage()
228
    {
229 3
        return $this->usage;
230
    }
231
232
    /**
233
     * Get version
234
     *
235
     * @return string
236
     */
237 4
    public function getVersion()
238
    {
239 4
        return $this->version;
240
    }
241
242
    /**
243
     * Get description
244
     *
245
     * @return string
246
     */
247 4
    public function getDescription()
248
    {
249 4
        return $this->description;
250
    }
251
252
    /**
253
     * Get name
254
     *
255
     * @return string
256
     */
257 4
    public function getName()
258
    {
259 4
        return $this->name;
260
    }
261
262
    /**
263
     * Get Show in Help
264
     *
265
     * @return bool
266
     */
267 3
    public function showInHelp()
268
    {
269 3
        return $this->show_in_help;
270
    }
271
272
    /**
273
     * Check if command is enabled
274
     *
275
     * @return bool
276
     */
277 5
    public function isEnabled()
278
    {
279 5
        return $this->enabled;
280
    }
281
282
    /**
283
     * If this is a SystemCommand
284
     *
285
     * @return bool
286
     */
287 4
    public function isSystemCommand()
288
    {
289 4
        return ($this instanceof SystemCommand);
290
    }
291
292
    /**
293
     * If this is an AdminCommand
294
     *
295
     * @return bool
296
     */
297
    public function isAdminCommand()
298
    {
299
        return ($this instanceof AdminCommand);
300
    }
301
302
    /**
303
     * If this is a UserCommand
304
     *
305
     * @return bool
306
     */
307
    public function isUserCommand()
308
    {
309
        return ($this instanceof UserCommand);
310
    }
311
}
312