Completed
Push — no_default_commands_fix_tests ( ffe966 )
by Armando
02:56
created

Command::getConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
ccs 6
cts 6
cp 1
cc 3
eloc 6
nc 3
nop 1
crap 3
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 15
    public function __construct(Telegram $telegram, Update $update = null)
104
    {
105 15
        $this->telegram = $telegram;
106 15
        $this->setUpdate($update);
107 15
        $this->config = $telegram->getCommandConfig($this->name);
108 15
    }
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)
118
    {
119 15
        if ($update !== null) {
120 1
            $this->update  = $update;
121 1
            $this->message = $this->update->getMessage();
122
        }
123
124 15
        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
    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()
176
    {
177 1
        return $this->update;
178
    }
179
180
    /**
181
     * Get message object
182
     *
183
     * @return \Longman\TelegramBot\Entities\Message
184
     */
185 1
    public function getMessage()
186
    {
187 1
        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 1
    public function getUsage()
228
    {
229 1
        return $this->usage;
230
    }
231
232
    /**
233
     * Get version
234
     *
235
     * @return string
236
     */
237 1
    public function getVersion()
238
    {
239 1
        return $this->version;
240
    }
241
242
    /**
243
     * Get description
244
     *
245
     * @return string
246
     */
247 1
    public function getDescription()
248
    {
249 1
        return $this->description;
250
    }
251
252
    /**
253
     * Get name
254
     *
255
     * @return string
256
     */
257 1
    public function getName()
258
    {
259 1
        return $this->name;
260
    }
261
262
    /**
263
     * Get Show in Help
264
     *
265
     * @return bool
266
     */
267 1
    public function showInHelp()
268
    {
269 1
        return $this->show_in_help;
270
    }
271
272
    /**
273
     * Check if command is enabled
274
     *
275
     * @return bool
276
     */
277 1
    public function isEnabled()
278
    {
279 1
        return $this->enabled;
280
    }
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()
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