Various smaller code and DocBlock fixes.

parent 38becee6
...@@ -18,6 +18,7 @@ use Longman\TelegramBot\Commands\Command; ...@@ -18,6 +18,7 @@ use Longman\TelegramBot\Commands\Command;
use Longman\TelegramBot\Entities\ServerResponse; use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use PDO;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
use RegexIterator; use RegexIterator;
...@@ -125,8 +126,9 @@ class Telegram ...@@ -125,8 +126,9 @@ class Telegram
/** /**
* Telegram constructor. * Telegram constructor.
* *
* @param $api_key * @param string $api_key
* @param $bot_name * @param string $bot_name
*
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function __construct($api_key, $bot_name) public function __construct($api_key, $bot_name)
...@@ -160,20 +162,24 @@ class Telegram ...@@ -160,20 +162,24 @@ class Telegram
* @param string $encoding * @param string $encoding
* *
* @return \Longman\TelegramBot\Telegram * @return \Longman\TelegramBot\Telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4') public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4')
{ {
$this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding); $this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding);
ConversationDB::initializeConversation(); ConversationDB::initializeConversation();
$this->mysql_enabled = true; $this->mysql_enabled = true;
return $this; return $this;
} }
/** /**
* Initialize Database external connection * Initialize Database external connection
* *
* @param /PDO $external_pdo_connection PDO database object * @param PDO $external_pdo_connection PDO database object
* @param string $table_prefix * @param string $table_prefix
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function enableExternalMysql($external_pdo_connection, $table_prefix = null) public function enableExternalMysql($external_pdo_connection, $table_prefix = null)
{ {
...@@ -236,7 +242,7 @@ class Telegram ...@@ -236,7 +242,7 @@ class Telegram
public function getCommandObject($command) public function getCommandObject($command)
{ {
$which = ['System']; $which = ['System'];
($this->isAdmin()) && $which[] = 'Admin'; $this->isAdmin() && $which[] = 'Admin';
$which[] = 'User'; $which[] = 'User';
foreach ($which as $auth) { foreach ($which as $auth) {
...@@ -259,6 +265,7 @@ class Telegram ...@@ -259,6 +265,7 @@ class Telegram
public function setCustomInput($input) public function setCustomInput($input)
{ {
$this->input = $input; $this->input = $input;
return $this; return $this;
} }
...@@ -289,6 +296,7 @@ class Telegram ...@@ -289,6 +296,7 @@ class Telegram
* @param int|null $timeout * @param int|null $timeout
* *
* @return \Longman\TelegramBot\Entities\ServerResponse * @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function handleGetUpdates($limit = null, $timeout = null) public function handleGetUpdates($limit = null, $timeout = null)
{ {
...@@ -304,19 +312,23 @@ class Telegram ...@@ -304,19 +312,23 @@ class Telegram
//DB Query //DB Query
$last_update = DB::selectTelegramUpdate(1); $last_update = DB::selectTelegramUpdate(1);
$last_update = reset($last_update);
//As explained in the telegram bot api documentation //As explained in the telegram bot api documentation
$offset = (isset($last_update[0]['id'])) ? $last_update[0]['id'] + 1 : null; $offset = isset($last_update['id']) ? $last_update['id'] + 1 : null;
$response = Request::getUpdates([ $response = Request::getUpdates(
'offset' => $offset, [
'limit' => $limit, 'offset' => $offset,
'timeout' => $timeout, 'limit' => $limit,
]); 'timeout' => $timeout,
]
);
if ($response->isOk()) { if ($response->isOk()) {
//Process all updates //Process all updates
foreach ((array) $response->getResult() as $result) { /** @var Update $result */
foreach ((array)$response->getResult() as $result) {
$this->processUpdate($result); $this->processUpdate($result);
} }
} }
...@@ -338,6 +350,7 @@ class Telegram ...@@ -338,6 +350,7 @@ class Telegram
if (empty($this->input)) { if (empty($this->input)) {
throw new TelegramException('Input is empty!'); throw new TelegramException('Input is empty!');
} }
$post = json_decode($this->input, true); $post = json_decode($this->input, true);
if (empty($post)) { if (empty($post)) {
throw new TelegramException('Invalid JSON!'); throw new TelegramException('Invalid JSON!');
...@@ -368,6 +381,7 @@ class Telegram ...@@ -368,6 +381,7 @@ class Telegram
* @param \Longman\TelegramBot\Entities\Update $update * @param \Longman\TelegramBot\Entities\Update $update
* *
* @return \Longman\TelegramBot\Entities\ServerResponse * @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function processUpdate(Update $update) public function processUpdate(Update $update)
{ {
...@@ -377,7 +391,7 @@ class Telegram ...@@ -377,7 +391,7 @@ class Telegram
$command = 'genericmessage'; $command = 'genericmessage';
$update_type = $this->update->getUpdateType(); $update_type = $this->update->getUpdateType();
if (in_array($update_type, ['inline_query', 'chosen_inline_result', 'callback_query', 'edited_message'])) { if (in_array($update_type, ['inline_query', 'chosen_inline_result', 'callback_query', 'edited_message'], true)) {
$command = $this->getCommandFromType($update_type); $command = $this->getCommandFromType($update_type);
} elseif ($update_type === 'message') { } elseif ($update_type === 'message') {
$message = $this->update->getMessage(); $message = $this->update->getMessage();
...@@ -403,7 +417,7 @@ class Telegram ...@@ -403,7 +417,7 @@ class Telegram
'new_chat_photo', 'new_chat_photo',
'new_chat_title', 'new_chat_title',
'supergroup_chat_created', 'supergroup_chat_created',
])) { ], true)) {
$command = $this->getCommandFromType($type); $command = $this->getCommandFromType($type);
} }
} }
...@@ -477,7 +491,7 @@ class Telegram ...@@ -477,7 +491,7 @@ class Telegram
*/ */
public function enableAdmin($admin_id) public function enableAdmin($admin_id)
{ {
if (is_int($admin_id) && $admin_id > 0 && !in_array($admin_id, $this->admins_list)) { if (is_int($admin_id) && $admin_id > 0 && !in_array($admin_id, $this->admins_list, true)) {
$this->admins_list[] = $admin_id; $this->admins_list[] = $admin_id;
} else { } else {
TelegramLog::error('Invalid value "' . $admin_id . '" for admin.'); TelegramLog::error('Invalid value "' . $admin_id . '" for admin.');
...@@ -537,7 +551,7 @@ class Telegram ...@@ -537,7 +551,7 @@ class Telegram
} }
} }
return ($user_id === null) ? false : in_array($user_id, $this->admins_list); return ($user_id === null) ? false : in_array($user_id, $this->admins_list, true);
} }
/** /**
...@@ -566,11 +580,11 @@ class Telegram ...@@ -566,11 +580,11 @@ class Telegram
{ {
if (!is_dir($path)) { if (!is_dir($path)) {
TelegramLog::error('Commands path "' . $path . '" does not exist.'); TelegramLog::error('Commands path "' . $path . '" does not exist.');
} elseif (!in_array($path, $this->commands_paths)) { } elseif (!in_array($path, $this->commands_paths, true)) {
if ($before) { if ($before) {
array_unshift($this->commands_paths, $path); array_unshift($this->commands_paths, $path);
} else { } else {
array_push($this->commands_paths, $path); $this->commands_paths[] = $path;
} }
} }
...@@ -588,7 +602,7 @@ class Telegram ...@@ -588,7 +602,7 @@ class Telegram
public function addCommandsPaths(array $paths, $before = true) public function addCommandsPaths(array $paths, $before = true)
{ {
foreach ($paths as $path) { foreach ($paths as $path) {
$this->addCommandsPath($path); $this->addCommandsPath($path, $before);
} }
return $this; return $this;
...@@ -604,6 +618,7 @@ class Telegram ...@@ -604,6 +618,7 @@ class Telegram
public function setUploadPath($path) public function setUploadPath($path)
{ {
$this->upload_path = $path; $this->upload_path = $path;
return $this; return $this;
} }
...@@ -627,6 +642,7 @@ class Telegram ...@@ -627,6 +642,7 @@ class Telegram
public function setDownloadPath($path) public function setDownloadPath($path)
{ {
$this->download_path = $path; $this->download_path = $path;
return $this; return $this;
} }
...@@ -655,6 +671,7 @@ class Telegram ...@@ -655,6 +671,7 @@ class Telegram
public function setCommandConfig($command, array $config) public function setCommandConfig($command, array $config)
{ {
$this->commands_config[$command] = $config; $this->commands_config[$command] = $config;
return $this; return $this;
} }
...@@ -777,12 +794,15 @@ class Telegram ...@@ -777,12 +794,15 @@ class Telegram
* Enable Botan.io integration * Enable Botan.io integration
* *
* @param $token * @param $token
*
* @return \Longman\TelegramBot\Telegram * @return \Longman\TelegramBot\Telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function enableBotan($token) public function enableBotan($token)
{ {
Botan::initializeBotan($token); Botan::initializeBotan($token);
$this->botan_enabled = true; $this->botan_enabled = true;
return $this; return $this;
} }
} }
...@@ -22,42 +22,42 @@ class TelegramLog ...@@ -22,42 +22,42 @@ class TelegramLog
* *
* @var \Monolog\Logger * @var \Monolog\Logger
*/ */
static protected $monolog = null; static protected $monolog;
/** /**
* Monolog instance for update * Monolog instance for update
* *
* @var \Monolog\Logger * @var \Monolog\Logger
*/ */
static protected $monolog_update = null; static protected $monolog_update;
/** /**
* Path for error log * Path for error log
* *
* @var string * @var string
*/ */
static protected $error_log_path = null; static protected $error_log_path;
/** /**
* Path for debug log * Path for debug log
* *
* @var string * @var string
*/ */
static protected $debug_log_path = null; static protected $debug_log_path;
/** /**
* Path for update log * Path for update log
* *
* @var string * @var string
*/ */
static protected $update_log_path = null; static protected $update_log_path;
/** /**
* Temporary stream handle for debug log * Temporary stream handle for debug log
* *
* @var null * @var null
*/ */
static protected $debug_log_temp_stream_handle = null; static protected $debug_log_temp_stream_handle;
/** /**
* Initialize * Initialize
...@@ -76,10 +76,10 @@ class TelegramLog ...@@ -76,10 +76,10 @@ class TelegramLog
self::$monolog = $external_monolog; self::$monolog = $external_monolog;
foreach (self::$monolog->getHandlers() as $handler) { foreach (self::$monolog->getHandlers() as $handler) {
if ($handler->getLevel() == 400) { if ($handler->getLevel() === 400) {
self::$error_log_path = true; self::$error_log_path = true;
} }
if ($handler->getLevel() == 100) { if ($handler->getLevel() === 100) {
self::$debug_log_path = true; self::$debug_log_path = true;
} }
} }
...@@ -87,6 +87,7 @@ class TelegramLog ...@@ -87,6 +87,7 @@ class TelegramLog
self::$monolog = new Logger('bot_log'); self::$monolog = new Logger('bot_log');
} }
} }
return self::$monolog; return self::$monolog;
} }
...@@ -97,6 +98,8 @@ class TelegramLog ...@@ -97,6 +98,8 @@ class TelegramLog
* *
* @return \Monolog\Logger * @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException * @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
*/ */
public static function initErrorLog($path) public static function initErrorLog($path)
{ {
...@@ -119,6 +122,8 @@ class TelegramLog ...@@ -119,6 +122,8 @@ class TelegramLog
* *
* @return \Monolog\Logger * @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException * @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
*/ */
public static function initDebugLog($path) public static function initDebugLog($path)
{ {
...@@ -148,6 +153,7 @@ class TelegramLog ...@@ -148,6 +153,7 @@ class TelegramLog
return false; return false;
} }
} }
return self::$debug_log_temp_stream_handle; return self::$debug_log_temp_stream_handle;
} }
...@@ -181,6 +187,8 @@ class TelegramLog ...@@ -181,6 +187,8 @@ class TelegramLog
* *
* @return \Monolog\Logger * @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException * @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
*/ */
public static function initUpdateLog($path) public static function initUpdateLog($path)
{ {
...@@ -200,6 +208,7 @@ class TelegramLog ...@@ -200,6 +208,7 @@ class TelegramLog
self::$monolog_update->pushHandler($update_handler); self::$monolog_update->pushHandler($update_handler);
} }
return self::$monolog; return self::$monolog;
} }
......
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