Skip to content

Commit

Permalink
Moved OEMSecrets config to new settings system
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtronics committed Jan 18, 2025
1 parent 97aed84 commit 3d4e91f
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 109 deletions.
10 changes: 0 additions & 10 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,6 @@ services:
$onlyAuthorizedSellers: '%env(bool:PROVIDER_OCTOPART_ONLY_AUTHORIZED_SELLERS)%'


App\Services\InfoProviderSystem\Providers\OEMSecretsProvider:
arguments:
$api_key: '%env(string:PROVIDER_OEMSECRETS_KEY)%'
$country_code: '%env(string:PROVIDER_OEMSECRETS_COUNTRY_CODE)%'
$currency: '%env(PROVIDER_OEMSECRETS_CURRENCY)%'
$zero_price: '%env(PROVIDER_OEMSECRETS_ZERO_PRICE)%'
$set_param: '%env(PROVIDER_OEMSECRETS_SET_PARAM)%'
$sort_criteria: '%env(PROVIDER_OEMSECRETS_SORT_CRITERIA)%'


####################################################################################################################
# API system
####################################################################################################################
Expand Down
39 changes: 18 additions & 21 deletions src/Services/InfoProviderSystem/Providers/OEMSecretsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
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;

Expand All @@ -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
)
{
Expand Down Expand Up @@ -268,7 +265,7 @@ public function getProviderKey(): string
*/
public function isActive(): bool
{
return $this->api_key !== '';
return $this->settings->apiKey !== '';
}


Expand Down Expand Up @@ -324,9 +321,9 @@ public function searchByKeyword(string $keyword): array
$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,
],
]);

Expand Down Expand Up @@ -533,7 +530,7 @@ private function processBatch(

// 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
}

Expand All @@ -557,7 +554,7 @@ private function processBatch(
}

$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] = [];
Expand All @@ -582,7 +579,7 @@ private function processBatch(
$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;
Expand All @@ -599,9 +596,9 @@ private function processBatch(
}

// 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;
}

Expand Down Expand Up @@ -681,8 +678,8 @@ private function getPrices(array $product): array

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) &&
Expand All @@ -694,7 +691,7 @@ private function getPrices(array $product): array
$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
);
Expand Down Expand Up @@ -1293,7 +1290,7 @@ private function generateInquiryUrl(string $partNumber, string $oemInquiry = 'co
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) {
Expand Down Expand Up @@ -1332,9 +1329,9 @@ private function sortResultsData(array &$resultsData, string $searchKeyword): vo
}

// 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);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Settings/InfoProviderSystem/InfoProviderSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ class InfoProviderSettings

#[EmbeddedSettings]
public ?LCSCSettings $lcsc = null;

#[EmbeddedSettings]
public ?OEMSecretsSettings $oemsecrets = null;
}
82 changes: 82 additions & 0 deletions src/Settings/InfoProviderSystem/OEMSecretsSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);


namespace App\Settings\InfoProviderSystem;

use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Translation\TranslatableMessage as TM;

#[Settings(label: new TM("settings.ips.oemsecrets"))]
#[SettingsIcon("fa-plug")]
class OEMSecretsSettings
{
use SettingsTrait;

public const SUPPORTED_CURRENCIES = ["AUD", "CAD", "CHF", "CNY", "DKK", "EUR", "GBP", "HKD", "ILS", "INR", "JPY", "KRW", "NOK",
"NZD", "RUB", "SEK", "SGD", "TWD", "USD"];

#[SettingsParameter(label: new TM("settings.ips.element14.apiKey"), envVar: "PROVIDER_OEMSECRETS_KEY")]
public ?string $apiKey = null;

#[Assert\Country]
#[SettingsParameter(label: new TM("settings.ips.tme.country"), formType: CountryType::class, formOptions: ["preferred_choices" => ["DE", "PL", "GB", "FR", "US"]], envVar: "PROVIDER_OEMSECRETS_COUNTRY_CODE")]
public ?string $country = "DE";

#[SettingsParameter(label: new TM("settings.ips.tme.currency"), formType: CurrencyType::class, formOptions: ["preferred_choices" => self::SUPPORTED_CURRENCIES], envVar: "PROVIDER_OEMSECRETS_CURRENCY")]
#[Assert\Choice(choices: self::SUPPORTED_CURRENCIES)]
public string $currency = "EUR";

/**
* @var bool If this is enabled, distributors with zero prices
* will be discarded from the creation of a new part
*/
#[SettingsParameter(label: new TM("settings.ips.oemsecrets.keepZeroPrices"), description: new TM("settings.ips.oemsecrets.keepZeroPrices.help"), envVar: "bool:PROVIDER_OEMSECRETS_ZERO_PRICE")]
public bool $keepZeroPrices = false;

/**
* @var bool If set to 1 the parameters for the part are generated
* # from the description transforming unstructured descriptions into structured parameters;
* # each parameter in description should have the form: "...;name1:value1;name2:value2"
*/
#[SettingsParameter(label: new TM("settings.ips.oemsecrets.parseParams"), description: new TM("settings.ips.oemsecrets.parseParams.help"), envVar: "bool:PROVIDER_OEMSECRETS_SET_PARAM")]
public bool $parseParams = true;

#[SettingsParameter(label: new TM("settings.ips.oemsecrets.sortMode"), envVar: "PROVIDER_OEMSECRETS_SORT_CRITERIA", envVarMapper: [self::class, "mapSortModeEnvVar"])]
public OEMSecretsSortMode $sortMode = OEMSecretsSortMode::COMPLETENESS;


public static function mapSortModeEnvVar(?string $value): OEMSecretsSortMode
{
if (!$value) {
return OEMSecretsSortMode::NONE;
}

return OEMSecretsSortMode::tryFrom($value) ?? OEMSecretsSortMode::NONE;
}
}
46 changes: 46 additions & 0 deletions src/Settings/InfoProviderSystem/OEMSecretsSortMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);


namespace App\Settings\InfoProviderSystem;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* This environment variable determines the sorting criteria for product results.
* The sorting process first arranges items based on the provided keyword.
* Then, if set to 'C', it further sorts by completeness (prioritizing items with the most
* detailed information). If set to 'M', it further sorts by manufacturer name.
* If unset or set to any other value, no sorting is performed.
*/
enum OEMSecretsSortMode : string implements TranslatableInterface
{
case NONE = "N";
case COMPLETENESS = "C";
case MANUFACTURER = "M";

public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return $translator->trans('settings.ips.oemsecrets.sortMode.' . $this->value, locale: $locale);
}
}
Loading

0 comments on commit 3d4e91f

Please sign in to comment.