diff --git a/config/services.yaml b/config/services.yaml index 34b0dddc..2e03f7a9 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -279,18 +279,6 @@ services: $language: '%env(string:PROVIDER_DIGIKEY_LANGUAGE)%' $country: '%env(string:PROVIDER_DIGIKEY_COUNTRY)%' - App\Services\InfoProviderSystem\Providers\TMEClient: - arguments: - $secret: '%env(string:PROVIDER_TME_SECRET)%' - $token: '%env(string:PROVIDER_TME_KEY)%' - - App\Services\InfoProviderSystem\Providers\TMEProvider: - arguments: - $currency: '%env(string:PROVIDER_TME_CURRENCY)%' - $country: '%env(string:PROVIDER_TME_COUNTRY)%' - $language: '%env(string:PROVIDER_TME_LANGUAGE)%' - $get_gross_prices: '%env(bool:PROVIDER_TME_GET_GROSS_PRICES)%' - App\Services\InfoProviderSystem\Providers\OctopartProvider: arguments: $clientId: '&env(string:PROVIDER_OCTOPART_CLIENT_ID)%' diff --git a/src/Services/InfoProviderSystem/Providers/TMEClient.php b/src/Services/InfoProviderSystem/Providers/TMEClient.php index df2c7202..1e679ef5 100644 --- a/src/Services/InfoProviderSystem/Providers/TMEClient.php +++ b/src/Services/InfoProviderSystem/Providers/TMEClient.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace App\Services\InfoProviderSystem\Providers; +use App\Settings\InfoProviderSystem\TMESettings; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; @@ -30,15 +31,15 @@ class TMEClient { public const BASE_URI = 'https://api.tme.eu'; - public function __construct(private readonly HttpClientInterface $tmeClient, private readonly string $token, private readonly string $secret) + public function __construct(private readonly HttpClientInterface $tmeClient, private readonly TMESettings $settings) { } public function makeRequest(string $action, array $parameters): ResponseInterface { - $parameters['Token'] = $this->token; - $parameters['ApiSignature'] = $this->getSignature($action, $parameters, $this->secret); + $parameters['Token'] = $this->settings->apiToken; + $parameters['ApiSignature'] = $this->getSignature($action, $parameters, $this->settings->apiSecret); return $this->tmeClient->request('POST', $this->getUrlForAction($action), [ 'body' => $parameters, @@ -47,7 +48,7 @@ class TMEClient public function isUsable(): bool { - return !($this->token === '' || $this->secret === ''); + return !($this->settings->apiToken === '' || $this->settings->apiSecret === ''); } diff --git a/src/Services/InfoProviderSystem/Providers/TMEProvider.php b/src/Services/InfoProviderSystem/Providers/TMEProvider.php index 892294f3..b38223b8 100644 --- a/src/Services/InfoProviderSystem/Providers/TMEProvider.php +++ b/src/Services/InfoProviderSystem/Providers/TMEProvider.php @@ -30,16 +30,14 @@ use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; use App\Services\InfoProviderSystem\DTOs\SearchResultDTO; +use App\Settings\InfoProviderSystem\TMESettings; class TMEProvider implements InfoProviderInterface { private const VENDOR_NAME = 'TME'; - public function __construct(private readonly TMEClient $tmeClient, private readonly string $country, - private readonly string $language, private readonly string $currency, - /** @var bool If true, the prices are gross prices. If false, the prices are net prices. */ - private readonly bool $get_gross_prices) + public function __construct(private readonly TMEClient $tmeClient, private readonly TMESettings $settings) { } @@ -67,8 +65,8 @@ class TMEProvider implements InfoProviderInterface public function searchByKeyword(string $keyword): array { $response = $this->tmeClient->makeRequest('Products/Search', [ - 'Country' => $this->country, - 'Language' => $this->language, + 'Country' => $this->settings->country, + 'Language' => $this->settings->language, 'SearchPlain' => $keyword, ]); @@ -97,8 +95,8 @@ class TMEProvider implements InfoProviderInterface public function getDetails(string $id): PartDetailDTO { $response = $this->tmeClient->makeRequest('Products/GetProducts', [ - 'Country' => $this->country, - 'Language' => $this->language, + 'Country' => $this->settings->country, + 'Language' => $this->settings->language, 'SymbolList' => [$id], ]); @@ -142,8 +140,8 @@ class TMEProvider implements InfoProviderInterface public function getFiles(string $id): array { $response = $this->tmeClient->makeRequest('Products/GetProductsFiles', [ - 'Country' => $this->country, - 'Language' => $this->language, + 'Country' => $this->settings->country, + 'Language' => $this->settings->language, 'SymbolList' => [$id], ]); @@ -184,10 +182,10 @@ class TMEProvider implements InfoProviderInterface public function getVendorInfo(string $id, ?string $productURL = null): PurchaseInfoDTO { $response = $this->tmeClient->makeRequest('Products/GetPricesAndStocks', [ - 'Country' => $this->country, - 'Language' => $this->language, - 'Currency' => $this->currency, - 'GrossPrices' => $this->get_gross_prices, + 'Country' => $this->settings->country, + 'Language' => $this->settings->language, + 'Currency' => $this->settings->currency, + 'GrossPrices' => $this->settings->grossPrices, 'SymbolList' => [$id], ]); @@ -227,8 +225,8 @@ class TMEProvider implements InfoProviderInterface public function getParameters(string $id, string|null &$footprint_name = null): array { $response = $this->tmeClient->makeRequest('Products/GetParameters', [ - 'Country' => $this->country, - 'Language' => $this->language, + 'Country' => $this->settings->country, + 'Language' => $this->settings->language, 'SymbolList' => [$id], ]); diff --git a/src/Settings/AppSettings.php b/src/Settings/AppSettings.php index 1b067939..1973ba1f 100644 --- a/src/Settings/AppSettings.php +++ b/src/Settings/AppSettings.php @@ -34,5 +34,5 @@ class AppSettings use SettingsTrait; #[EmbeddedSettings] - public InfoProviderSettings $infoProviders; + public ?InfoProviderSettings $infoProviders = null; } \ No newline at end of file diff --git a/src/Settings/InfoProviderSystem/InfoProviderSettings.php b/src/Settings/InfoProviderSystem/InfoProviderSettings.php index 626320f2..f2896ce1 100644 --- a/src/Settings/InfoProviderSystem/InfoProviderSettings.php +++ b/src/Settings/InfoProviderSystem/InfoProviderSettings.php @@ -33,8 +33,11 @@ class InfoProviderSettings use SettingsTrait; #[EmbeddedSettings] - public LCSCSettings $lcsc; + public ?LCSCSettings $lcsc = null; #[EmbeddedSettings] - public MouserSettings $mouser; + public ?MouserSettings $mouser = null; + + #[EmbeddedSettings] + public ?TMESettings $tme = null; } \ No newline at end of file diff --git a/src/Settings/InfoProviderSystem/TMESettings.php b/src/Settings/InfoProviderSystem/TMESettings.php new file mode 100644 index 00000000..d9307b88 --- /dev/null +++ b/src/Settings/InfoProviderSystem/TMESettings.php @@ -0,0 +1,58 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Settings\InfoProviderSystem; + +use Jbtronics\SettingsBundle\Settings\Settings; +use Jbtronics\SettingsBundle\Settings\SettingsParameter; +use Symfony\Component\Form\Extension\Core\Type\CountryType; +use Symfony\Component\Form\Extension\Core\Type\CurrencyType; +use Symfony\Component\Form\Extension\Core\Type\LanguageType; +use Symfony\Component\Validator\Constraints as Assert; + +#[Settings] +class TMESettings +{ + private const SUPPORTED_CURRENCIES = ["EUR", "USD", "PLN", "GBP"]; + + #[SettingsParameter(envVar: "PROVIDER_TME_KEY")] + public ?string $apiToken = null; + + #[SettingsParameter(envVar: "PROVIDER_TME_SECRET")] + public ?string $apiSecret = null; + + #[SettingsParameter(formType: CurrencyType::class, formOptions: ["preferred_choices" => self::SUPPORTED_CURRENCIES], envVar: "PROVIDER_TME_CURRENCY")] + #[Assert\Choice(choices: self::SUPPORTED_CURRENCIES)] + public string $currency = "EUR"; + + #[SettingsParameter(formType: LanguageType::class, formOptions: ["preferred_choices" => ["en", "de", "fr", "pl"]], envVar: "PROVIDER_TME_LANGUAGE")] + #[Assert\Language] + public string $language = "en"; + + #[SettingsParameter(envVar: "PROVIDER_TME_COUNTRY", formType: CountryType::class, formOptions: ["preferred_choices" => ["DE", "PL", "GB", "FR"]])] + #[Assert\Country] + public string $country = "DE"; + + #[SettingsParameter(envVar: "bool:PROVIDER_TME_GET_GROSS_PRICES")] + public bool $grossPrices = true; +} \ No newline at end of file