|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 4 | + * |
| 5 | + * Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics) |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | + |
| 24 | +namespace App\Services\InfoProviderSystem\Providers; |
| 25 | + |
| 26 | +use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; |
| 27 | +use App\Services\InfoProviderSystem\DTOs\SearchResultDTO; |
| 28 | +use App\Settings\InfoProviderSystem\ConradSettings; |
| 29 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 30 | + |
| 31 | +readonly class ConradProvider implements InfoProviderInterface |
| 32 | +{ |
| 33 | + |
| 34 | + private const SEARCH_ENDPOINT = 'https://api.conrad.de/search/1/v3/facetSearch'; |
| 35 | + |
| 36 | + public function __construct(private HttpClientInterface $httpClient, private ConradSettings $settings) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + public function getProviderInfo(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + 'name' => 'Pollin', |
| 44 | + 'description' => 'Retrieves part information from conrad.de', |
| 45 | + 'url' => 'https://www.conrad.de/', |
| 46 | + 'disabled_help' => 'Set API key in settings', |
| 47 | + 'settings_class' => ConradSettings::class, |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + public function getProviderKey(): string |
| 52 | + { |
| 53 | + return 'conrad'; |
| 54 | + } |
| 55 | + |
| 56 | + public function isActive(): bool |
| 57 | + { |
| 58 | + return !empty($this->settings->apiKey); |
| 59 | + } |
| 60 | + |
| 61 | + public function searchByKeyword(string $keyword): array |
| 62 | + { |
| 63 | + $url = self::SEARCH_ENDPOINT . '/' . $this->settings->country . '/' . $this->settings->language . '/' . $this->settings->customerType; |
| 64 | + |
| 65 | + $response = $this->httpClient->request('POST', $url, [ |
| 66 | + 'query' => [ |
| 67 | + 'apikey' => $this->settings->apiKey, |
| 68 | + ], |
| 69 | + 'json' => [ |
| 70 | + 'query' => $keyword, |
| 71 | + ], |
| 72 | + ]); |
| 73 | + |
| 74 | + $out = []; |
| 75 | + $results = $response->toArray(); |
| 76 | + |
| 77 | + foreach($results as $result) { |
| 78 | + $out[] = new SearchResultDTO( |
| 79 | + provider_key: $this->getProviderKey(), |
| 80 | + provider_id: $result['productId'], |
| 81 | + name: $result['title'], |
| 82 | + description: '', |
| 83 | + manufacturer: $result['brand']['name'] ?? null, |
| 84 | + mpn: $result['manufacturerId'] ?? null, |
| 85 | + preview_image_url: $result['image'] ?? null, |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + return $out; |
| 90 | + } |
| 91 | + |
| 92 | + public function getDetails(string $id): PartDetailDTO |
| 93 | + { |
| 94 | + // TODO: Implement getDetails() method. |
| 95 | + } |
| 96 | + |
| 97 | + public function getCapabilities(): array |
| 98 | + { |
| 99 | + return [ProviderCapabilities::BASIC, |
| 100 | + ProviderCapabilities::PICTURE, |
| 101 | + ProviderCapabilities::PRICE,]; |
| 102 | + } |
| 103 | +} |
0 commit comments