add new configs
This commit is contained in:
@@ -8,7 +8,6 @@ use App\Commerce\Dto\ShopProductResult;
|
||||
use App\Shopware\ShopwareCriteriaBuilder;
|
||||
use App\Shopware\StoreApiClient;
|
||||
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;
|
||||
@@ -16,14 +15,13 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
final readonly class ShopSearchService
|
||||
{
|
||||
public function __construct(
|
||||
private CommerceQueryParser $queryParser,
|
||||
private CommerceQueryParser $queryParser,
|
||||
private ShopwareCriteriaBuilder $criteriaBuilder,
|
||||
private StoreApiClient $storeApiClient,
|
||||
private bool $enabled = true,
|
||||
private int $maxResults = 25,
|
||||
private string $baseUrl
|
||||
)
|
||||
{
|
||||
private StoreApiClient $storeApiClient,
|
||||
private bool $enabled = true,
|
||||
private int $maxResults = 25,
|
||||
private string $baseUrl
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,18 +34,17 @@ final readonly class ShopSearchService
|
||||
}
|
||||
|
||||
$response = [];
|
||||
|
||||
$query = $this->queryParser->parse($originalPrompt, $commerceIntent);
|
||||
$criteria = $this->criteriaBuilder->build($query, $this->maxResults);
|
||||
|
||||
try {
|
||||
$response = $this->storeApiClient->searchProducts($criteria);
|
||||
} catch (ClientExceptionInterface|DecodingExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $e) {
|
||||
} catch (ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $e) {
|
||||
|
||||
}
|
||||
|
||||
$result = $this->mapProducts($response);;
|
||||
|
||||
return $result;
|
||||
return $this->mapProducts($response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,42 +65,44 @@ final readonly class ShopSearchService
|
||||
}
|
||||
|
||||
$results[] = new ShopProductResult(
|
||||
id: (string)($row['id'] ?? ''),
|
||||
name: trim((string)($row['translated']['name'] ?? '')),
|
||||
productNumber: isset($row['productNumber']) ? (string)$row['productNumber'] : null,
|
||||
id: (string) ($row['id'] ?? ''),
|
||||
name: trim((string) ($row['translated']['name'] ?? '')),
|
||||
productNumber: isset($row['productNumber']) ? (string) $row['productNumber'] : null,
|
||||
manufacturer: $this->extractManufacturer($row),
|
||||
price: $this->extractPrice($row),
|
||||
available: isset($row['available']) ? (bool)$row['available'] : null,
|
||||
available: isset($row['available']) ? (bool) $row['available'] : null,
|
||||
url: $this->baseUrl . $this->extractUrl($row),
|
||||
highlights: $this->extractHighlights($row),
|
||||
description: $this->cleanUpDescription($row),
|
||||
productImage: $row['cover']['media']['thumbnails'][0]['url'] ?? 'no-image',
|
||||
customFields: $this->getRelevantCustomFields($row['customFields'])
|
||||
customFields: $this->getRelevantCustomFields($row['customFields'] ?? [])
|
||||
);
|
||||
}
|
||||
|
||||
return array_values(array_filter(
|
||||
$results,
|
||||
static fn(ShopProductResult $product): bool => $product->name !== ''
|
||||
static fn (ShopProductResult $product): bool => $product->name !== ''
|
||||
));
|
||||
}
|
||||
|
||||
private function getRelevantCustomFields($customField): string
|
||||
private function getRelevantCustomFields(array $customField): string
|
||||
{
|
||||
$result = ($customField['migration_Backup_product_attr1'] ?? '') . ': ' . ($customField['migration_Backup_product_attr2'] ?? '');
|
||||
$result .= ' | Einsatzgebiete: ' . ($customField['migration_Backup_product_attr4'] ?? '');
|
||||
$result .= ' | Sprachen: ' . ($customField['migration_Backup_product_attr5'] ?? '');
|
||||
|
||||
return $result;
|
||||
return trim($result);
|
||||
}
|
||||
|
||||
private function cleanUpDescription($description): string
|
||||
private function cleanUpDescription(array $description): string
|
||||
{
|
||||
if (isset($description['translated']['description'])) {
|
||||
$newDesc = strip_tags((string)$description['translated']['description']);
|
||||
$newDesc = preg_replace('/^[ \t]*\R/m', '', $newDesc); // leere Zeilen weg
|
||||
$newDesc = preg_replace('/[ \t]{2,}/', ' ', $newDesc); // mehrere Spaces zu einem
|
||||
$result = trim($newDesc);
|
||||
return substr($result, 0, 500);
|
||||
$newDesc = strip_tags((string) $description['translated']['description']);
|
||||
$newDesc = preg_replace('/^[ \t]*\R/m', '', $newDesc);
|
||||
$newDesc = preg_replace('/[ \t]{2,}/', ' ', $newDesc);
|
||||
$result = trim((string) $newDesc);
|
||||
|
||||
return mb_substr($result, 0, 500);
|
||||
}
|
||||
|
||||
return '';
|
||||
@@ -114,7 +113,9 @@ final readonly class ShopSearchService
|
||||
$manufacturer = $row['manufacturer'] ?? null;
|
||||
|
||||
if (is_array($manufacturer) && isset($manufacturer['name']) && is_string($manufacturer['name'])) {
|
||||
return trim($manufacturer['name']) !== '' ? trim($manufacturer['name']) : null;
|
||||
$name = trim($manufacturer['name']);
|
||||
|
||||
return $name !== '' ? $name : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -128,12 +129,18 @@ final readonly class ShopSearchService
|
||||
return null;
|
||||
}
|
||||
|
||||
$unitPrice = $calculatedPrice['unitPrice'] ?? $calculatedPrice['totalPrice'] ?? $calculatedPrice['referencePrice'] ?? $calculatedPrice['listPrice'] ?? $calculatedPrice['regulationPrice'] ?? 0;
|
||||
$unitPrice = $calculatedPrice['unitPrice']
|
||||
?? $calculatedPrice['totalPrice']
|
||||
?? $calculatedPrice['referencePrice']
|
||||
?? $calculatedPrice['listPrice']
|
||||
?? $calculatedPrice['regulationPrice']
|
||||
?? null;
|
||||
|
||||
if (!is_numeric($unitPrice)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return number_format((float)$unitPrice, 2, ',', '.') . ' €';
|
||||
return number_format((float) $unitPrice, 2, ',', '.') . ' €';
|
||||
}
|
||||
|
||||
private function extractUrl(array $row): ?string
|
||||
@@ -166,7 +173,7 @@ final readonly class ShopSearchService
|
||||
$highlights = [];
|
||||
|
||||
if (isset($row['available'])) {
|
||||
$highlights[] = ((bool)$row['available']) ? 'Verfügbar' : 'Nicht verfügbar';
|
||||
$highlights[] = (bool) $row['available'] ? 'Verfügbar' : 'Nicht verfügbar';
|
||||
}
|
||||
|
||||
if (isset($row['productNumber']) && is_string($row['productNumber']) && trim($row['productNumber']) !== '') {
|
||||
|
||||
Reference in New Issue
Block a user