Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
use Phptg\BotApi\ParseResult\ResultFactory;
use Phptg\BotApi\ParseResult\TelegramParseResultException;
use Phptg\BotApi\Transport\ApiResponse;
use Phptg\BotApi\Transport\HttpMethod;
use Phptg\BotApi\Transport\TransportInterface;
use Phptg\BotApi\Type\InputFile;
use Phptg\BotApi\Type\ResponseParameters;

use function array_key_exists;
use function is_array;
use function is_bool;
use function is_int;
use function is_scalar;
use function is_string;
use function json_decode;
use function strlen;

/**
* @internal
Expand Down Expand Up @@ -50,11 +54,11 @@
LogContextFactory::sendRequest($method),
);

$response = $this->transport->send(
$this->makeUrlPath($method->getApiMethod()),
$method->getData(),
$method->getHttpMethod(),
);
$url = $this->baseUrl . '/bot' . $this->token . '/' . $method->getApiMethod();
$response = match ($method->getHttpMethod()) {
HttpMethod::GET => $this->sendGetRequest($url, $method->getData()),
HttpMethod::POST => $this->sendPostRequest($url, $method->getData()),
};

try {
$decodedBody = json_decode($response->body, true, flags: JSON_THROW_ON_ERROR);
Expand Down Expand Up @@ -106,9 +110,54 @@
return $result;
}

private function makeUrlPath(string $apiMethod): string
/**
* @psalm-param array<string, mixed> $data
*/
private function sendGetRequest(string $url, array $data): ApiResponse
{
return $this->baseUrl . '/bot' . $this->token . '/' . $apiMethod;
$queryParameters = array_map(
static fn($value) => is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR),
$data,
);

if (!empty($queryParameters)) {
$url .= '?' . http_build_query($queryParameters);
}

return $this->transport->get($url);
}

/**
* @psalm-param array<string, mixed> $data
*/
private function sendPostRequest(string $url, array $data): ApiResponse
{
$files = [];
foreach ($data as $key => $value) {

Check warning on line 136 in src/Api.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "Foreach_": @@ @@ private function sendPostRequest(string $url, array $data): ApiResponse { $files = []; - foreach ($data as $key => $value) { + foreach ([] as $key => $value) { if ($value instanceof InputFile) { $files[$key] = $value; unset($data[$key]);
if ($value instanceof InputFile) {

Check warning on line 137 in src/Api.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "InstanceOf_": @@ @@ { $files = []; foreach ($data as $key => $value) { - if ($value instanceof InputFile) { + if (false) { $files[$key] = $value; unset($data[$key]); }

Check warning on line 137 in src/Api.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "InstanceOf_": @@ @@ { $files = []; foreach ($data as $key => $value) { - if ($value instanceof InputFile) { + if (true) { $files[$key] = $value; unset($data[$key]); }
$files[$key] = $value;
unset($data[$key]);
}
}

if (empty($files)) {
$content = json_encode($data, JSON_THROW_ON_ERROR);
return $this->transport->post(
$url,
$content,
[

Check warning on line 148 in src/Api.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ } if (empty($files)) { $content = json_encode($data, JSON_THROW_ON_ERROR); - return $this->transport->post($url, $content, ['Content-Length' => (string) strlen($content), 'Content-Type' => 'application/json; charset=utf-8']); + return $this->transport->post($url, $content, ['Content-Type' => 'application/json; charset=utf-8']); } $data = array_map(static fn(mixed $value) => is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR), $data); return $this->transport->postWithFiles($url, $data, $files);
'Content-Length' => (string) strlen($content),

Check warning on line 149 in src/Api.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "CastString": @@ @@ } if (empty($files)) { $content = json_encode($data, JSON_THROW_ON_ERROR); - return $this->transport->post($url, $content, ['Content-Length' => (string) strlen($content), 'Content-Type' => 'application/json; charset=utf-8']); + return $this->transport->post($url, $content, ['Content-Length' => strlen($content), 'Content-Type' => 'application/json; charset=utf-8']); } $data = array_map(static fn(mixed $value) => is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR), $data); return $this->transport->postWithFiles($url, $data, $files);
'Content-Type' => 'application/json; charset=utf-8',
],
);
}

$data = array_map(

Check warning on line 155 in src/Api.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMap": @@ @@ $content = json_encode($data, JSON_THROW_ON_ERROR); return $this->transport->post($url, $content, ['Content-Length' => (string) strlen($content), 'Content-Type' => 'application/json; charset=utf-8']); } - $data = array_map(static fn(mixed $value) => is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR), $data); + $data = $data; return $this->transport->postWithFiles($url, $data, $files); } /**
static fn(mixed $value) => is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR),

Check warning on line 156 in src/Api.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "Ternary": @@ @@ $content = json_encode($data, JSON_THROW_ON_ERROR); return $this->transport->post($url, $content, ['Content-Length' => (string) strlen($content), 'Content-Type' => 'application/json; charset=utf-8']); } - $data = array_map(static fn(mixed $value) => is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR), $data); + $data = array_map(static fn(mixed $value) => is_scalar($value) ? json_encode($value, JSON_THROW_ON_ERROR) : $value, $data); return $this->transport->postWithFiles($url, $data, $files); } /**
$data,
);

return $this->transport->postWithFiles($url, $data, $files);
}

/**
Expand Down
121 changes: 51 additions & 70 deletions src/Transport/CurlTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
use Phptg\BotApi\Curl\Curl;
use Phptg\BotApi\Curl\CurlException;
use Phptg\BotApi\Curl\CurlInterface;
use Phptg\BotApi\Type\InputFile;

use function is_int;
use function is_scalar;
use function json_encode;

/**
* @api
Expand All @@ -28,37 +25,46 @@ public function __construct(
$this->curlShareHandle = $this->createCurlShareHandle();
}

/**
* @psalm-param array<string, mixed> $data
*/
public function send(string $urlPath, array $data = [], HttpMethod $httpMethod = HttpMethod::POST): ApiResponse
public function get(string $url): ApiResponse
{
$options = match ($httpMethod) {
HttpMethod::GET => $this->createGetOptions($urlPath, $data),
HttpMethod::POST => $this->createPostOptions($urlPath, $data),
};
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_SHARE] = $this->curlShareHandle;

$curl = $this->curl->init();
$options = [
CURLOPT_HTTPGET => true,
CURLOPT_URL => $url,
];
return $this->send($options);
}

try {
$this->curl->setopt_array($curl, $options);
public function post(string $url, string $body, array $headers): ApiResponse
{
$header = [];
foreach ($headers as $name => $value) {
$header[] = $name . ': ' . $value;
}

/**
* @var string $body `curl_exec` returns string because `CURLOPT_RETURNTRANSFER` is set to `true`.
*/
$body = $this->curl->exec($curl);
$options = [
CURLOPT_POST => true,
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $header,
];
return $this->send($options);
}

$statusCode = $this->curl->getinfo($curl, CURLINFO_HTTP_CODE);
if (!is_int($statusCode)) {
$statusCode = 0;
}
} finally {
$this->curl->close($curl);
public function postWithFiles(string $url, array $data, array $files): ApiResponse
{
foreach ($files as $key => $file) {
$data[$key] = new CURLStringFile(
FileHelper::read($file),
$file->filename ?? '',
);
}

return new ApiResponse($statusCode, $body);
$options = [
CURLOPT_POST => true,
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $data,
];
return $this->send($options);
}

public function downloadFile(string $url): string
Expand Down Expand Up @@ -128,55 +134,30 @@ static function (int $errorNumber, string $errorString): bool {
}
}

/**
* @psalm-param array<string, mixed> $data
*/
private function createPostOptions(string $urlPath, array $data): array
private function send(array $options): ApiResponse
{
$postFields = [];
foreach ($data as $key => $value) {
if (is_scalar($value)) {
$postFields[$key] = $value;
continue;
}

if ($value instanceof InputFile) {
$postFields[$key] = new CURLStringFile(
FileHelper::read($value),
$value->filename ?? '',
);
continue;
}
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_SHARE] = $this->curlShareHandle;

$postFields[$key] = json_encode($value, JSON_THROW_ON_ERROR);
}
$curl = $this->curl->init();

return [
CURLOPT_POST => true,
CURLOPT_URL => $urlPath,
CURLOPT_POSTFIELDS => $postFields,
];
}
try {
$this->curl->setopt_array($curl, $options);

/**
* @psalm-param array<string, mixed> $data
*/
private function createGetOptions(string $urlPath, array $data): array
{
$queryParameters = [];
foreach ($data as $key => $value) {
$queryParameters[$key] = is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR);
}
/**
* @var string $body `curl_exec` returns string because `CURLOPT_RETURNTRANSFER` is set to `true`.
*/
$body = $this->curl->exec($curl);

$url = $urlPath;
if (!empty($queryParameters)) {
$url .= '?' . http_build_query($queryParameters);
$statusCode = $this->curl->getinfo($curl, CURLINFO_HTTP_CODE);
if (!is_int($statusCode)) {
$statusCode = 0;
}
} finally {
$this->curl->close($curl);
}

return [
CURLOPT_HTTPGET => true,
CURLOPT_URL => $url,
];
return new ApiResponse($statusCode, $body);
}

private function createCurlShareHandle(): CurlShareHandle
Expand Down
Loading
Loading