From 071f6f85913ab4a5c496a06e46939ab603c0a77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 1 Feb 2026 17:34:08 +0100 Subject: [PATCH] Return an empty array if no URL is provider to the Generic Web URL provider --- .../ProviderIDNotSupportedException.php | 32 +++++++++++++++++++ .../Providers/GenericWebProvider.php | 13 +++++++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/Exceptions/ProviderIDNotSupportedException.php diff --git a/src/Exceptions/ProviderIDNotSupportedException.php b/src/Exceptions/ProviderIDNotSupportedException.php new file mode 100644 index 00000000..429f43ea --- /dev/null +++ b/src/Exceptions/ProviderIDNotSupportedException.php @@ -0,0 +1,32 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Exceptions; + +class ProviderIDNotSupportedException extends \RuntimeException +{ + public function fromProvider(string $providerKey, string $id): self + { + return new self(sprintf('The given ID %s is not supported by the provider %s.', $id, $providerKey,)); + } +} diff --git a/src/Services/InfoProviderSystem/Providers/GenericWebProvider.php b/src/Services/InfoProviderSystem/Providers/GenericWebProvider.php index a098c685..248b4f0e 100644 --- a/src/Services/InfoProviderSystem/Providers/GenericWebProvider.php +++ b/src/Services/InfoProviderSystem/Providers/GenericWebProvider.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace App\Services\InfoProviderSystem\Providers; +use App\Exceptions\ProviderIDNotSupportedException; use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; @@ -71,9 +72,12 @@ class GenericWebProvider implements InfoProviderInterface public function searchByKeyword(string $keyword): array { + try { return [ $this->getDetails($keyword) - ]; + ]; } catch (ProviderIDNotSupportedException $e) { + return []; + } } private function extractShopName(string $url): string @@ -212,6 +216,13 @@ class GenericWebProvider implements InfoProviderInterface $url = $id; + //If this is not a valid URL with host, domain and path, throw an exception + if (filter_var($url, FILTER_VALIDATE_URL) === false || + parse_url($url, PHP_URL_HOST) === null || + parse_url($url, PHP_URL_PATH) === null) { + throw new ProviderIDNotSupportedException("The given ID is not a valid URL: ".$id); + } + //Try to get the webpage content $response = $this->httpClient->request('GET', $url); $content = $response->getContent();