mappingEngine = $mappingEngine; } public function resolve(ProductDTO $productDTO): ?CategoryDTO { $result = $this->mappingEngine->map($productDTO, new MappingContext([ 'source' => 'ProductIntelligence', 'target' => 'category_suggestion', ])); $mappedProduct = $result->getData(); if ($mappedProduct instanceof ProductDTO && $mappedProduct->getCategories() !== []) { $category = $mappedProduct->getCategories()[0]; if ($category instanceof CategoryDTO) { return $category; } } if ($productDTO->getCategories() !== []) { $category = $productDTO->getCategories()[0]; if ($category instanceof CategoryDTO) { return $category; } } $mediaType = $this->mediaType($productDTO); $macroCategory = ProductMacroCategory::defaultForMediaType($mediaType); $label = ProductMacroCategory::label($macroCategory, $mediaType); $slug = trim((string) preg_replace('/[^a-z0-9]+/i', '-', strtolower($label)), '-'); return new CategoryDTO( $label, $slug !== '' ? $slug : null, 'Categoria suggerita automaticamente dal motore di intelligence.' ); } private function mediaType(ProductDTO $product): string { foreach ($product->getAttributes() as $attribute) { if ($attribute instanceof AttributeDTO && strtolower(trim($attribute->getName())) === 'media_type') { return MediaType::normalize($attribute->getValue()); } } return MediaType::UNKNOWN; } }