Skip to content

Commit 3d4e91f

Browse files
committed
Moved OEMSecrets config to new settings system
1 parent 97aed84 commit 3d4e91f

File tree

6 files changed

+281
-109
lines changed

6 files changed

+281
-109
lines changed

config/services.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,6 @@ services:
216216
$onlyAuthorizedSellers: '%env(bool:PROVIDER_OCTOPART_ONLY_AUTHORIZED_SELLERS)%'
217217

218218

219-
App\Services\InfoProviderSystem\Providers\OEMSecretsProvider:
220-
arguments:
221-
$api_key: '%env(string:PROVIDER_OEMSECRETS_KEY)%'
222-
$country_code: '%env(string:PROVIDER_OEMSECRETS_COUNTRY_CODE)%'
223-
$currency: '%env(PROVIDER_OEMSECRETS_CURRENCY)%'
224-
$zero_price: '%env(PROVIDER_OEMSECRETS_ZERO_PRICE)%'
225-
$set_param: '%env(PROVIDER_OEMSECRETS_SET_PARAM)%'
226-
$sort_criteria: '%env(PROVIDER_OEMSECRETS_SORT_CRITERIA)%'
227-
228-
229219
####################################################################################################################
230220
# API system
231221
####################################################################################################################

src/Services/InfoProviderSystem/Providers/OEMSecretsProvider.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
use App\Services\InfoProviderSystem\DTOs\PriceDTO;
8989
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
9090
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
91+
use App\Settings\InfoProviderSystem\OEMSecretsSettings;
92+
use App\Settings\InfoProviderSystem\OEMSecretsSortMode;
9193
use Symfony\Contracts\HttpClient\HttpClientInterface;
9294
use Psr\Cache\CacheItemPoolInterface;
9395

@@ -99,12 +101,7 @@ class OEMSecretsProvider implements InfoProviderInterface
99101

100102
public function __construct(
101103
private readonly HttpClientInterface $oemsecretsClient,
102-
private readonly string $api_key,
103-
private readonly string $country_code,
104-
private readonly string $currency,
105-
private readonly string $zero_price,
106-
private readonly string $set_param,
107-
private readonly string $sort_criteria,
104+
private readonly OEMSecretsSettings $settings,
108105
private readonly CacheItemPoolInterface $partInfoCache
109106
)
110107
{
@@ -268,7 +265,7 @@ public function getProviderKey(): string
268265
*/
269266
public function isActive(): bool
270267
{
271-
return $this->api_key !== '';
268+
return $this->settings->apiKey !== '';
272269
}
273270

274271

@@ -324,9 +321,9 @@ public function searchByKeyword(string $keyword): array
324321
$response = $this->oemsecretsClient->request('GET', self::ENDPOINT_URL, [
325322
'query' => [
326323
'searchTerm' => $keyword,
327-
'apiKey' => $this->api_key,
328-
'currency' => $this->currency,
329-
'countryCode' => $this->country_code,
324+
'apiKey' => $this->settings->apiKey,
325+
'currency' => $this->settings->currency,
326+
'countryCode' => $this->settings->country,
330327
],
331328
]);
332329

@@ -533,7 +530,7 @@ private function processBatch(
533530

534531
// Extract prices
535532
$priceDTOs = $this->getPrices($product);
536-
if (empty($priceDTOs) && (int)$this->zero_price === 0) {
533+
if (empty($priceDTOs) && !$this->settings->keepZeroPrices) {
537534
return null; // Skip products without valid prices
538535
}
539536

@@ -557,7 +554,7 @@ private function processBatch(
557554
}
558555

559556
$imagesResults[$provider_id] = $this->getImages($product, $imagesResults[$provider_id] ?? []);
560-
if ($this->set_param == 1) {
557+
if ($this->settings->parseParams) {
561558
$parametersResults[$provider_id] = $this->getParameters($product, $parametersResults[$provider_id] ?? []);
562559
} else {
563560
$parametersResults[$provider_id] = [];
@@ -582,7 +579,7 @@ private function processBatch(
582579
$regionB = $this->countryCodeToRegionMap[$countryCodeB] ?? '';
583580

584581
// If the map is empty or doesn't contain the key for $this->country_code, assign a placeholder region.
585-
$regionForEnvCountry = $this->countryCodeToRegionMap[$this->country_code] ?? '';
582+
$regionForEnvCountry = $this->countryCodeToRegionMap[$this->settings->country] ?? '';
586583

587584
// Convert to string before comparison to avoid mixed types
588585
$countryCodeA = (string) $countryCodeA;
@@ -599,9 +596,9 @@ private function processBatch(
599596
}
600597

601598
// Step 1: country_code from the environment
602-
if ($countryCodeA === $this->country_code && $countryCodeB !== $this->country_code) {
599+
if ($countryCodeA === $this->settings->country && $countryCodeB !== $this->settings->country) {
603600
return -1;
604-
} elseif ($countryCodeA !== $this->country_code && $countryCodeB === $this->country_code) {
601+
} elseif ($countryCodeA !== $this->settings->country && $countryCodeB === $this->settings->country) {
605602
return 1;
606603
}
607604

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

682679
if (is_array($prices)) {
683680
// Step 1: Check if prices exist in the preferred currency
684-
if (isset($prices[$this->currency]) && is_array($prices[$this->currency])) {
685-
$priceDetails = $prices[$this->currency];
681+
if (isset($prices[$this->settings->currency]) && is_array($prices[$this->settings->currency])) {
682+
$priceDetails = $prices[$this->$this->settings->currency];
686683
foreach ($priceDetails as $priceDetail) {
687684
if (
688685
is_array($priceDetail) &&
@@ -694,7 +691,7 @@ private function getPrices(array $product): array
694691
$priceDTOs[] = new PriceDTO(
695692
minimum_discount_amount: (float)$priceDetail['unit_break'],
696693
price: (string)$priceDetail['unit_price'],
697-
currency_iso_code: $this->currency,
694+
currency_iso_code: $this->settings->currency,
698695
includes_tax: false,
699696
price_related_quantity: 1.0
700697
);
@@ -1293,7 +1290,7 @@ private function generateInquiryUrl(string $partNumber, string $oemInquiry = 'co
12931290
private function sortResultsData(array &$resultsData, string $searchKeyword): void
12941291
{
12951292
// If the SORT_CRITERIA is not 'C' or 'M', do not sort
1296-
if ($this->sort_criteria !== 'C' && $this->sort_criteria !== 'M') {
1293+
if ($this->settings->sortMode !== OEMSecretsSortMode::COMPLETENESS && $this->settings->sortMode !== OEMSecretsSortMode::MANUFACTURER) {
12971294
return;
12981295
}
12991296
usort($resultsData, function ($a, $b) use ($searchKeyword) {
@@ -1332,9 +1329,9 @@ private function sortResultsData(array &$resultsData, string $searchKeyword): vo
13321329
}
13331330

13341331
// Final sorting: by completeness or manufacturer, if necessary
1335-
if ($this->sort_criteria === 'C') {
1332+
if ($this->settings->sortMode === OEMSecretsSortMode::COMPLETENESS) {
13361333
return $this->compareByCompleteness($a, $b);
1337-
} elseif ($this->sort_criteria === 'M') {
1334+
} elseif ($this->settings->sortMode === OEMSecretsSortMode::MANUFACTURER) {
13381335
return strcasecmp($a->manufacturer, $b->manufacturer);
13391336
}
13401337

src/Settings/InfoProviderSystem/InfoProviderSettings.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ class InfoProviderSettings
4343

4444
#[EmbeddedSettings]
4545
public ?LCSCSettings $lcsc = null;
46+
47+
#[EmbeddedSettings]
48+
public ?OEMSecretsSettings $oemsecrets = null;
4649
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\Settings\InfoProviderSystem;
25+
26+
use App\Settings\SettingsIcon;
27+
use Jbtronics\SettingsBundle\Settings\Settings;
28+
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
29+
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
30+
use Symfony\Component\Form\Extension\Core\Type\CountryType;
31+
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
32+
use Symfony\Component\Validator\Constraints as Assert;
33+
use Symfony\Component\Translation\TranslatableMessage as TM;
34+
35+
#[Settings(label: new TM("settings.ips.oemsecrets"))]
36+
#[SettingsIcon("fa-plug")]
37+
class OEMSecretsSettings
38+
{
39+
use SettingsTrait;
40+
41+
public const SUPPORTED_CURRENCIES = ["AUD", "CAD", "CHF", "CNY", "DKK", "EUR", "GBP", "HKD", "ILS", "INR", "JPY", "KRW", "NOK",
42+
"NZD", "RUB", "SEK", "SGD", "TWD", "USD"];
43+
44+
#[SettingsParameter(label: new TM("settings.ips.element14.apiKey"), envVar: "PROVIDER_OEMSECRETS_KEY")]
45+
public ?string $apiKey = null;
46+
47+
#[Assert\Country]
48+
#[SettingsParameter(label: new TM("settings.ips.tme.country"), formType: CountryType::class, formOptions: ["preferred_choices" => ["DE", "PL", "GB", "FR", "US"]], envVar: "PROVIDER_OEMSECRETS_COUNTRY_CODE")]
49+
public ?string $country = "DE";
50+
51+
#[SettingsParameter(label: new TM("settings.ips.tme.currency"), formType: CurrencyType::class, formOptions: ["preferred_choices" => self::SUPPORTED_CURRENCIES], envVar: "PROVIDER_OEMSECRETS_CURRENCY")]
52+
#[Assert\Choice(choices: self::SUPPORTED_CURRENCIES)]
53+
public string $currency = "EUR";
54+
55+
/**
56+
* @var bool If this is enabled, distributors with zero prices
57+
* will be discarded from the creation of a new part
58+
*/
59+
#[SettingsParameter(label: new TM("settings.ips.oemsecrets.keepZeroPrices"), description: new TM("settings.ips.oemsecrets.keepZeroPrices.help"), envVar: "bool:PROVIDER_OEMSECRETS_ZERO_PRICE")]
60+
public bool $keepZeroPrices = false;
61+
62+
/**
63+
* @var bool If set to 1 the parameters for the part are generated
64+
* # from the description transforming unstructured descriptions into structured parameters;
65+
* # each parameter in description should have the form: "...;name1:value1;name2:value2"
66+
*/
67+
#[SettingsParameter(label: new TM("settings.ips.oemsecrets.parseParams"), description: new TM("settings.ips.oemsecrets.parseParams.help"), envVar: "bool:PROVIDER_OEMSECRETS_SET_PARAM")]
68+
public bool $parseParams = true;
69+
70+
#[SettingsParameter(label: new TM("settings.ips.oemsecrets.sortMode"), envVar: "PROVIDER_OEMSECRETS_SORT_CRITERIA", envVarMapper: [self::class, "mapSortModeEnvVar"])]
71+
public OEMSecretsSortMode $sortMode = OEMSecretsSortMode::COMPLETENESS;
72+
73+
74+
public static function mapSortModeEnvVar(?string $value): OEMSecretsSortMode
75+
{
76+
if (!$value) {
77+
return OEMSecretsSortMode::NONE;
78+
}
79+
80+
return OEMSecretsSortMode::tryFrom($value) ?? OEMSecretsSortMode::NONE;
81+
}
82+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\Settings\InfoProviderSystem;
25+
26+
use Symfony\Contracts\Translation\TranslatableInterface;
27+
use Symfony\Contracts\Translation\TranslatorInterface;
28+
29+
/**
30+
* This environment variable determines the sorting criteria for product results.
31+
* The sorting process first arranges items based on the provided keyword.
32+
* Then, if set to 'C', it further sorts by completeness (prioritizing items with the most
33+
* detailed information). If set to 'M', it further sorts by manufacturer name.
34+
* If unset or set to any other value, no sorting is performed.
35+
*/
36+
enum OEMSecretsSortMode : string implements TranslatableInterface
37+
{
38+
case NONE = "N";
39+
case COMPLETENESS = "C";
40+
case MANUFACTURER = "M";
41+
42+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
43+
{
44+
return $translator->trans('settings.ips.oemsecrets.sortMode.' . $this->value, locale: $locale);
45+
}
46+
}

0 commit comments

Comments
 (0)