<?php declare(strict_types=1);
namespace Crehler\MojeBambinoEngine\Subscriber;
use Crehler\MojeBambinoEngine\Event\ImportExportIdSerializedEvent;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ImportExportEventSubscriber implements EventSubscriberInterface
{
private EntityRepository $productRepository;
private EntityRepository $productCategoryRepository;
private EntityRepository $productPropertyRepository;
private EntityRepository $productOptionRepository;
private EntityRepository $productConfiguratorSettings;
public function __construct(
EntityRepository $productRepository,
EntityRepository $productPropertyRepository,
EntityRepository $productCategoryRepository,
EntityRepository $productOptionRepository,
EntityRepository $productConfiguratorSettings
)
{
$this->productRepository = $productRepository;
$this->productPropertyRepository = $productPropertyRepository;
$this->productCategoryRepository = $productCategoryRepository;
$this->productOptionRepository = $productOptionRepository;
$this->productConfiguratorSettings = $productConfiguratorSettings;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ImportExportIdSerializedEvent::class => 'onIdSerialized'
];
}
public function onIdSerialized(ImportExportIdSerializedEvent $event): void
{
try {
$criteria = new Criteria([$event->getId()]);
$criteria->addAssociation('categories');
$criteria->addAssociation('options');
$criteria->addAssociation('properties');
$criteria->addAssociation('configuratorSettings');
$criteria->addAssociation('children');
$product = $this->productRepository->search($criteria, Context::createDefaultContext())->first();
if (!$product) {
return;
}
$this->clearSettings($product);
$this->removeVariants($product);
} catch (\Throwable $exception) {
dd($exception);
}
}
private function clearSettings(ProductEntity $product)
{
$categoriesDelete = [];
foreach ($product->getCategories() as $key => $category) {
$categoriesDelete[] = ['productId' => $product->getId(), 'categoryId' => $key];
}
$this->productCategoryRepository->delete(
$categoriesDelete,
Context::createDefaultContext()
);
$optionsDelete = [];
foreach ($product->getOptions() as $key => $option) {
$optionsDelete[] = ['productId' => $product->getId(), 'optionId' => $key];
}
$this->productOptionRepository->delete(
$optionsDelete,
Context::createDefaultContext()
);
$propertyDelete = [];
foreach ($product->getProperties() as $key => $property) {
$propertyDelete[] = ['productId' => $product->getId(), 'optionId' => $key];
}
$this->productPropertyRepository->delete(
$propertyDelete,
Context::createDefaultContext()
);
$configuratorDelete = [];
foreach ($product->getConfiguratorSettings() as $key => $settings) {
$configuratorDelete[] = ['id' => $key];
}
$this->productConfiguratorSettings->delete($configuratorDelete, Context::createDefaultContext());
}
private function removeVariants(ProductEntity $productEntity)
{
$deleteIds = [];
$childrenIds = $productEntity->getChildren()->getIds();
foreach ($childrenIds as $id) {
$deleteIds[] = ['id' => $id];
}
$this->productRepository->delete($deleteIds, Context::createDefaultContext());
}
}