Simplify some code bits.

Update code style.
parent 706fc120
......@@ -69,7 +69,7 @@ class Botan
}
$options_default = [
'timeout' => 3
'timeout' => 3,
];
$options = array_merge($options_default, $options);
......@@ -115,7 +115,7 @@ class Botan
return false;
}
if (empty($update)) {
if ($update === null) {
throw new TelegramException('Update object is empty!');
}
......@@ -133,16 +133,15 @@ class Botan
'edited_channel_post' => 'Edited Channel Post',
'inline_query' => 'Inline Query',
'chosen_inline_result' => 'Chosen Inline Result',
'callback_query' => 'Callback Query'
'callback_query' => 'Callback Query',
];
if (array_key_exists($update_type, $update_object_names)) {
$data = $update_data[$update_type];
$event_name = $update_object_names[$update_type];
if ($update_type === 'message') {
if ($update->getMessage()->getEntities()) {
foreach ($update->getMessage()->getEntities() as $entity) {
if ($update_type === 'message' && $entities = $update->getMessage()->getEntities()) {
foreach ($entities as $entity) {
if ($entity->getType() === 'bot_command' && $entity->getOffset() === 0) {
if ($command === 'generic') {
$command = 'Generic';
......@@ -158,48 +157,47 @@ class Botan
}
}
}
}
if (empty($event_name)) {
TelegramLog::error("Botan.io stats report failed, no suitable update object found!");
TelegramLog::error('Botan.io stats report failed, no suitable update object found!');
return false;
}
// In case there is no from field assign id = 0
$uid = isset($data['from']['id']) ? $data['from']['id'] : 0;
$result = null;
try {
$response = self::$client->post(
str_replace(
['#TOKEN', '#UID', '#NAME'],
[self::$token, $uid, urlencode($event_name)],
'/track?token=#TOKEN&uid=#UID&name=#NAME'
sprintf(
'/track?token=%1$s&uid=%2$s&name=%3$s',
self::$token,
$uid,
urlencode($event_name)
),
[
'headers' => [
'Content-Type' => 'application/json'
'Content-Type' => 'application/json',
],
'json' => $data
'json' => $data,
]
);
$result = (string) $response->getBody();
} catch (RequestException $e) {
$result = $e->getMessage();
} finally {
}
$responseData = json_decode($result, true);
if ($responseData['status'] !== 'accepted') {
TelegramLog::debug('Botan.io stats report failed: ' . ($result ?: 'empty response') . "\n\n");
if (!$responseData || $responseData['status'] !== 'accepted') {
TelegramLog::debug('Botan.io stats report failed: ' . ($result ?: 'empty response'));
return false;
}
return $responseData;
}
}
/**
* Url Shortener function
......@@ -226,25 +224,27 @@ class Botan
try {
$response = self::$client->post(
str_replace(
['#TOKEN', '#UID', '#URL'],
[self::$token, $user_id, urlencode($url)],
'/s/?token=#TOKEN&user_ids=#UID&url=#URL'
sprintf(
'/s?token=%1$s&user_ids=%2$s&url=%3$s',
self::$token,
$user_id,
urlencode($url)
)
);
$result = (string) $response->getBody();
} catch (RequestException $e) {
$result = $e->getMessage();
} finally {
if (filter_var($result, FILTER_VALIDATE_URL) !== false) {
BotanDB::insertShortUrl($url, $user_id, $result);
return $result;
} else {
TelegramLog::debug('Botan.io URL shortening failed for \'' . $url . '\': ' . ($result ?: 'empty response') . "\n\n");
}
if (filter_var($result, FILTER_VALIDATE_URL) === false) {
TelegramLog::debug('Botan.io URL shortening failed for "' . $url . '": ' . ($result ?: 'empty response'));
return $url;
}
}
BotanDB::insertShortUrl($url, $user_id, $result);
return $result;
}
}
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