mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-01-15 06:39:33 +00:00
Refactor bulk info provider: replace complex arrays with DTOs
- Add BulkSearchResponseDTO, FieldMappingDTO for type safety - Use composition instead of inheritance in BulkSearchResultDTO - Remove unnecessary BulkSearchRequestDTO - Fix N+1 queries and API error handling - Fix Add Mapping button functionality
This commit is contained in:
parent
8998b006e0
commit
2c195d9767
15 changed files with 838 additions and 195 deletions
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\InfoProviderSystem\DTOs;
|
||||
|
||||
use App\Entity\Parts\Part;
|
||||
|
||||
readonly class BulkSearchRequestDTO
|
||||
{
|
||||
/**
|
||||
* @param array $fieldMappings
|
||||
* @param bool $prefetchDetails
|
||||
* @param Part[] $parts The parts for which the bulk search should be performed.
|
||||
*/
|
||||
public function __construct(
|
||||
public array $fieldMappings,
|
||||
public bool $prefetchDetails = false,
|
||||
public array $parts = []
|
||||
) {}
|
||||
}
|
||||
122
src/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTO.php
Normal file
122
src/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTO.php
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 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\DTOs;
|
||||
|
||||
/**
|
||||
* Represents the complete response from a bulk info provider search operation.
|
||||
* This DTO provides type safety and clear structure instead of complex arrays.
|
||||
*/
|
||||
readonly class BulkSearchResponseDTO
|
||||
{
|
||||
/**
|
||||
* @param PartSearchResultDTO[] $partResults Array of search results for each part
|
||||
*/
|
||||
public function __construct(
|
||||
public array $partResults
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create from legacy array format for backwards compatibility.
|
||||
* @param array $data Array of part result arrays in legacy format
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$partResults = [];
|
||||
foreach ($data as $partData) {
|
||||
$partResults[] = PartSearchResultDTO::fromArray($partData);
|
||||
}
|
||||
|
||||
return new self($partResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to legacy array format for backwards compatibility.
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->partResults as $partResult) {
|
||||
$result[] = $partResult->toArray();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if any parts have search results.
|
||||
*/
|
||||
public function hasAnyResults(): bool
|
||||
{
|
||||
foreach ($this->partResults as $partResult) {
|
||||
if ($partResult->hasResults()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total number of search results across all parts.
|
||||
*/
|
||||
public function getTotalResultCount(): int
|
||||
{
|
||||
$count = 0;
|
||||
foreach ($this->partResults as $partResult) {
|
||||
$count += $partResult->getResultCount();
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all parts that have search results.
|
||||
* @return PartSearchResultDTO[]
|
||||
*/
|
||||
public function getPartsWithResults(): array
|
||||
{
|
||||
return array_filter($this->partResults, fn($result) => $result->hasResults());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all parts that have errors.
|
||||
* @return PartSearchResultDTO[]
|
||||
*/
|
||||
public function getPartsWithErrors(): array
|
||||
{
|
||||
return array_filter($this->partResults, fn($result) => $result->hasErrors());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of parts processed.
|
||||
*/
|
||||
public function getPartCount(): int
|
||||
{
|
||||
return count($this->partResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of parts with successful results.
|
||||
*/
|
||||
public function getSuccessfulPartCount(): int
|
||||
{
|
||||
return count($this->getPartsWithResults());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,32 +1,139 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 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\DTOs;
|
||||
|
||||
use App\Entity\Parts\ManufacturingStatus;
|
||||
use App\Entity\Parts\Part;
|
||||
|
||||
class BulkSearchResultDTO extends SearchResultDTO
|
||||
/**
|
||||
* Represents a search result from bulk search with additional context information.
|
||||
* Uses composition instead of inheritance for better maintainability.
|
||||
*/
|
||||
readonly class BulkSearchResultDTO
|
||||
{
|
||||
public function __construct(
|
||||
SearchResultDTO $baseDto,
|
||||
public readonly ?string $sourceField = null,
|
||||
public readonly ?string $sourceKeyword = null,
|
||||
public readonly ?Part $localPart = null,
|
||||
public readonly int $priority = 1
|
||||
) {
|
||||
parent::__construct(
|
||||
provider_key: $baseDto->provider_key,
|
||||
provider_id: $baseDto->provider_id,
|
||||
name: $baseDto->name,
|
||||
description: $baseDto->description,
|
||||
category: $baseDto->category,
|
||||
manufacturer: $baseDto->manufacturer,
|
||||
mpn: $baseDto->mpn,
|
||||
preview_image_url: $baseDto->preview_image_url,
|
||||
manufacturing_status: $baseDto->manufacturing_status,
|
||||
provider_url: $baseDto->provider_url,
|
||||
footprint: $baseDto->footprint
|
||||
);
|
||||
/** The base search result DTO containing provider data */
|
||||
public SearchResultDTO $baseDto,
|
||||
/** The field that was used to find this result */
|
||||
public ?string $sourceField = null,
|
||||
/** The actual keyword that was searched for */
|
||||
public ?string $sourceKeyword = null,
|
||||
/** Local part that matches this search result, if any */
|
||||
public ?Part $localPart = null,
|
||||
/** Priority for this search result */
|
||||
public int $priority = 1
|
||||
) {}
|
||||
|
||||
// Delegation methods for SearchResultDTO properties
|
||||
public function getProviderKey(): string
|
||||
{
|
||||
return $this->baseDto->provider_key;
|
||||
}
|
||||
|
||||
public function getProviderId(): string
|
||||
{
|
||||
return $this->baseDto->provider_id;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->baseDto->name;
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->baseDto->description;
|
||||
}
|
||||
|
||||
public function getCategory(): ?string
|
||||
{
|
||||
return $this->baseDto->category;
|
||||
}
|
||||
|
||||
public function getManufacturer(): ?string
|
||||
{
|
||||
return $this->baseDto->manufacturer;
|
||||
}
|
||||
|
||||
public function getMpn(): ?string
|
||||
{
|
||||
return $this->baseDto->mpn;
|
||||
}
|
||||
|
||||
public function getPreviewImageUrl(): ?string
|
||||
{
|
||||
return $this->baseDto->preview_image_url;
|
||||
}
|
||||
|
||||
public function getPreviewImageFile(): ?FileDTO
|
||||
{
|
||||
return $this->baseDto->preview_image_file;
|
||||
}
|
||||
|
||||
public function getManufacturingStatus(): ?ManufacturingStatus
|
||||
{
|
||||
return $this->baseDto->manufacturing_status;
|
||||
}
|
||||
|
||||
public function getProviderUrl(): ?string
|
||||
{
|
||||
return $this->baseDto->provider_url;
|
||||
}
|
||||
|
||||
public function getFootprint(): ?string
|
||||
{
|
||||
return $this->baseDto->footprint;
|
||||
}
|
||||
|
||||
// Backwards compatibility properties for legacy code
|
||||
public function __get(string $name): mixed
|
||||
{
|
||||
return match ($name) {
|
||||
'provider_key' => $this->baseDto->provider_key,
|
||||
'provider_id' => $this->baseDto->provider_id,
|
||||
'name' => $this->baseDto->name,
|
||||
'description' => $this->baseDto->description,
|
||||
'category' => $this->baseDto->category,
|
||||
'manufacturer' => $this->baseDto->manufacturer,
|
||||
'mpn' => $this->baseDto->mpn,
|
||||
'preview_image_url' => $this->baseDto->preview_image_url,
|
||||
'preview_image_file' => $this->baseDto->preview_image_file,
|
||||
'manufacturing_status' => $this->baseDto->manufacturing_status,
|
||||
'provider_url' => $this->baseDto->provider_url,
|
||||
'footprint' => $this->baseDto->footprint,
|
||||
default => throw new \InvalidArgumentException("Property '{$name}' does not exist")
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic isset method for backwards compatibility.
|
||||
*/
|
||||
public function __isset(string $name): bool
|
||||
{
|
||||
return in_array($name, [
|
||||
'provider_key', 'provider_id', 'name', 'description', 'category',
|
||||
'manufacturer', 'mpn', 'preview_image_url', 'preview_image_file',
|
||||
'manufacturing_status', 'provider_url', 'footprint'
|
||||
], true);
|
||||
}
|
||||
}
|
||||
92
src/Services/InfoProviderSystem/DTOs/FieldMappingDTO.php
Normal file
92
src/Services/InfoProviderSystem/DTOs/FieldMappingDTO.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 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\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
|
||||
{
|
||||
/**
|
||||
* @param string $field The field to search in (e.g., 'mpn', 'name', or supplier-specific fields like 'digikey_spn')
|
||||
* @param string[] $providers Array of provider keys to search with (e.g., ['digikey', 'farnell'])
|
||||
* @param int $priority Priority for this field mapping (1-10, lower numbers = higher priority)
|
||||
*/
|
||||
public function __construct(
|
||||
public string $field,
|
||||
public array $providers,
|
||||
public int $priority = 1
|
||||
) {
|
||||
if ($priority < 1 || $priority > 10) {
|
||||
throw new \InvalidArgumentException('Priority must be between 1 and 10');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a FieldMappingDTO from legacy array format.
|
||||
* @param array{field: string, providers: string[], priority?: int} $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
field: $data['field'],
|
||||
providers: $data['providers'] ?? [],
|
||||
priority: $data['priority'] ?? 1
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert this DTO to the legacy array format for backwards compatibility.
|
||||
* @return array{field: string, providers: string[], priority: int}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'field' => $this->field,
|
||||
'providers' => $this->providers,
|
||||
'priority' => $this->priority,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this field mapping is for a supplier part number field.
|
||||
*/
|
||||
public function isSupplierPartNumberField(): bool
|
||||
{
|
||||
return str_ends_with($this->field, '_spn');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the supplier key from a supplier part number field.
|
||||
* Returns null if this is not a supplier part number field.
|
||||
*/
|
||||
public function getSupplierKey(): ?string
|
||||
{
|
||||
if (!$this->isSupplierPartNumberField()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return substr($this->field, 0, -4);
|
||||
}
|
||||
}
|
||||
114
src/Services/InfoProviderSystem/DTOs/PartSearchResultDTO.php
Normal file
114
src/Services/InfoProviderSystem/DTOs/PartSearchResultDTO.php
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 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\DTOs;
|
||||
|
||||
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.
|
||||
*/
|
||||
readonly class PartSearchResultDTO
|
||||
{
|
||||
/**
|
||||
* @param Part $part The part that was searched for
|
||||
* @param SearchResultWithMetadataDTO[] $searchResults Array of search results found for this part
|
||||
* @param string[] $errors Array of error messages encountered during search
|
||||
*/
|
||||
public function __construct(
|
||||
public Part $part,
|
||||
public array $searchResults = [],
|
||||
public array $errors = []
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create from legacy array format for backwards compatibility.
|
||||
* @param array{part: Part, search_results: array, errors: string[]} $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$searchResults = [];
|
||||
foreach ($data['search_results'] as $result) {
|
||||
$searchResults[] = SearchResultWithMetadataDTO::fromArray($result);
|
||||
}
|
||||
|
||||
return new self(
|
||||
part: $data['part'],
|
||||
searchResults: $searchResults,
|
||||
errors: $data['errors'] ?? []
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to legacy array format for backwards compatibility.
|
||||
* @return array{part: Part, search_results: array, errors: string[]}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$searchResults = [];
|
||||
foreach ($this->searchResults as $result) {
|
||||
$searchResults[] = $result->toArray();
|
||||
}
|
||||
|
||||
return [
|
||||
'part' => $this->part,
|
||||
'search_results' => $searchResults,
|
||||
'errors' => $this->errors,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this part has any search results.
|
||||
*/
|
||||
public function hasResults(): bool
|
||||
{
|
||||
return !empty($this->searchResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this part has any errors.
|
||||
*/
|
||||
public function hasErrors(): bool
|
||||
{
|
||||
return !empty($this->errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of search results for this part.
|
||||
*/
|
||||
public function getResultCount(): int
|
||||
{
|
||||
return count($this->searchResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get search results sorted by priority (ascending).
|
||||
* @return SearchResultWithMetadataDTO[]
|
||||
*/
|
||||
public function getResultsSortedByPriority(): array
|
||||
{
|
||||
$results = $this->searchResults;
|
||||
usort($results, fn($a, $b) => $a->getPriority() <=> $b->getPriority());
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 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\DTOs;
|
||||
|
||||
use App\Entity\Parts\Part;
|
||||
|
||||
/**
|
||||
* Represents a search result with additional metadata about how it was found.
|
||||
* This DTO encapsulates both the search result data and the context of the search.
|
||||
*/
|
||||
readonly class SearchResultWithMetadataDTO
|
||||
{
|
||||
public function __construct(
|
||||
/** The search result DTO containing part information from the provider */
|
||||
public BulkSearchResultDTO $searchResult,
|
||||
/** Local part that matches this search result, if any */
|
||||
public ?Part $localPart = null,
|
||||
/** The field that was used to find this result (e.g., 'mpn', 'name') */
|
||||
public ?string $sourceField = null,
|
||||
/** The actual keyword/value that was searched for */
|
||||
public ?string $sourceKeyword = null
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create from legacy array format for backwards compatibility.
|
||||
* @param array{dto: BulkSearchResultDTO, localPart: ?Part, source_field: string, source_keyword: string} $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
searchResult: $data['dto'],
|
||||
localPart: $data['localPart'] ?? null,
|
||||
sourceField: $data['source_field'] ?? null,
|
||||
sourceKeyword: $data['source_keyword'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to legacy array format for backwards compatibility.
|
||||
* @return array{dto: BulkSearchResultDTO, localPart: ?Part, source_field: ?string, source_keyword: ?string}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'dto' => $this->searchResult,
|
||||
'localPart' => $this->localPart,
|
||||
'source_field' => $this->sourceField,
|
||||
'source_keyword' => $this->sourceKeyword,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the priority of this search result.
|
||||
*/
|
||||
public function getPriority(): int
|
||||
{
|
||||
return $this->searchResult->priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the provider key from the search result.
|
||||
*/
|
||||
public function getProviderKey(): string
|
||||
{
|
||||
return $this->searchResult->getProviderKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the provider ID from the search result.
|
||||
*/
|
||||
public function getProviderId(): string
|
||||
{
|
||||
return $this->searchResult->getProviderId();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue