Moved Element14 configuration logic to jbtronics/settings-bundle

This commit is contained in:
Jan Böhmer 2024-05-06 22:25:02 +02:00
parent 7ad077862c
commit 3967c53468
5 changed files with 56 additions and 14 deletions

View file

@ -267,11 +267,6 @@ services:
arguments: arguments:
$providers: !tagged_iterator 'app.info_provider' $providers: !tagged_iterator 'app.info_provider'
App\Services\InfoProviderSystem\Providers\Element14Provider:
arguments:
$api_key: '%env(string:PROVIDER_ELEMENT14_KEY)%'
$store_id: '%env(string:PROVIDER_ELEMENT14_STORE_ID)%'
App\Services\InfoProviderSystem\Providers\DigikeyProvider: App\Services\InfoProviderSystem\Providers\DigikeyProvider:
arguments: arguments:
$clientId: '%env(string:PROVIDER_DIGIKEY_CLIENT_ID)%' $clientId: '%env(string:PROVIDER_DIGIKEY_CLIENT_ID)%'

View file

@ -29,6 +29,7 @@ use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PriceDTO;
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
use App\Settings\InfoProviderSystem\Element14Settings;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
class Element14Provider implements InfoProviderInterface class Element14Provider implements InfoProviderInterface
@ -43,7 +44,7 @@ class Element14Provider implements InfoProviderInterface
private const COMPLIANCE_ATTRIBUTES = ['euEccn', 'hazardous', 'MSL', 'productTraceability', 'rohsCompliant', private const COMPLIANCE_ATTRIBUTES = ['euEccn', 'hazardous', 'MSL', 'productTraceability', 'rohsCompliant',
'rohsPhthalatesCompliant', 'SVHC', 'tariffCode', 'usEccn', 'hazardCode']; 'rohsPhthalatesCompliant', 'SVHC', 'tariffCode', 'usEccn', 'hazardCode'];
public function __construct(private readonly HttpClientInterface $element14Client, private readonly string $api_key, private readonly string $store_id) public function __construct(private readonly HttpClientInterface $element14Client, private readonly Element14Settings $settings)
{ {
} }
@ -65,7 +66,7 @@ class Element14Provider implements InfoProviderInterface
public function isActive(): bool public function isActive(): bool
{ {
return !empty($this->api_key); return !empty($this->settings->apiKey);
} }
/** /**
@ -77,11 +78,11 @@ class Element14Provider implements InfoProviderInterface
$response = $this->element14Client->request('GET', self::ENDPOINT_URL, [ $response = $this->element14Client->request('GET', self::ENDPOINT_URL, [
'query' => [ 'query' => [
'term' => $term, 'term' => $term,
'storeInfo.id' => $this->store_id, 'storeInfo.id' => $this->settings->storeId,
'resultsSettings.offset' => 0, 'resultsSettings.offset' => 0,
'resultsSettings.numberOfResults' => self::NUMBER_OF_RESULTS, 'resultsSettings.numberOfResults' => self::NUMBER_OF_RESULTS,
'resultsSettings.responseGroup' => 'large', 'resultsSettings.responseGroup' => 'large',
'callInfo.apiKey' => $this->api_key, 'callInfo.apiKey' => $this->settings->apiKey,
'callInfo.responseDataFormat' => 'json', 'callInfo.responseDataFormat' => 'json',
'callInfo.version' => self::API_VERSION_NUMBER, 'callInfo.version' => self::API_VERSION_NUMBER,
], ],
@ -119,7 +120,7 @@ class Element14Provider implements InfoProviderInterface
private function generateProductURL($sku): string private function generateProductURL($sku): string
{ {
return 'https://' . $this->store_id . '/' . $sku; return 'https://' . $this->settings->storeId . '/' . $sku;
} }
/** /**
@ -152,7 +153,7 @@ class Element14Provider implements InfoProviderInterface
$locale = 'en_US'; $locale = 'en_US';
} }
return 'https://' . $this->store_id . '/productimages/standard/' . $locale . $image['baseName']; return 'https://' . $this->settings->storeId . '/productimages/standard/' . $locale . $image['baseName'];
} }
/** /**
@ -187,7 +188,7 @@ class Element14Provider implements InfoProviderInterface
public function getUsedCurrency(): string public function getUsedCurrency(): string
{ {
//Decide based on the shop ID //Decide based on the shop ID
return match ($this->store_id) { return match ($this->settings->storeId) {
'bg.farnell.com', 'at.farnell.com', 'si.farnell.com', 'sk.farnell.com', 'ro.farnell.com', 'pt.farnell.com', 'nl.farnell.com', 'be.farnell.com', 'lv.farnell.com', 'lt.farnell.com', 'it.farnell.com', 'fr.farnell.com', 'fi.farnell.com', 'ee.farnell.com', 'es.farnell.com', 'ie.farnell.com', 'cpcireland.farnell.com', 'de.farnell.com' => 'EUR', 'bg.farnell.com', 'at.farnell.com', 'si.farnell.com', 'sk.farnell.com', 'ro.farnell.com', 'pt.farnell.com', 'nl.farnell.com', 'be.farnell.com', 'lv.farnell.com', 'lt.farnell.com', 'it.farnell.com', 'fr.farnell.com', 'fi.farnell.com', 'ee.farnell.com', 'es.farnell.com', 'ie.farnell.com', 'cpcireland.farnell.com', 'de.farnell.com' => 'EUR',
'cz.farnell.com' => 'CZK', 'cz.farnell.com' => 'CZK',
'dk.farnell.com' => 'DKK', 'dk.farnell.com' => 'DKK',
@ -214,7 +215,7 @@ class Element14Provider implements InfoProviderInterface
'tw.element14.com' => 'TWD', 'tw.element14.com' => 'TWD',
'kr.element14.com' => 'KRW', 'kr.element14.com' => 'KRW',
'vn.element14.com' => 'VND', 'vn.element14.com' => 'VND',
default => throw new \RuntimeException('Unknown store ID: ' . $this->store_id) default => throw new \RuntimeException('Unknown store ID: ' . $this->settings->storeId)
}; };
} }

View file

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 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 Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
#[Settings]
class Element14Settings
{
use SettingsTrait;
#[SettingsParameter(envVar: "PROVIDER_ELEMENT14_KEY")]
public ?string $apiKey = null;
#[SettingsParameter(envVar: "PROVIDER_ELEMENT14_STORE_ID")]
public string $storeId = "de.farnell.com";
}

View file

@ -27,7 +27,7 @@ use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
use Jbtronics\SettingsBundle\Settings\Settings; use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsTrait; use Jbtronics\SettingsBundle\Settings\SettingsTrait;
#[Settings] #[Settings()]
class InfoProviderSettings class InfoProviderSettings
{ {
use SettingsTrait; use SettingsTrait;
@ -40,4 +40,7 @@ class InfoProviderSettings
#[EmbeddedSettings] #[EmbeddedSettings]
public ?TMESettings $tme = null; public ?TMESettings $tme = null;
#[EmbeddedSettings]
public ?Element14Settings $element14 = null;
} }

View file

@ -25,6 +25,7 @@ namespace App\Settings\InfoProviderSystem;
use Jbtronics\SettingsBundle\Settings\Settings; use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter; 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\CountryType;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType; use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
use Symfony\Component\Form\Extension\Core\Type\LanguageType; use Symfony\Component\Form\Extension\Core\Type\LanguageType;
@ -33,6 +34,8 @@ use Symfony\Component\Validator\Constraints as Assert;
#[Settings] #[Settings]
class TMESettings class TMESettings
{ {
use SettingsTrait;
private const SUPPORTED_CURRENCIES = ["EUR", "USD", "PLN", "GBP"]; private const SUPPORTED_CURRENCIES = ["EUR", "USD", "PLN", "GBP"];
#[SettingsParameter(envVar: "PROVIDER_TME_KEY")] #[SettingsParameter(envVar: "PROVIDER_TME_KEY")]