mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-02-12 04:29:36 +00:00
Return an empty array if no URL is provider to the Generic Web URL provider
This commit is contained in:
parent
7feba634b8
commit
071f6f8591
2 changed files with 44 additions and 1 deletions
32
src/Exceptions/ProviderIDNotSupportedException.php
Normal file
32
src/Exceptions/ProviderIDNotSupportedException.php
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2026 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\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,));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Services\InfoProviderSystem\Providers;
|
namespace App\Services\InfoProviderSystem\Providers;
|
||||||
|
|
||||||
|
use App\Exceptions\ProviderIDNotSupportedException;
|
||||||
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;
|
||||||
|
|
@ -71,9 +72,12 @@ class GenericWebProvider implements InfoProviderInterface
|
||||||
|
|
||||||
public function searchByKeyword(string $keyword): array
|
public function searchByKeyword(string $keyword): array
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
return [
|
return [
|
||||||
$this->getDetails($keyword)
|
$this->getDetails($keyword)
|
||||||
];
|
]; } catch (ProviderIDNotSupportedException $e) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function extractShopName(string $url): string
|
private function extractShopName(string $url): string
|
||||||
|
|
@ -212,6 +216,13 @@ class GenericWebProvider implements InfoProviderInterface
|
||||||
|
|
||||||
$url = $id;
|
$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
|
//Try to get the webpage content
|
||||||
$response = $this->httpClient->request('GET', $url);
|
$response = $this->httpClient->request('GET', $url);
|
||||||
$content = $response->getContent();
|
$content = $response->getContent();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue