Commit a5a91335 authored by Armando Lüscher's avatar Armando Lüscher

Initial code cleanup of `Command.php`.

No functional changes, only modifications of documentation and code layout.
parent 6eef53dc
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -7,37 +6,117 @@ ...@@ -7,37 +6,117 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot; namespace Longman\TelegramBot;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\User; use Longman\TelegramBot\Entities\User;
use Longman\TelegramBot\Entities\Chat;
abstract class Command abstract class Command
{ {
/**
* Telegram object
*
* @var Telegram
*/
protected $telegram; protected $telegram;
/**
* Update object
*
* @var Entities\Update
*/
protected $update; protected $update;
/**
* Message object
*
* @var Entities\Message
*/
protected $message; protected $message;
/**
* Command
*
* @var string
*/
protected $command; protected $command;
protected $public = false; /**
* Name
*
* @var string
*/
protected $name = '';
/**
* Description
*
* @var string
*/
protected $description = 'Command help'; protected $description = 'Command help';
/**
* Usage
*
* @var string
*/
protected $usage = 'Command usage'; protected $usage = 'Command usage';
/**
* Version
*
* @var string
*/
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $need_mysql = false;
/**
* If this command is enabled
*
* @var boolean
*/
protected $enabled = true; protected $enabled = true;
protected $name = '';
/**
* If this command is public
*
* @var boolean
*/
protected $public = false;
/**
* If this command needs mysql
*
* @var boolean
*/
protected $need_mysql = false;
/**
* Command config
*
* @var array
*/
protected $config; protected $config;
/**
* Constructor
*
* @param Telegram $telegram
*/
public function __construct(Telegram $telegram) public function __construct(Telegram $telegram)
{ {
$this->telegram = $telegram; $this->telegram = $telegram;
$this->config = $telegram->getCommandConfig($this->name); $this->config = $telegram->getCommandConfig($this->name);
} }
/**
* Set update object
*
* @param Entities\Update $update
* @return Command
*/
public function setUpdate(Update $update) public function setUpdate(Update $update)
{ {
$this->update = $update; $this->update = $update;
...@@ -45,6 +124,11 @@ abstract class Command ...@@ -45,6 +124,11 @@ abstract class Command
return $this; return $this;
} }
/**
* Pre-execute command
*
* @return mixed
*/
public function preExecute() public function preExecute()
{ {
if (!$this->need_mysql | if (!$this->need_mysql |
...@@ -55,25 +139,48 @@ abstract class Command ...@@ -55,25 +139,48 @@ abstract class Command
return $this->executeNoDB(); return $this->executeNoDB();
} }
/**
* Execute command
*/
abstract public function execute(); abstract public function execute();
// this methods is executed if $need_mysql is true /**
// but DB connection for some reason is not avaiable * This methods is executed if $need_mysql is true
* but DB connection for some reason is not avaiable
*/
public function executeNoDB() public function executeNoDB()
{ {
} }
/**
* Get update object
*
* @return Entities\Update
*/
public function getUpdate() public function getUpdate()
{ {
return $this->update; return $this->update;
} }
/**
* Get message object
*
* @return Entities\Message
*/
public function getMessage() public function getMessage()
{ {
return $this->message; return $this->message;
} }
/**
* Get command config
*
* @todo Returns need looking at!
*
* @param string $name
* @return mixed
*/
public function getConfig($name = null) public function getConfig($name = null)
{ {
if (isset($this->config[$name])) { if (isset($this->config[$name])) {
...@@ -85,42 +192,83 @@ abstract class Command ...@@ -85,42 +192,83 @@ abstract class Command
return $this->config; return $this->config;
} }
/**
* Get telegram object
*
* @return Telegram
*/
public function getTelegram() public function getTelegram()
{ {
return $this->telegram; return $this->telegram;
} }
/**
* Set command
*
* @param string $command
* @return Command
*/
public function setCommand($command) public function setCommand($command)
{ {
$this->command = $command; $this->command = $command;
return $this; return $this;
} }
/**
* Get usage
*
* @return string
*/
public function getUsage() public function getUsage()
{ {
return $this->usage; return $this->usage;
} }
/**
* Get version
*
* @return string
*/
public function getVersion() public function getVersion()
{ {
return $this->version; return $this->version;
} }
/**
* Get description
*
* @return string
*/
public function getDescription() public function getDescription()
{ {
return $this->description; return $this->description;
} }
/**
* Get name
*
* @return string
*/
public function getName() public function getName()
{ {
return $this->name; return $this->name;
} }
/**
* Check if command is enabled
*
* @return boolean
*/
public function isEnabled() public function isEnabled()
{ {
return $this->enabled; return $this->enabled;
} }
/**
* Check if command is public
*
* @return boolean
*/
public function isPublic() public function isPublic()
{ {
return $this->public; return $this->public;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment