Command.php 5.24 KB
Newer Older
LONGMAN's avatar
LONGMAN committed
1
<?php
MBoretto's avatar
MBoretto committed
2
/**
LONGMAN's avatar
LONGMAN committed
3
 * This file is part of the TelegramBot package.
LONGMAN's avatar
LONGMAN committed
4 5 6 7 8
 *
 * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
9 10
 */

11
namespace Longman\TelegramBot\Commands;
LONGMAN's avatar
LONGMAN committed
12

MBoretto's avatar
MBoretto committed
13
use Longman\TelegramBot\DB;
14
use Longman\TelegramBot\Request;
15
use Longman\TelegramBot\Telegram;
16
use Longman\TelegramBot\Entities\Chat;
LONGMAN's avatar
LONGMAN committed
17
use Longman\TelegramBot\Entities\Update;
MBoretto's avatar
MBoretto committed
18
use Longman\TelegramBot\Entities\User;
LONGMAN's avatar
LONGMAN committed
19

MBoretto's avatar
MBoretto committed
20
/**
21
 * Abstract Command Class
MBoretto's avatar
MBoretto committed
22
 */
LONGMAN's avatar
LONGMAN committed
23 24
abstract class Command
{
25 26 27 28 29
    /**
     * Telegram object
     *
     * @var Telegram
     */
30
    protected $telegram;
31 32 33 34 35 36

    /**
     * Update object
     *
     * @var Entities\Update
     */
37
    protected $update;
38 39 40 41 42 43

    /**
     * Message object
     *
     * @var Entities\Message
     */
44
    protected $message;
45 46 47 48 49 50 51 52 53 54 55 56 57

    /**
     * Name
     *
     * @var string
     */
    protected $name = '';

    /**
     * Description
     *
     * @var string
     */
58
    protected $description = 'Command description';
59 60 61 62 63 64

    /**
     * Usage
     *
     * @var string
     */
65
    protected $usage = 'Command usage';
66 67 68 69 70 71

    /**
     * Version
     *
     * @var string
     */
72
    protected $version = '1.0.0';
73 74 75 76 77 78

    /**
     * If this command is enabled
     *
     * @var boolean
     */
79 80
    protected $enabled = true;

81 82 83 84 85 86
    /**
     * If this command needs mysql
     *
     * @var boolean
     */
    protected $need_mysql = false;
87

88 89 90 91 92
    /**
     * Command config
     *
     * @var array
     */
93
    protected $config = [];
94

95 96 97
    /**
     * Constructor
     *
98 99
     * @param Telegram        $telegram
     * @param Entities\Update $update
100
     */
101
    public function __construct(Telegram $telegram, Update $update = null)
102 103
    {
        $this->telegram = $telegram;
104
        $this->setUpdate($update);
105
        $this->config = $telegram->getCommandConfig($this->name);
106 107
    }

108 109 110 111 112 113
    /**
     * Set update object
     *
     * @param Entities\Update $update
     * @return Command
     */
114
    public function setUpdate(Update $update = null)
115
    {
116 117 118 119
        if (!empty($update)) {
            $this->update = $update;
            $this->message = $this->update->getMessage();
        }
120 121 122
        return $this;
    }

123 124 125
    /**
     * Pre-execute command
     *
126
     * @return Entities\ServerResponse
127
     */
128 129
    public function preExecute()
    {
130 131
        if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) {
            return $this->executeNoDB();
132
        }
133
        return $this->execute();
134 135
    }

136 137
    /**
     * Execute command
138 139
     *
     * @return Entities\ServerResponse
140
     */
141 142
    abstract public function execute();

143
    /**
144 145
     * Execution if MySQL is required but not available
     *
146
     * @return Entities\ServerResponse
147
     */
MBoretto's avatar
MBoretto committed
148
    public function executeNoDB()
149
    {
150 151 152 153 154 155 156 157
        //Preparing message
        $message = $this->getMessage();
        $chat_id = $message->getChat()->getId();

        $data = [
            'chat_id' => $chat_id,
            'text'    => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
        ];
158

159
        return Request::sendMessage($data);
160 161
    }

162 163 164 165 166
    /**
     * Get update object
     *
     * @return Entities\Update
     */
167 168 169 170 171
    public function getUpdate()
    {
        return $this->update;
    }

172 173 174 175 176
    /**
     * Get message object
     *
     * @return Entities\Message
     */
177 178 179 180 181
    public function getMessage()
    {
        return $this->message;
    }

182 183 184
    /**
     * Get command config
     *
185
     * Look for config $name if found return it, if not return null.
186
     * If $name is not set return all set config.
187
     *
MBoretto's avatar
MBoretto committed
188
     * @param string|null $name
189
     *
190 191
     * @return mixed
     */
192 193
    public function getConfig($name = null)
    {
194 195 196
        if ($name === null) {
            return $this->config;
        }
197 198 199
        if (isset($this->config[$name])) {
            return $this->config[$name];
        }
200
        return null;
201 202
    }

203 204 205 206 207
    /**
     * Get telegram object
     *
     * @return Telegram
     */
208 209 210 211 212
    public function getTelegram()
    {
        return $this->telegram;
    }

213 214 215 216 217
    /**
     * Get usage
     *
     * @return string
     */
218 219 220 221 222
    public function getUsage()
    {
        return $this->usage;
    }

223 224 225 226 227
    /**
     * Get version
     *
     * @return string
     */
228 229 230 231 232
    public function getVersion()
    {
        return $this->version;
    }

233 234 235 236 237
    /**
     * Get description
     *
     * @return string
     */
238 239 240 241 242
    public function getDescription()
    {
        return $this->description;
    }

243 244 245 246 247
    /**
     * Get name
     *
     * @return string
     */
248 249 250 251 252
    public function getName()
    {
        return $this->name;
    }

253 254 255 256 257
    /**
     * Check if command is enabled
     *
     * @return boolean
     */
258 259 260 261
    public function isEnabled()
    {
        return $this->enabled;
    }
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

    /**
     * If this is a SystemCommand
     *
     * @return bool
     */
    public function isSystemCommand()
    {
        return ($this instanceof SystemCommand);
    }

    /**
     * If this is an AdminCommand
     *
     * @return bool
     */
    public function isAdminCommand()
    {
        return ($this instanceof AdminCommand);
    }

    /**
     * If this is a UserCommand
     *
     * @return bool
     */
    public function isUserCommand()
    {
        return ($this instanceof UserCommand);
    }
LONGMAN's avatar
LONGMAN committed
292
}