Optimized LCSC batch search calls and extracted it into interface for potential general use in the future

This commit is contained in:
Jan Böhmer 2025-08-31 23:41:16 +02:00
parent 4fcd55748f
commit 52444e05e4
3 changed files with 60 additions and 28 deletions

View file

@ -30,6 +30,8 @@ use App\Entity\Parts\Supplier;
use App\Form\InfoProviderSystem\GlobalFieldMappingType; use App\Form\InfoProviderSystem\GlobalFieldMappingType;
use App\Services\InfoProviderSystem\PartInfoRetriever; use App\Services\InfoProviderSystem\PartInfoRetriever;
use App\Services\InfoProviderSystem\ExistingPartFinder; use App\Services\InfoProviderSystem\ExistingPartFinder;
use App\Services\InfoProviderSystem\ProviderRegistry;
use App\Services\InfoProviderSystem\Providers\LCSCProvider;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -44,6 +46,7 @@ class BulkInfoProviderImportController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly PartInfoRetriever $infoRetriever, private readonly PartInfoRetriever $infoRetriever,
private readonly LCSCProvider $LCSCProvider,
private readonly ExistingPartFinder $existingPartFinder, private readonly ExistingPartFinder $existingPartFinder,
private readonly EntityManagerInterface $entityManager private readonly EntityManagerInterface $entityManager
) { ) {
@ -619,18 +622,7 @@ class BulkInfoProviderImportController extends AbstractController
*/ */
private function searchLcscBatch(array $keywords): array private function searchLcscBatch(array $keywords): array
{ {
// Get LCSC provider through reflection since PartInfoRetriever doesn't expose it return $this->LCSCProvider->searchByKeywordsBatch($keywords);
$reflection = new \ReflectionClass($this->infoRetriever);
$registryProp = $reflection->getProperty('provider_registry');
$registryProp->setAccessible(true);
$registry = $registryProp->getValue($this->infoRetriever);
$lcscProvider = $registry->getProviderByKey('lcsc');
if ($lcscProvider && method_exists($lcscProvider, 'searchByKeywordsBatch')) {
return $lcscProvider->searchByKeywordsBatch($keywords);
}
return [];
} }
#[Route('/job/{jobId}/part/{partId}/mark-completed', name: 'bulk_info_provider_mark_completed', methods: ['POST'])] #[Route('/job/{jobId}/part/{partId}/mark-completed', name: 'bulk_info_provider_mark_completed', methods: ['POST'])]

View file

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 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\Services\InfoProviderSystem\Providers;
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
/**
* This interface marks a provider as a info provider which can provide information directly in batch operations
*/
interface BatchInfoProviderInterface extends InfoProviderInterface
{
/**
* Search for multiple keywords in a single batch operation and return the results, ordered by the keywords.
* This allows for a more efficient search compared to running multiple single searches.
* @param string[] $keywords
* @return array<string, SearchResultDTO[]> An associative array where the key is the keyword and the value is the search results for that keyword
*/
public function searchByKeywordsBatch(array $keywords): array;
}

View file

@ -33,7 +33,7 @@ use App\Settings\InfoProviderSystem\LCSCSettings;
use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
class LCSCProvider implements InfoProviderInterface class LCSCProvider implements BatchInfoProviderInterface
{ {
private const ENDPOINT_URL = 'https://wmsc.lcsc.com/ftps/wm'; private const ENDPOINT_URL = 'https://wmsc.lcsc.com/ftps/wm';