Commit 765a9c40 authored by Armando Lüscher's avatar Armando Lüscher

Initial code cleanup of `Telegram.php`.

No functional changes, only slight modifications of documentation and code layout.
This commit is mainly to discuss with the project members if this style is ok and can be further refined.
parent dfa7ee4f
......@@ -23,7 +23,6 @@ use Longman\TelegramBot\Exception\TelegramException;
*/
class Telegram
{
/**
* Version
*
......@@ -67,7 +66,7 @@ class Telegram
protected $update;
/**
* Log Requests
* Log requests
*
* @var bool
*/
......@@ -81,14 +80,14 @@ class Telegram
protected $log_path;
/**
* Upload Path
* Upload path
*
* @var string
*/
protected $upload_path;
/**
* Dowload Path
* Download path
*
* @var string
*/
......@@ -102,7 +101,7 @@ class Telegram
protected $log_verbosity;
/**
* MySQL Integration
* MySQL integration
*
* @var boolean
*/
......@@ -127,14 +126,15 @@ class Telegram
*
* @var array
*/
protected $message_types =['Message', 'Photo', 'Audio', 'Document', 'Sticker', 'Video',
protected $message_types = [
'Message', 'Photo', 'Audio', 'Document', 'Sticker', 'Video',
'Voice', 'Location', 'command', 'new_chat_participant',
'left_chat_participant', 'new_chat_title', 'delete_chat_photo',
'group_chat_created', 'supergroup_chat_created', 'channel_chat_created',
];
/**
* Admins List
* Admins list
*
* @var array
*/
......@@ -145,15 +145,14 @@ class Telegram
*
* @var boolean
*/
protected $admin_enabled = false;
/**
* Constructor
*
* @param string $api_key
* @param string $bot_name
*/
public function __construct($api_key, $bot_name)
{
if (empty($api_key)) {
......@@ -167,8 +166,8 @@ class Telegram
$this->api_key = $api_key;
$this->bot_name = $bot_name;
//Set default download and upload dir
$this->setDownloadPath(BASE_PATH . "/../Download");
$this->setUploadPath(BASE_PATH . "/../Upload");
$this->setDownloadPath(BASE_PATH . '/../Download');
$this->setUploadPath(BASE_PATH . '/../Upload');
Request::initialize($this);
}
......@@ -176,12 +175,12 @@ class Telegram
/**
* Initialize
*
* @param array credential, string table_prefix
* @param array $credential
* @param string $table_prefix
*/
public function enableMySQL(array $credential, $table_prefix = null)
{
$this->pdo = DB::initialize($credential, $this, $table_prefix);
$this->mysql_enabled = true;
}
......@@ -192,8 +191,8 @@ class Telegram
*/
public function getCommandsList()
{
$commands = [];
try {
$files = new \DirectoryIterator(BASE_PATH . '/Commands');
} catch (\Exception $e) {
......@@ -230,6 +229,7 @@ class Telegram
}
}
}
return $commands;
}
......@@ -243,7 +243,7 @@ class Telegram
public function setLogRequests($log_requests)
{
$this->log_requests = $log_requests;
//set default log verbosity
//Set default log verbosity
$this->log_verbosity = 1;
return $this;
}
......@@ -274,8 +274,6 @@ class Telegram
/**
* Get log path
*
* @param string $log_path
*
* @return string
*/
public function getLogPath()
......@@ -283,7 +281,6 @@ class Telegram
return $this->log_path;
}
/**
* Set log Verbosity
*
......@@ -303,7 +300,6 @@ class Telegram
/**
* Get log verbosity
*
*
* @return int
*/
public function getLogVerbosity()
......@@ -337,9 +333,8 @@ class Telegram
/**
* Handle getUpdates method
*
*
* @todo Complete DocBlock
*/
public function handleGetUpdates($limit = null, $timeout = null)
{
//DB Query
......@@ -358,7 +353,6 @@ class Telegram
'timeout' => $timeout
]);
if ($ServerResponse->isOk()) {
$results = '';
$n_update = count($ServerResponse->getResult());
......@@ -377,7 +371,6 @@ class Telegram
*/
public function handle()
{
$this->input = Request::getInput();
if (empty($this->input)) {
throw new TelegramException('Input is empty!');
......@@ -396,8 +389,7 @@ class Telegram
*
* @return \Longman\TelegramBot\Telegram
*/
public function processUpdate(update $update)
public function processUpdate(Update $update)
{
$update_type = $update->getUpdateType();
if ($update_type == 'message') {
......@@ -414,7 +406,7 @@ class Telegram
$user_id = $chat->getId();
if (in_array($user_id, $this->admins_list)) {
$this->addCommandsPath(BASE_PATH.'/Admin');
$this->addCommandsPath(BASE_PATH . '/Admin');
}
}
......@@ -481,6 +473,8 @@ class Telegram
/**
* Execute /command
*
* @todo Complete DocBlock
*
* @return mixed
*/
public function executeCommand($command, Update $update)
......@@ -504,6 +498,8 @@ class Telegram
/**
* Get command class
*
* @todo Complete DocBlock
*
* @return object
*/
public function getCommandClass($command, Update $update = null)
......@@ -545,7 +541,9 @@ class Telegram
return false;
}
/**
* @todo Complete DocBlock
*/
protected function sanitizeCommand($string, $capitalizeFirstCharacter = false)
{
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
......@@ -555,7 +553,7 @@ class Telegram
/**
* Enable Admin Account
*
* @param array list of admins
* @param array $admins_list List of admins
*
* @return string
*/
......@@ -565,7 +563,7 @@ class Telegram
if ($admin > 0) {
$this->admins_list[] = $admin;
} else {
throw new TelegramException('Invalid value "'.$admin.'" for admin!');
throw new TelegramException('Invalid value "' . $admin . '" for admin!');
}
}
......@@ -575,7 +573,7 @@ class Telegram
}
/**
* check id user require the db connection
* Check if user required the db connection
*
* @return bool
*/
......@@ -588,11 +586,12 @@ class Telegram
}
}
/**
* Add custom commands path
*
* @return object
* @param string $folder Custom commands path
*
* @return \Longman\TelegramBot\Telegram
*/
public function addCommandsPath($folder)
{
......@@ -603,11 +602,12 @@ class Telegram
return $this;
}
/**
* Set custom upload path
*
* @return object
* @param string $folder Custom upload path
*
* @return \Longman\TelegramBot\Telegram
*/
public function setUploadPath($folder)
{
......@@ -626,9 +626,11 @@ class Telegram
}
/**
* Set custom Download path
* Set custom download path
*
* @return object
* @param string $folder Custom download path
*
* @return \Longman\TelegramBot\Telegram
*/
public function setDownloadPath($folder)
{
......@@ -637,7 +639,7 @@ class Telegram
}
/**
* Get custom Download path
* Get custom download path
*
* @return string
*/
......@@ -649,7 +651,9 @@ class Telegram
/**
* Set command config
*
* @return object
* @todo Complete DocBlock
*
* @return \Longman\TelegramBot\Telegram
*/
public function setCommandConfig($command, array $array)
{
......@@ -667,9 +671,8 @@ class Telegram
return isset($this->commands_config[$command]) ? $this->commands_config[$command] : array();
}
/**
* Get API KEY
* Get API key
*
* @return string
*/
......@@ -679,7 +682,7 @@ class Telegram
}
/**
* Get BOT NAME
* Get Bot name
*
* @return string
*/
......@@ -701,6 +704,8 @@ class Telegram
/**
* Set Webhook for bot
*
* @todo Complete DocBlock
*
* @return string
*/
public function setWebHook($url, $path_certificate = null)
......@@ -713,7 +718,7 @@ class Telegram
if (!$result->isOk()) {
throw new TelegramException(
'Webhook was not set! Error: '.$result->getErrorCode().' '. $result->getDescription()
'Webhook was not set! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription()
);
}
......@@ -731,7 +736,7 @@ class Telegram
if (!$result->isOk()) {
throw new TelegramException(
'Webhook was not unset! Error: '.$result->getErrorCode().' '. $result->getDescription()
'Webhook was not unset! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription()
);
}
......
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