optimize retrieval
This commit is contained in:
@@ -6,7 +6,6 @@ namespace App\Shopware;
|
||||
|
||||
use RuntimeException;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
@@ -31,6 +30,16 @@ final readonly class StoreApiClient
|
||||
public function searchProducts(array $criteria): array
|
||||
{
|
||||
$url = rtrim($this->baseUrl, '/') . '/store-api/search';
|
||||
$sanitizedCriteria = $this->sanitizeValue($criteria);
|
||||
|
||||
$body = json_encode(
|
||||
$sanitizedCriteria,
|
||||
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
|
||||
);
|
||||
|
||||
if (!is_string($body)) {
|
||||
throw new RuntimeException('Failed to encode Store API criteria.');
|
||||
}
|
||||
|
||||
$response = $this->httpClient->request('POST', $url, [
|
||||
'headers' => [
|
||||
@@ -38,12 +47,13 @@ final readonly class StoreApiClient
|
||||
'Accept' => 'application/json',
|
||||
'sw-access-key' => $this->salesChannelAccessKey,
|
||||
],
|
||||
'json' => $criteria,
|
||||
'body' => $body,
|
||||
'timeout' => $this->timeoutSeconds,
|
||||
]);
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
$content = $response->getContent(false);
|
||||
$content = $this->sanitizeString($content);
|
||||
|
||||
if ($statusCode < 200 || $statusCode >= 300) {
|
||||
throw new RuntimeException(sprintf(
|
||||
@@ -61,4 +71,48 @@ final readonly class StoreApiClient
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function sanitizeValue(mixed $value): mixed
|
||||
{
|
||||
if (is_array($value)) {
|
||||
$out = [];
|
||||
|
||||
foreach ($value as $key => $item) {
|
||||
$out[$key] = $this->sanitizeValue($item);
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
if (!is_string($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return $this->sanitizeString($value);
|
||||
}
|
||||
|
||||
private function sanitizeString(string $value): string
|
||||
{
|
||||
if (preg_match('//u', $value) === 1) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (function_exists('mb_convert_encoding')) {
|
||||
$value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
|
||||
}
|
||||
|
||||
if (preg_match('//u', $value) === 1) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (function_exists('iconv')) {
|
||||
$converted = @iconv('UTF-8', 'UTF-8//IGNORE', $value);
|
||||
|
||||
if (is_string($converted) && $converted !== '') {
|
||||
return $converted;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user