Skip to content

Commit 1f0e4f5

Browse files
reduce methods (setUpRequestParams, processUpdate) code complexity
1 parent 20c89db commit 1f0e4f5

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
@@ -160,21 +160,7 @@ public static function processUpdate(string $input, string|null $apiKey = null):
160160
);
161161
}
162162

163-
if ($apiKey !== null && self::validateWebData($apiKey, $input)) {
164-
if (Toolkit::isUrlEncode($input)) {
165-
$web_data = Toolkit::urlDecode($input);
166-
}
167-
168-
if (Toolkit::isJson($input)) {
169-
$web_data = json_decode($input, true);
170-
}
171-
172-
if (!empty($web_data) && is_array($web_data)) {
173-
$input = json_encode([
174-
'web_app_data' => $web_data,
175-
]);
176-
}
177-
}
163+
$input = self::handleInputEncoding($apiKey, $input);
178164

179165
if (!Toolkit::isJson($input)) {
180166
throw new TelegramException(sprintf(
@@ -235,4 +221,35 @@ public static function getApiToken(): string|false
235221
return self::$api_key !== null ? (self::validateToken(self::$api_key) ? self::$api_key : false) : false;
236222
}
237223

224+
/**
225+
* Handle input encoding
226+
* from web data
227+
* @param $apiKey
228+
* @param $input
229+
* @return string
230+
*/
231+
private static function handleInputEncoding($apiKey, $input): string
232+
{
233+
if ($apiKey !== null && self::validateWebData($apiKey, $input)) {
234+
235+
$web_data = [];
236+
if (Toolkit::isUrlEncode($input)) {
237+
$web_data = Toolkit::urlDecode($input);
238+
}
239+
240+
if (Toolkit::isJson($input)) {
241+
$web_data = json_decode($input, true);
242+
}
243+
244+
245+
if (!empty($web_data) && is_array($web_data)) {
246+
$input = json_encode([
247+
'web_app_data' => $web_data,
248+
]);
249+
}
250+
}
251+
252+
return $input;
253+
}
254+
238255
}

0 commit comments

Comments
 (0)