Various smaller code and DocBlock fixes.

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