Remember the current action being executed and return correct objects for requests.

parent c599d545
......@@ -11,6 +11,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- Botan.io service has been discontinued.
### Removed
### Fixed
- Return correct objects for requests.
### Security
## [0.55.1] - 2019-01-06
......
......@@ -8,6 +8,9 @@
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Entities\Games\GameHighScore;
use Longman\TelegramBot\Request;
/**
* Class ServerResponse
*
......@@ -103,32 +106,27 @@ class ServerResponse extends Entity
* @param string $bot_username
*
* @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private function createResultObject($result, $bot_username)
private function createResultObject(array $result, $bot_username)
{
$action = Request::getCurrentAction();
// We don't need to save the raw_data of the response object!
$result['raw_data'] = null;
$result_object_types = [
'total_count' => 'UserProfilePhotos', //Response from getUserProfilePhotos
'stickers' => 'StickerSet', //Response from getStickerSet
'file_id' => 'File', //Response from getFile
'title' => 'Chat', //Response from getChat
'username' => 'User', //Response from getMe
'user' => 'ChatMember', //Response from getChatMember
'url' => 'WebhookInfo', //Response from getWebhookInfo
'getChat' => Chat::class,
'getChatMember' => ChatMember::class,
'getFile' => File::class,
'getMe' => User::class,
'getStickerSet' => StickerSet::class,
'getUserProfilePhotos' => UserProfilePhotos::class,
'getWebhookInfo' => WebhookInfo::class,
];
foreach ($result_object_types as $type => $object_class) {
if (isset($result[$type])) {
$object_class = __NAMESPACE__ . '\\' . $object_class;
return new $object_class($result);
}
}
$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Message::class;
//Response from sendMessage
return new Message($result, $bot_username);
return new $object_class($result, $bot_username);
}
/**
......@@ -137,28 +135,26 @@ class ServerResponse extends Entity
* @param array $result
* @param string $bot_username
*
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
* @throws \Longman\TelegramBot\Exception\TelegramException
* @return \Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Games\GameHighScore[]|\Longman\TelegramBot\Entities\Message[]|\Longman\TelegramBot\Entities\Update[]
*/
private function createResultObjects($result, $bot_username)
private function createResultObjects(array $result, $bot_username)
{
$results = [];
if (isset($result[0]['user'])) {
//Response from getChatAdministrators
foreach ($result as $user) {
// We don't need to save the raw_data of the response object!
$user['raw_data'] = null;
$action = Request::getCurrentAction();
$results[] = new ChatMember($user);
}
} else {
//Get Update
foreach ($result as $update) {
// We don't need to save the raw_data of the response object!
$update['raw_data'] = null;
$result_object_types = [
'getChatAdministrators' => ChatMember::class,
'getGameHighScores' => GameHighScore::class,
'sendMediaGroup' => Message::class,
];
$results[] = new Update($update, $bot_username);
}
$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Update::class;
foreach ($result as $data) {
// We don't need to save the raw_data of the response object!
$data['raw_data'] = null;
$results[] = new $object_class($data, $bot_username);
}
return $results;
......
......@@ -128,6 +128,13 @@ class Request
*/
private static $limiter_interval;
/**
* Get the current action that is being executed
*
* @var string
*/
private static $current_action;
/**
* Available actions to send
*
......@@ -400,6 +407,16 @@ class Request
return json_encode($item);
}
/**
* Get the current action that's being executed
*
* @return string
*/
public static function getCurrentAction()
{
return self::$current_action;
}
/**
* Execute HTTP Request
*
......@@ -432,7 +449,7 @@ class Request
TelegramLog::update($result);
}
} catch (RequestException $e) {
$result = ($e->getResponse()) ? (string) $e->getResponse()->getBody() : '';
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
} finally {
//Logging verbose debug output
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
......@@ -529,8 +546,11 @@ class Request
self::limitTelegramRequests($action, $data);
// Remember which action is currently being executed.
self::$current_action = $action;
$raw_response = self::execute($action, $data);
$response = json_decode($raw_response, true);
$response = json_decode($raw_response, true);
if (null === $response) {
TelegramLog::debug($raw_response);
......@@ -543,6 +563,9 @@ class Request
throw new InvalidBotTokenException();
}
// Reset current action after completion.
self::$current_action = null;
return $response;
}
......
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