Skip to content

Commit 40b431c

Browse files
reduce methods (setUpRequestParams, processUpdate) code complexity
1 parent be2678f commit 40b431c

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

src/Request.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,9 @@ private static function setUpRequestParams(array $data): array {
342342
}
343343
}
344344

345-
if ($item instanceof Entity) {
346-
$item = $item->getRawData();
347-
}
348-
349-
if (is_array($item)) {
350-
$item = json_encode($item);
351-
}
345+
$itemObject = self::getItemEncode($item);
352346

353-
$options['query'][$key] = $item;
347+
$options['query'][$key] = $itemObject;
354348
}
355349
unset($item);
356350

@@ -370,4 +364,21 @@ private static function getClient(): Client {
370364
return new Client();
371365
}
372366

367+
/**
368+
* get the item encoded
369+
* @param $item
370+
* @return string
371+
*/
372+
private static function getItemEncode($item): string {
373+
if ($item instanceof Entity) {
374+
$item = $item->getRawData();
375+
}
376+
377+
if (is_array($item)) {
378+
$item = json_encode($item);
379+
}
380+
381+
return $item;
382+
}
383+
373384
}

src/Telegram.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,7 @@ public static function processUpdate(string $input, string|null $apiKey = null):
151151
);
152152
}
153153

154-
if ($apiKey !== null && self::validateWebData($apiKey, $input)) {
155-
if (Toolkit::isUrlEncode($input)) {
156-
$web_data = Toolkit::urlDecode($input);
157-
}
158-
159-
if (Toolkit::isJson($input)) {
160-
$web_data = json_decode($input, true);
161-
}
162-
163-
if (!empty($web_data) && is_array($web_data)) {
164-
$input = json_encode([
165-
'web_app_data' => $web_data,
166-
]);
167-
}
168-
}
154+
$input = self::handleInputEncoding($apiKey, $input);
169155

170156
if (!Toolkit::isJson($input)) {
171157
throw new TelegramException(sprintf(
@@ -226,4 +212,35 @@ public static function getApiToken(): string|false
226212
return self::$api_key !== null ? (self::validateToken(self::$api_key) ? self::$api_key : false) : false;
227213
}
228214

215+
/**
216+
* Handle input encoding
217+
* from web data
218+
* @param $apiKey
219+
* @param $input
220+
* @return string
221+
*/
222+
private static function handleInputEncoding($apiKey, $input): string
223+
{
224+
if ($apiKey !== null && self::validateWebData($apiKey, $input)) {
225+
226+
$web_data = [];
227+
if (Toolkit::isUrlEncode($input)) {
228+
$web_data = Toolkit::urlDecode($input);
229+
}
230+
231+
if (Toolkit::isJson($input)) {
232+
$web_data = json_decode($input, true);
233+
}
234+
235+
236+
if (!empty($web_data) && is_array($web_data)) {
237+
$input = json_encode([
238+
'web_app_data' => $web_data,
239+
]);
240+
}
241+
}
242+
243+
return $input;
244+
}
245+
229246
}

0 commit comments

Comments
 (0)