vendor/crehler/mojebambino-engine/src/Subscriber/ImportExportEventSubscriber.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\MojeBambinoEngine\Event\ImportExportIdSerializedEvent;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ImportExportEventSubscriber implements EventSubscriberInterface
  11. {
  12.     private EntityRepository $productRepository;
  13.     private EntityRepository $productCategoryRepository;
  14.     private EntityRepository $productPropertyRepository;
  15.     private EntityRepository $productOptionRepository;
  16.     private EntityRepository $productConfiguratorSettings;
  17.     public function __construct(
  18.         EntityRepository $productRepository,
  19.         EntityRepository $productPropertyRepository,
  20.         EntityRepository $productCategoryRepository,
  21.         EntityRepository $productOptionRepository,
  22.         EntityRepository $productConfiguratorSettings
  23.     )
  24.     {
  25.         $this->productRepository $productRepository;
  26.         $this->productPropertyRepository $productPropertyRepository;
  27.         $this->productCategoryRepository $productCategoryRepository;
  28.         $this->productOptionRepository $productOptionRepository;
  29.         $this->productConfiguratorSettings $productConfiguratorSettings;
  30.     }
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             ImportExportIdSerializedEvent::class => 'onIdSerialized'
  38.         ];
  39.     }
  40.     public function onIdSerialized(ImportExportIdSerializedEvent $event): void
  41.     {
  42.         try {
  43.             $criteria = new Criteria([$event->getId()]);
  44.             $criteria->addAssociation('categories');
  45.             $criteria->addAssociation('options');
  46.             $criteria->addAssociation('properties');
  47.             $criteria->addAssociation('configuratorSettings');
  48.             $criteria->addAssociation('children');
  49.             $product $this->productRepository->search($criteriaContext::createDefaultContext())->first();
  50.             if (!$product) {
  51.                 return;
  52.             }
  53.             $this->clearSettings($product);
  54.             $this->removeVariants($product);
  55.         } catch (\Throwable $exception) {
  56.             dd($exception);
  57.         }
  58.     }
  59.     private function clearSettings(ProductEntity $product)
  60.     {
  61.         $categoriesDelete = [];
  62.         foreach ($product->getCategories() as $key => $category) {
  63.             $categoriesDelete[] = ['productId' => $product->getId(), 'categoryId' => $key];
  64.         }
  65.         $this->productCategoryRepository->delete(
  66.             $categoriesDelete,
  67.             Context::createDefaultContext()
  68.         );
  69.         $optionsDelete = [];
  70.         foreach ($product->getOptions() as $key => $option) {
  71.             $optionsDelete[] = ['productId' => $product->getId(), 'optionId' => $key];
  72.         }
  73.         $this->productOptionRepository->delete(
  74.             $optionsDelete,
  75.             Context::createDefaultContext()
  76.         );
  77.         $propertyDelete = [];
  78.         foreach ($product->getProperties() as $key => $property) {
  79.             $propertyDelete[] = ['productId' => $product->getId(), 'optionId' => $key];
  80.         }
  81.         $this->productPropertyRepository->delete(
  82.             $propertyDelete,
  83.             Context::createDefaultContext()
  84.         );
  85.         $configuratorDelete = [];
  86.         foreach ($product->getConfiguratorSettings() as $key => $settings) {
  87.             $configuratorDelete[] = ['id' => $key];
  88.         }
  89.         $this->productConfiguratorSettings->delete($configuratorDeleteContext::createDefaultContext());
  90.     }
  91.     private function removeVariants(ProductEntity $productEntity)
  92.     {
  93.         $deleteIds = [];
  94.         $childrenIds $productEntity->getChildren()->getIds();
  95.         foreach ($childrenIds as $id) {
  96.             $deleteIds[] = ['id' => $id];
  97.         }
  98.         $this->productRepository->delete($deleteIdsContext::createDefaultContext());
  99.     }
  100. }