platform/src/Core/Content/ImportExport/Event/Subscriber/ProductCategoryPathsSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\ImportExport\Event\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\ImportExport\Event\ImportExportBeforeImportRecordEvent;
  5. use Shopware\Core\Content\ImportExport\Exception\ProcessingException;
  6. use Shopware\Core\Content\Product\ProductDefinition;
  7. use Shopware\Core\Framework\Api\Sync\SyncBehavior;
  8. use Shopware\Core\Framework\Api\Sync\SyncOperation;
  9. use Shopware\Core\Framework\Api\Sync\SyncServiceInterface;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Feature;
  15. use Shopware\Core\Framework\Uuid\Uuid;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProductCategoryPathsSubscriber implements EventSubscriberInterface
  18. {
  19.     private EntityRepositoryInterface $categoryRepository;
  20.     private SyncServiceInterface $syncService;
  21.     private array $categoryIdCache = [];
  22.     public function __construct(EntityRepositoryInterface $categoryRepositorySyncServiceInterface $syncService)
  23.     {
  24.         $this->categoryRepository $categoryRepository;
  25.         $this->syncService $syncService;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             ImportExportBeforeImportRecordEvent::class => 'categoryPathsToAssignment',
  31.         ];
  32.     }
  33.     public function categoryPathsToAssignment(ImportExportBeforeImportRecordEvent $event): void
  34.     {
  35.         $row $event->getRow();
  36.         $entityName $event->getConfig()->get('sourceEntity');
  37.         if ($entityName !== ProductDefinition::ENTITY_NAME || empty($row['category_paths'])) {
  38.             return;
  39.         }
  40.         $result = [];
  41.         $categoriesPaths explode('|'$row['category_paths']);
  42.         $newCategoriesPayload = [];
  43.         foreach ($categoriesPaths as $path) {
  44.             $categories explode('>'$path);
  45.             $categoryId null;
  46.             foreach ($categories as $currentIndex => $categoryName) {
  47.                 if (empty($categoryName)) {
  48.                     continue;
  49.                 }
  50.                 $partialPath implode('>'\array_slice($categories0$currentIndex 1));
  51.                 if (isset($this->categoryIdCache[$partialPath])) {
  52.                     $categoryId $this->categoryIdCache[$partialPath];
  53.                     continue;
  54.                 }
  55.                 $criteria = new Criteria();
  56.                 $criteria->addFilter(new EqualsFilter('name'$categoryName));
  57.                 $criteria->addFilter(new EqualsFilter('parentId'$categoryId));
  58.                 $category $this->categoryRepository->search($criteriaContext::createDefaultContext())->first();
  59.                 if ($category === null && $categoryId === null) {
  60.                     break;
  61.                 }
  62.                 if ($category !== null) {
  63.                     $categoryId $category->getId();
  64.                     $this->categoryIdCache[$partialPath] = $categoryId;
  65.                     continue;
  66.                 }
  67.                 $parentId $categoryId;
  68.                 $categoryId Uuid::fromStringToHex($partialPath);
  69.                 $this->categoryIdCache[$partialPath] = $categoryId;
  70.                 $newCategoriesPayload[] = [
  71.                     'id' => $categoryId,
  72.                     'parent' => ['id' => $parentId],
  73.                     'name' => $categoryName,
  74.                 ];
  75.             }
  76.             if ($categoryId !== null) {
  77.                 $result[] = ['id' => $categoryId];
  78.             }
  79.         }
  80.         if (!empty($newCategoriesPayload)) {
  81.             $this->createNewCategories($newCategoriesPayload$row['category_paths']);
  82.         }
  83.         $record $event->getRecord();
  84.         $record['categories'] = !empty($record['categories']) ? array_merge($record['categories'], $result) : $result;
  85.         $event->setRecord($record);
  86.     }
  87.     private function createNewCategories(array $payloadstring $categoryPaths): void
  88.     {
  89.         if (Feature::isActive('FEATURE_NEXT_15815')) {
  90.             $behavior = new SyncBehavior();
  91.         } else {
  92.             $behavior = new SyncBehavior(truetrue);
  93.         }
  94.         $result $this->syncService->sync([
  95.             new SyncOperation(
  96.                 'write',
  97.                 CategoryDefinition::ENTITY_NAME,
  98.                 SyncOperation::ACTION_UPSERT,
  99.                 $payload
  100.             ),
  101.         ], Context::createDefaultContext(), $behavior);
  102.         if (Feature::isActive('FEATURE_NEXT_15815')) {
  103.             // @internal (flag:FEATURE_NEXT_15815) - remove code below, "isSuccess" function will be removed, simply return because sync service would throw an exception in error case
  104.             return;
  105.         }
  106.         if (!$result->isSuccess()) {
  107.             $operation $result->get('write');
  108.             throw new ProcessingException(sprintf(
  109.                 'Failed writing categories for path %s with errors: %s',
  110.                 $categoryPaths,
  111.                 $operation json_encode(array_column($operation->getResult(), 'errors')) : ''
  112.             ));
  113.         }
  114.     }
  115. }