From 16126c4000401f92c4c4a09e50a66375012637a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 21 Sep 2025 17:41:56 +0200 Subject: [PATCH] Encapsulate the fieldmapping data in the importjob further --- .../BulkInfoProviderImportController.php | 7 ++- src/Entity/BulkInfoProviderImportJob.php | 43 ++++++++++++++----- .../DTOs/FieldMappingDTO.php | 6 +-- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/Controller/BulkInfoProviderImportController.php b/src/Controller/BulkInfoProviderImportController.php index 11353202..ee8658e6 100644 --- a/src/Controller/BulkInfoProviderImportController.php +++ b/src/Controller/BulkInfoProviderImportController.php @@ -66,7 +66,7 @@ class BulkInfoProviderImportController extends AbstractController { $dtos = []; foreach ($fieldMappings as $mapping) { - $dtos[] = FieldMappingDTO::fromArray($mapping); + $dtos[] = new FieldMappingDTO(field: $mapping['field'], providers: $mapping['providers'], priority: $mapping['priority'] ?? 1); } return $dtos; } @@ -183,7 +183,7 @@ class BulkInfoProviderImportController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $formData = $form->getData(); - $fieldMappings = $formData['field_mappings']; + $fieldMappingDtos = $this->convertFieldMappingsToDto($formData['field_mappings']); $prefetchDetails = $formData['prefetch_details'] ?? false; $user = $this->getUser(); @@ -200,7 +200,7 @@ class BulkInfoProviderImportController extends AbstractController // Create and save the job $job = new BulkInfoProviderImportJob(); - $job->setFieldMappings($fieldMappings); + $job->setFieldMappings($fieldMappingDtos); $job->setPrefetchDetails($prefetchDetails); $job->setCreatedBy($user); @@ -213,7 +213,6 @@ class BulkInfoProviderImportController extends AbstractController $this->entityManager->flush(); try { - $fieldMappingDtos = $this->convertFieldMappingsToDto($fieldMappings); $searchResultsDto = $this->bulkService->performBulkSearch($parts, $fieldMappingDtos, $prefetchDetails); // Save search results to job diff --git a/src/Entity/BulkInfoProviderImportJob.php b/src/Entity/BulkInfoProviderImportJob.php index 0cd97841..69570690 100644 --- a/src/Entity/BulkInfoProviderImportJob.php +++ b/src/Entity/BulkInfoProviderImportJob.php @@ -26,7 +26,7 @@ use App\Entity\Base\AbstractDBElement; use App\Entity\Parts\Part; use App\Entity\UserSystem\User; 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\Collection; use Doctrine\DBAL\Types\Types; @@ -43,6 +43,11 @@ class BulkInfoProviderImportJob extends AbstractDBElement #[ORM\Column(type: Types::JSON)] private array $fieldMappings = []; + /** + * @var FieldMappingDTO[] The deserialized field mappings DTOs, cached for performance + */ + private ?array $fieldMappingsDTO = null; + #[ORM\Column(type: Types::JSON)] private array $searchResults = []; @@ -150,14 +155,39 @@ class BulkInfoProviderImportJob extends AbstractDBElement return $this; } + /** + * @return FieldMappingDTO[] The deserialized field mappings + */ 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 { - $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; } @@ -260,13 +290,6 @@ class BulkInfoProviderImportJob extends AbstractDBElement 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 { $this->status = BulkImportJobStatus::COMPLETED; diff --git a/src/Services/InfoProviderSystem/DTOs/FieldMappingDTO.php b/src/Services/InfoProviderSystem/DTOs/FieldMappingDTO.php index cdbe4bc0..ec51ebb2 100644 --- a/src/Services/InfoProviderSystem/DTOs/FieldMappingDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/FieldMappingDTO.php @@ -47,7 +47,7 @@ readonly class FieldMappingDTO * Create a FieldMappingDTO from legacy array format. * @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( field: $data['field'], @@ -60,7 +60,7 @@ readonly class FieldMappingDTO * Convert this DTO to the legacy array format for backwards compatibility. * @return array{field: string, providers: string[], priority: int} */ - public function toArray(): array + public function toSerializableArray(): array { return [ 'field' => $this->field, @@ -89,4 +89,4 @@ readonly class FieldMappingDTO return substr($this->field, 0, -4); } -} \ No newline at end of file +}