diff --git a/config/services.yaml b/config/services.yaml index 2e03f7a9..292c532f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -267,11 +267,6 @@ services: arguments: $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: arguments: $clientId: '%env(string:PROVIDER_DIGIKEY_CLIENT_ID)%' diff --git a/src/Services/InfoProviderSystem/Providers/Element14Provider.php b/src/Services/InfoProviderSystem/Providers/Element14Provider.php index 085c9e50..cdde26df 100644 --- a/src/Services/InfoProviderSystem/Providers/Element14Provider.php +++ b/src/Services/InfoProviderSystem/Providers/Element14Provider.php @@ -29,6 +29,7 @@ use App\Services\InfoProviderSystem\DTOs\ParameterDTO; use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; +use App\Settings\InfoProviderSystem\Element14Settings; use Symfony\Contracts\HttpClient\HttpClientInterface; class Element14Provider implements InfoProviderInterface @@ -43,7 +44,7 @@ class Element14Provider implements InfoProviderInterface private const COMPLIANCE_ATTRIBUTES = ['euEccn', 'hazardous', 'MSL', 'productTraceability', 'rohsCompliant', '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 { - 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, [ 'query' => [ 'term' => $term, - 'storeInfo.id' => $this->store_id, + 'storeInfo.id' => $this->settings->storeId, 'resultsSettings.offset' => 0, 'resultsSettings.numberOfResults' => self::NUMBER_OF_RESULTS, 'resultsSettings.responseGroup' => 'large', - 'callInfo.apiKey' => $this->api_key, + 'callInfo.apiKey' => $this->settings->apiKey, 'callInfo.responseDataFormat' => 'json', 'callInfo.version' => self::API_VERSION_NUMBER, ], @@ -119,7 +120,7 @@ class Element14Provider implements InfoProviderInterface 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'; } - 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 { //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', 'cz.farnell.com' => 'CZK', 'dk.farnell.com' => 'DKK', @@ -214,7 +215,7 @@ class Element14Provider implements InfoProviderInterface 'tw.element14.com' => 'TWD', 'kr.element14.com' => 'KRW', 'vn.element14.com' => 'VND', - default => throw new \RuntimeException('Unknown store ID: ' . $this->store_id) + default => throw new \RuntimeException('Unknown store ID: ' . $this->settings->storeId) }; } diff --git a/src/Settings/InfoProviderSystem/Element14Settings.php b/src/Settings/InfoProviderSystem/Element14Settings.php new file mode 100644 index 00000000..38797edc --- /dev/null +++ b/src/Settings/InfoProviderSystem/Element14Settings.php @@ -0,0 +1,40 @@ +. + */ + +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"; +} \ No newline at end of file diff --git a/src/Settings/InfoProviderSystem/InfoProviderSettings.php b/src/Settings/InfoProviderSystem/InfoProviderSettings.php index f2896ce1..006bed27 100644 --- a/src/Settings/InfoProviderSystem/InfoProviderSettings.php +++ b/src/Settings/InfoProviderSystem/InfoProviderSettings.php @@ -27,7 +27,7 @@ use Jbtronics\SettingsBundle\Settings\EmbeddedSettings; use Jbtronics\SettingsBundle\Settings\Settings; use Jbtronics\SettingsBundle\Settings\SettingsTrait; -#[Settings] +#[Settings()] class InfoProviderSettings { use SettingsTrait; @@ -40,4 +40,7 @@ class InfoProviderSettings #[EmbeddedSettings] public ?TMESettings $tme = null; + + #[EmbeddedSettings] + public ?Element14Settings $element14 = null; } \ No newline at end of file diff --git a/src/Settings/InfoProviderSystem/TMESettings.php b/src/Settings/InfoProviderSystem/TMESettings.php index d9307b88..1312189e 100644 --- a/src/Settings/InfoProviderSystem/TMESettings.php +++ b/src/Settings/InfoProviderSystem/TMESettings.php @@ -25,6 +25,7 @@ namespace App\Settings\InfoProviderSystem; 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\Form\Extension\Core\Type\LanguageType; @@ -33,6 +34,8 @@ use Symfony\Component\Validator\Constraints as Assert; #[Settings] class TMESettings { + use SettingsTrait; + private const SUPPORTED_CURRENCIES = ["EUR", "USD", "PLN", "GBP"]; #[SettingsParameter(envVar: "PROVIDER_TME_KEY")]