Skip to content

Commit a051f40

Browse files
committed
Introducing executeCurl
1 parent f7b532d commit a051f40

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/Request.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,8 @@ public static function generateGeneralFakeServerSesponse($data = null)
109109
return $fake_response;
110110
}
111111

112-
public static function send($action, array $data = null)
112+
public static function executeCurl(i$action, array $data)
113113
{
114-
if (!in_array($action, self::$methods)) {
115-
throw new TelegramException('This methods doesn\'t exist!');
116-
}
117-
118-
if (defined('PHPUNIT_TESTSUITE')) {
119-
$fake_response = self::generateGeneralFakeServerSesponse($data);
120-
return new ServerResponse($fake_response, self::$telegram->getBotName());
121-
}
122-
123114

124115
$ch = curl_init();
125116
if ($ch === false) {
@@ -146,6 +137,21 @@ public static function send($action, array $data = null)
146137
throw new TelegramException(curl_error($ch), curl_errno($ch));
147138
}
148139
curl_close($ch);
140+
return $result;
141+
}
142+
143+
public static function send($action, array $data = null)
144+
{
145+
if (!in_array($action, self::$methods)) {
146+
throw new TelegramException('This methods doesn\'t exist!');
147+
}
148+
149+
if (defined('PHPUNIT_TESTSUITE')) {
150+
$fake_response = self::generateGeneralFakeServerSesponse($data);
151+
return new ServerResponse($fake_response, self::$telegram->getBotName());
152+
}
153+
154+
$result = self::executeCurl($action, $data);
149155

150156
if (empty($result)) {
151157
$response['ok'] = 1;
@@ -154,7 +160,7 @@ public static function send($action, array $data = null)
154160
$result =json_encode($response);
155161
}
156162
//For the getUpdate method log
157-
self::setInputRaw($result);
163+
//self::setInputRaw($result);
158164

159165
$bot_name = self::$telegram->getBotName();
160166
return new ServerResponse(json_decode($result, true), $bot_name);
@@ -211,15 +217,28 @@ public static function sendChatAction(array $data)
211217

212218
public static function getUpdates($data)
213219
{
220+
214221
if ($update = self::$telegram->getCustomUpdate()) {
215-
//Cannot be used for testing yet json update have to be converted in a ServerResponse Object
216-
self::$input = $update;
222+
self::setInputRaw($update);
217223
} else {
218-
self::$input = self::send('getUpdates', $data);
224+
225+
$result = self::executeCurl('getUpdates', $data);
226+
227+
if (empty($result)) {
228+
$response['ok'] = 1;
229+
$response['error_code'] = 1;
230+
$response['description'] = 'Empty server response';
231+
$result =json_encode($response);
232+
}
233+
self::setInputRaw($result);
234+
219235
}
220-
//In send methods is set input_raw
236+
221237
self::log();
222-
return self::$input;
238+
239+
$bot_name = self::$telegram->getBotName();
240+
return new ServerResponse(json_decode($result, true), $bot_name);
241+
223242
}
224243

225244
public static function setWebhook($url)

0 commit comments

Comments
 (0)