Encapsulate the fieldmapping data in the importjob further

This commit is contained in:
Jan Böhmer 2025-09-21 17:41:56 +02:00
parent eda6deff47
commit 16126c4000
3 changed files with 39 additions and 17 deletions

View file

@ -66,7 +66,7 @@ class BulkInfoProviderImportController extends AbstractController
{ {
$dtos = []; $dtos = [];
foreach ($fieldMappings as $mapping) { foreach ($fieldMappings as $mapping) {
$dtos[] = FieldMappingDTO::fromArray($mapping); $dtos[] = new FieldMappingDTO(field: $mapping['field'], providers: $mapping['providers'], priority: $mapping['priority'] ?? 1);
} }
return $dtos; return $dtos;
} }
@ -183,7 +183,7 @@ class BulkInfoProviderImportController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$formData = $form->getData(); $formData = $form->getData();
$fieldMappings = $formData['field_mappings']; $fieldMappingDtos = $this->convertFieldMappingsToDto($formData['field_mappings']);
$prefetchDetails = $formData['prefetch_details'] ?? false; $prefetchDetails = $formData['prefetch_details'] ?? false;
$user = $this->getUser(); $user = $this->getUser();
@ -200,7 +200,7 @@ class BulkInfoProviderImportController extends AbstractController
// Create and save the job // Create and save the job
$job = new BulkInfoProviderImportJob(); $job = new BulkInfoProviderImportJob();
$job->setFieldMappings($fieldMappings); $job->setFieldMappings($fieldMappingDtos);
$job->setPrefetchDetails($prefetchDetails); $job->setPrefetchDetails($prefetchDetails);
$job->setCreatedBy($user); $job->setCreatedBy($user);
@ -213,7 +213,6 @@ class BulkInfoProviderImportController extends AbstractController
$this->entityManager->flush(); $this->entityManager->flush();
try { try {
$fieldMappingDtos = $this->convertFieldMappingsToDto($fieldMappings);
$searchResultsDto = $this->bulkService->performBulkSearch($parts, $fieldMappingDtos, $prefetchDetails); $searchResultsDto = $this->bulkService->performBulkSearch($parts, $fieldMappingDtos, $prefetchDetails);
// Save search results to job // Save search results to job

View file

@ -26,7 +26,7 @@ use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\UserSystem\User; use App\Entity\UserSystem\User;
use App\Services\InfoProviderSystem\DTOs\BulkSearchResponseDTO; use App\Services\InfoProviderSystem\DTOs\BulkSearchResponseDTO;
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO; use App\Services\InfoProviderSystem\DTOs\FieldMappingDTO;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types; use Doctrine\DBAL\Types\Types;
@ -43,6 +43,11 @@ class BulkInfoProviderImportJob extends AbstractDBElement
#[ORM\Column(type: Types::JSON)] #[ORM\Column(type: Types::JSON)]
private array $fieldMappings = []; private array $fieldMappings = [];
/**
* @var FieldMappingDTO[] The deserialized field mappings DTOs, cached for performance
*/
private ?array $fieldMappingsDTO = null;
#[ORM\Column(type: Types::JSON)] #[ORM\Column(type: Types::JSON)]
private array $searchResults = []; private array $searchResults = [];
@ -150,14 +155,39 @@ class BulkInfoProviderImportJob extends AbstractDBElement
return $this; return $this;
} }
/**
* @return FieldMappingDTO[] The deserialized field mappings
*/
public function getFieldMappings(): array public function getFieldMappings(): array
{ {
return $this->fieldMappings; if ($this->fieldMappingsDTO === null) {
// Lazy load the DTOs from the raw JSON data
$this->fieldMappingsDTO = array_map(
static fn($data) => FieldMappingDTO::fromSerializableArray($data),
$this->fieldMappings
);
} }
return $this->fieldMappingsDTO;
}
/**
* @param FieldMappingDTO[] $fieldMappings
* @return $this
*/
public function setFieldMappings(array $fieldMappings): self public function setFieldMappings(array $fieldMappings): self
{ {
$this->fieldMappings = $fieldMappings; //Ensure that we are dealing with the objects here
if (count($fieldMappings) > 0 && !$fieldMappings[0] instanceof FieldMappingDTO) {
throw new \InvalidArgumentException('Expected an array of FieldMappingDTO objects');
}
$this->fieldMappingsDTO = $fieldMappings;
$this->fieldMappings = array_map(
static fn(FieldMappingDTO $dto) => $dto->toSerializableArray(),
$fieldMappings
);
return $this; return $this;
} }
@ -260,13 +290,6 @@ class BulkInfoProviderImportJob extends AbstractDBElement
return $progress; return $progress;
} }
public function setProgress(array $progress): self
{
// This method is kept for backward compatibility
// The progress is now managed through the jobParts relationship
return $this;
}
public function markAsCompleted(): self public function markAsCompleted(): self
{ {
$this->status = BulkImportJobStatus::COMPLETED; $this->status = BulkImportJobStatus::COMPLETED;

View file

@ -47,7 +47,7 @@ readonly class FieldMappingDTO
* Create a FieldMappingDTO from legacy array format. * Create a FieldMappingDTO from legacy array format.
* @param array{field: string, providers: string[], priority?: int} $data * @param array{field: string, providers: string[], priority?: int} $data
*/ */
public static function fromArray(array $data): self public static function fromSerializableArray(array $data): self
{ {
return new self( return new self(
field: $data['field'], field: $data['field'],
@ -60,7 +60,7 @@ readonly class FieldMappingDTO
* Convert this DTO to the legacy array format for backwards compatibility. * Convert this DTO to the legacy array format for backwards compatibility.
* @return array{field: string, providers: string[], priority: int} * @return array{field: string, providers: string[], priority: int}
*/ */
public function toArray(): array public function toSerializableArray(): array
{ {
return [ return [
'field' => $this->field, 'field' => $this->field,