Moved OEMSecrets config to new settings system

This commit is contained in:
Jan Böhmer 2025-01-18 22:58:26 +01:00
parent 97aed847b6
commit 3d4e91fc69
6 changed files with 281 additions and 109 deletions

View file

@ -88,6 +88,8 @@ use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
use App\Services\InfoProviderSystem\DTOs\PriceDTO;
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
use App\Settings\InfoProviderSystem\OEMSecretsSettings;
use App\Settings\InfoProviderSystem\OEMSecretsSortMode;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Psr\Cache\CacheItemPoolInterface;
@ -99,12 +101,7 @@ class OEMSecretsProvider implements InfoProviderInterface
public function __construct(
private readonly HttpClientInterface $oemsecretsClient,
private readonly string $api_key,
private readonly string $country_code,
private readonly string $currency,
private readonly string $zero_price,
private readonly string $set_param,
private readonly string $sort_criteria,
private readonly OEMSecretsSettings $settings,
private readonly CacheItemPoolInterface $partInfoCache
)
{
@ -268,7 +265,7 @@ class OEMSecretsProvider implements InfoProviderInterface
*/
public function isActive(): bool
{
return $this->api_key !== '';
return $this->settings->apiKey !== '';
}
@ -324,9 +321,9 @@ class OEMSecretsProvider implements InfoProviderInterface
$response = $this->oemsecretsClient->request('GET', self::ENDPOINT_URL, [
'query' => [
'searchTerm' => $keyword,
'apiKey' => $this->api_key,
'currency' => $this->currency,
'countryCode' => $this->country_code,
'apiKey' => $this->settings->apiKey,
'currency' => $this->settings->currency,
'countryCode' => $this->settings->country,
],
]);
@ -533,7 +530,7 @@ class OEMSecretsProvider implements InfoProviderInterface
// Extract prices
$priceDTOs = $this->getPrices($product);
if (empty($priceDTOs) && (int)$this->zero_price === 0) {
if (empty($priceDTOs) && !$this->settings->keepZeroPrices) {
return null; // Skip products without valid prices
}
@ -557,7 +554,7 @@ class OEMSecretsProvider implements InfoProviderInterface
}
$imagesResults[$provider_id] = $this->getImages($product, $imagesResults[$provider_id] ?? []);
if ($this->set_param == 1) {
if ($this->settings->parseParams) {
$parametersResults[$provider_id] = $this->getParameters($product, $parametersResults[$provider_id] ?? []);
} else {
$parametersResults[$provider_id] = [];
@ -582,7 +579,7 @@ class OEMSecretsProvider implements InfoProviderInterface
$regionB = $this->countryCodeToRegionMap[$countryCodeB] ?? '';
// If the map is empty or doesn't contain the key for $this->country_code, assign a placeholder region.
$regionForEnvCountry = $this->countryCodeToRegionMap[$this->country_code] ?? '';
$regionForEnvCountry = $this->countryCodeToRegionMap[$this->settings->country] ?? '';
// Convert to string before comparison to avoid mixed types
$countryCodeA = (string) $countryCodeA;
@ -599,9 +596,9 @@ class OEMSecretsProvider implements InfoProviderInterface
}
// Step 1: country_code from the environment
if ($countryCodeA === $this->country_code && $countryCodeB !== $this->country_code) {
if ($countryCodeA === $this->settings->country && $countryCodeB !== $this->settings->country) {
return -1;
} elseif ($countryCodeA !== $this->country_code && $countryCodeB === $this->country_code) {
} elseif ($countryCodeA !== $this->settings->country && $countryCodeB === $this->settings->country) {
return 1;
}
@ -681,8 +678,8 @@ class OEMSecretsProvider implements InfoProviderInterface
if (is_array($prices)) {
// Step 1: Check if prices exist in the preferred currency
if (isset($prices[$this->currency]) && is_array($prices[$this->currency])) {
$priceDetails = $prices[$this->currency];
if (isset($prices[$this->settings->currency]) && is_array($prices[$this->settings->currency])) {
$priceDetails = $prices[$this->$this->settings->currency];
foreach ($priceDetails as $priceDetail) {
if (
is_array($priceDetail) &&
@ -694,7 +691,7 @@ class OEMSecretsProvider implements InfoProviderInterface
$priceDTOs[] = new PriceDTO(
minimum_discount_amount: (float)$priceDetail['unit_break'],
price: (string)$priceDetail['unit_price'],
currency_iso_code: $this->currency,
currency_iso_code: $this->settings->currency,
includes_tax: false,
price_related_quantity: 1.0
);
@ -1293,7 +1290,7 @@ class OEMSecretsProvider implements InfoProviderInterface
private function sortResultsData(array &$resultsData, string $searchKeyword): void
{
// If the SORT_CRITERIA is not 'C' or 'M', do not sort
if ($this->sort_criteria !== 'C' && $this->sort_criteria !== 'M') {
if ($this->settings->sortMode !== OEMSecretsSortMode::COMPLETENESS && $this->settings->sortMode !== OEMSecretsSortMode::MANUFACTURER) {
return;
}
usort($resultsData, function ($a, $b) use ($searchKeyword) {
@ -1332,9 +1329,9 @@ class OEMSecretsProvider implements InfoProviderInterface
}
// Final sorting: by completeness or manufacturer, if necessary
if ($this->sort_criteria === 'C') {
if ($this->settings->sortMode === OEMSecretsSortMode::COMPLETENESS) {
return $this->compareByCompleteness($a, $b);
} elseif ($this->sort_criteria === 'M') {
} elseif ($this->settings->sortMode === OEMSecretsSortMode::MANUFACTURER) {
return strcasecmp($a->manufacturer, $b->manufacturer);
}