Renamed dto to make their relation to batch searches more clear

This commit is contained in:
Jan Böhmer 2025-09-21 17:49:00 +02:00
parent 16126c4000
commit 92cd645945
9 changed files with 53 additions and 54 deletions

View file

@ -6,10 +6,10 @@ namespace App\Services\InfoProviderSystem;
use App\Entity\Parts\Part;
use App\Entity\Parts\Supplier;
use App\Services\InfoProviderSystem\DTOs\BulkSearchResultDTO;
use App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO;
use App\Services\InfoProviderSystem\DTOs\BulkSearchResponseDTO;
use App\Services\InfoProviderSystem\DTOs\FieldMappingDTO;
use App\Services\InfoProviderSystem\DTOs\PartSearchResultsDTO;
use App\Services\InfoProviderSystem\DTOs\BulkSearchFieldMappingDTO;
use App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultsDTO;
use App\Services\InfoProviderSystem\Providers\BatchInfoProviderInterface;
use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
use Doctrine\ORM\EntityManagerInterface;
@ -33,7 +33,7 @@ final class BulkInfoProviderService
* Perform bulk search across multiple parts and providers.
*
* @param Part[] $parts Array of parts to search for
* @param FieldMappingDTO[] $fieldMappings Array of field mappings defining search strategy
* @param BulkSearchFieldMappingDTO[] $fieldMappings Array of field mappings defining search strategy
* @param bool $prefetchDetails Whether to prefetch detailed information for results
* @return BulkSearchResponseDTO Structured response containing all search results
* @throws \InvalidArgumentException If no valid parts provided
@ -92,7 +92,7 @@ final class BulkInfoProviderService
$searchResults = $this->formatSearchResults($allResults);
}
$partResults[] = new PartSearchResultsDTO(
$partResults[] = new BulkSearchPartResultsDTO(
part: $part,
searchResults: $searchResults,
errors: []
@ -117,9 +117,9 @@ final class BulkInfoProviderService
* Process parts using batch-capable info providers.
*
* @param Part[] $parts Array of parts to search for
* @param FieldMappingDTO[] $fieldMappings Array of field mapping configurations
* @param BulkSearchFieldMappingDTO[] $fieldMappings Array of field mapping configurations
* @param array<string, BatchInfoProviderInterface> $batchProviders Batch providers indexed by key
* @return array<int, BulkSearchResultDTO[]> Results indexed by part ID
* @return array<int, BulkSearchPartResultDTO[]> Results indexed by part ID
*/
private function processBatchProviders(array $parts, array $fieldMappings, array $batchProviders): array
{
@ -145,7 +145,7 @@ final class BulkInfoProviderService
$keyword = $this->getKeywordFromField($part, $mapping->field);
if ($keyword && isset($providerResults[$keyword])) {
foreach ($providerResults[$keyword] as $dto) {
$batchResults[$part->getId()][] = new BulkSearchResultDTO(
$batchResults[$part->getId()][] = new BulkSearchPartResultDTO(
searchResult: $dto,
sourceField: $mapping->field,
sourceKeyword: $keyword,
@ -171,10 +171,10 @@ final class BulkInfoProviderService
* Process parts using regular (non-batch) info providers.
*
* @param Part[] $parts Array of parts to search for
* @param FieldMappingDTO[] $fieldMappings Array of field mapping configurations
* @param BulkSearchFieldMappingDTO[] $fieldMappings Array of field mapping configurations
* @param array<string, InfoProviderInterface> $regularProviders Regular providers indexed by key
* @param array<int, BulkSearchResultDTO[]> $excludeResults Results to exclude (from batch processing)
* @return array<int, BulkSearchResultDTO[]> Results indexed by part ID
* @param array<int, BulkSearchPartResultDTO[]> $excludeResults Results to exclude (from batch processing)
* @return array<int, BulkSearchPartResultDTO[]> Results indexed by part ID
*/
private function processRegularProviders(array $parts, array $fieldMappings, array $regularProviders, array $excludeResults): array
{
@ -204,7 +204,7 @@ final class BulkInfoProviderService
$dtos = $this->infoRetriever->searchByKeyword($keyword, $providers);
foreach ($dtos as $dto) {
$regularResults[$part->getId()][] = new BulkSearchResultDTO(
$regularResults[$part->getId()][] = new BulkSearchPartResultDTO(
searchResult: $dto,
sourceField: $mapping->field,
sourceKeyword: $keyword,
@ -229,7 +229,7 @@ final class BulkInfoProviderService
* Collect unique keywords for a specific provider from all parts and field mappings.
*
* @param Part[] $parts Array of parts to collect keywords from
* @param FieldMappingDTO[] $fieldMappings Array of field mapping configurations
* @param BulkSearchFieldMappingDTO[] $fieldMappings Array of field mapping configurations
* @param string $providerKey The provider key to collect keywords for
* @return string[] Array of unique keywords
*/
@ -326,8 +326,8 @@ final class BulkInfoProviderService
/**
* Format and deduplicate search results.
*
* @param BulkSearchResultDTO[] $bulkResults Array of bulk search results
* @return BulkSearchResultDTO[] Array of formatted search results with metadata
* @param BulkSearchPartResultDTO[] $bulkResults Array of bulk search results
* @return BulkSearchPartResultDTO[] Array of formatted search results with metadata
*/
private function formatSearchResults(array $bulkResults): array
{

View file

@ -24,9 +24,8 @@ namespace App\Services\InfoProviderSystem\DTOs;
/**
* Represents a mapping between a part field and the info providers that should search in that field.
* This DTO provides type safety and better structure than raw arrays for field mapping configuration.
*/
readonly class FieldMappingDTO
readonly class BulkSearchFieldMappingDTO
{
/**
* @param string $field The field to search in (e.g., 'mpn', 'name', or supplier-specific fields like 'digikey_spn')

View file

@ -25,9 +25,9 @@ namespace App\Services\InfoProviderSystem\DTOs;
use App\Entity\Parts\Part;
/**
* Represents a search result from bulk search with additional context information, like how the part was found.
* Represents a single search result from bulk search with additional context information, like how the part was found.
*/
readonly class BulkSearchResultDTO
readonly class BulkSearchPartResultDTO
{
public function __construct(
/** The base search result DTO containing provider data */

View file

@ -26,13 +26,13 @@ use App\Entity\Parts\Part;
/**
* Represents the search results for a single part from bulk info provider search.
* This DTO provides type safety and clear structure for part search results.
* It contains multiple search results, that match the part.
*/
readonly class PartSearchResultsDTO
readonly class BulkSearchPartResultsDTO
{
/**
* @param Part $part The part that was searched for
* @param BulkSearchResultDTO[] $searchResults Array of search results found for this part
* @param BulkSearchPartResultDTO[] $searchResults Array of search results found for this part
* @param string[] $errors Array of error messages encountered during search
*/
public function __construct(
@ -72,12 +72,12 @@ readonly class PartSearchResultsDTO
/**
* Get search results sorted by priority (ascending).
* @return BulkSearchResultDTO[]
* @return BulkSearchPartResultDTO[]
*/
public function getResultsSortedByPriority(): array
{
$results = $this->searchResults;
usort($results, static fn(BulkSearchResultDTO $a, BulkSearchResultDTO $b) => $a->priority <=> $b->priority);
usort($results, static fn(BulkSearchPartResultDTO $a, BulkSearchPartResultDTO $b) => $a->priority <=> $b->priority);
return $results;
}
}

View file

@ -32,7 +32,7 @@ use Doctrine\ORM\EntityManagerInterface;
readonly class BulkSearchResponseDTO implements \ArrayAccess
{
/**
* @param PartSearchResultsDTO[] $partResults Array of search results for each part
* @param BulkSearchPartResultsDTO[] $partResults Array of search results for each part
*/
public function __construct(
public array $partResults
@ -41,10 +41,10 @@ readonly class BulkSearchResponseDTO implements \ArrayAccess
/**
* Replaces the search results for a specific part, and returns a new instance.
* @param Part|int $part
* @param PartSearchResultsDTO $new_results
* @param BulkSearchPartResultsDTO $new_results
* @return BulkSearchResponseDTO
*/
public function replaceResultsForPart(Part|int $part, PartSearchResultsDTO $new_results): self
public function replaceResultsForPart(Part|int $part, BulkSearchPartResultsDTO $new_results): self
{
$array = $this->partResults;
foreach ($array as $index => $partResult) {
@ -85,7 +85,7 @@ readonly class BulkSearchResponseDTO implements \ArrayAccess
/**
* Get all parts that have search results.
* @return PartSearchResultsDTO[]
* @return BulkSearchPartResultsDTO[]
*/
public function getPartsWithResults(): array
{
@ -94,7 +94,7 @@ readonly class BulkSearchResponseDTO implements \ArrayAccess
/**
* Get all parts that have errors.
* @return PartSearchResultsDTO[]
* @return BulkSearchPartResultsDTO[]
*/
public function getPartsWithErrors(): array
{
@ -175,9 +175,9 @@ readonly class BulkSearchResponseDTO implements \ArrayAccess
{
$partResults = [];
foreach ($data as $partData) {
$partResults[] = new PartSearchResultsDTO(
$partResults[] = new BulkSearchPartResultsDTO(
part: $entityManager->getReference(Part::class, $partData['part_id']),
searchResults: array_map(fn($result) => new BulkSearchResultDTO(
searchResults: array_map(fn($result) => new BulkSearchPartResultDTO(
searchResult: SearchResultDTO::fromNormalizedSearchResultArray($result['dto']),
sourceField: $result['source_field'] ?? null,
sourceKeyword: $result['source_keyword'] ?? null,
@ -199,7 +199,7 @@ readonly class BulkSearchResponseDTO implements \ArrayAccess
return isset($this->partResults[$offset]);
}
public function offsetGet(mixed $offset): ?PartSearchResultsDTO
public function offsetGet(mixed $offset): ?BulkSearchPartResultsDTO
{
if (!is_int($offset)) {
throw new \InvalidArgumentException("Offset must be an integer.");