custom/plugins/NewsletterSendinblue/src/Subscriber/PreWriteValidationSubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NewsletterSendinblue\Subscriber;
  3. use NewsletterSendinblue\NewsletterSendinblue;
  4. use NewsletterSendinblue\Service\BaseSyncService;
  5. use Shopware\Core\Content\Category\Aggregate\CategoryTranslation\CategoryTranslationDefinition;
  6. use Shopware\Core\Content\Category\CategoryDefinition;
  7. use Shopware\Core\Content\Product\Aggregate\ProductTranslation\ProductTranslationDefinition;
  8. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  9. use Shopware\Core\Content\Product\ProductDefinition;
  10. use Shopware\Core\Defaults;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\ChangeSetAware;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  15. use Shopware\Core\Framework\Struct\ArrayStruct;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class PreWriteValidationSubscriber implements EventSubscriberInterface
  19. {
  20.     /** @var BaseSyncService */
  21.     private $productSyncService;
  22.     /** @var BaseSyncService */
  23.     private $categorySyncService;
  24.     private $deleteProducts = [];
  25.     private $deleteCategories = [];
  26.     private $visibilityAlsoRemoved false;
  27.     private $onlyVisibilityRemoved false;
  28.     public function __construct(
  29.         BaseSyncService $productSyncService,
  30.         BaseSyncService $categorySyncService
  31.     ) {
  32.         $this->productSyncService $productSyncService;
  33.         $this->categorySyncService $categorySyncService;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             PreWriteValidationEvent::class => 'onPreWriteValidationEvent'
  39.         ];
  40.     }
  41.     public function onPreWriteValidationEvent(PreWriteValidationEvent $event): void
  42.     {
  43.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  44.             return;
  45.         }
  46.         if ($event->getContext()->hasExtension(NewsletterSendinblue::IGNORE_EVENT)) {
  47.             return;
  48.         }
  49.         $isProductFieldUpdated false;
  50.         $productDeletableIds = [];
  51.         $categoryDeletableIds = [];
  52.         foreach ($event->getCommands() as $command) {
  53.             $entityName $command->getDefinition()->getEntityName();
  54.             // Need to check if there are updating fields concerning to productEntity (e.g., stock, price, etc.).
  55.             // For example, there can be only product sales channel (visibility) change etc...
  56.             if (!$isProductFieldUpdated && ($entityName === ProductDefinition::ENTITY_NAME
  57.                     || $entityName === ProductTranslationDefinition::ENTITY_NAME)
  58.             ) {
  59.                 $isProductFieldUpdated true;
  60.             }
  61.             if (!$command instanceof ChangeSetAware) {
  62.                 continue;
  63.             }
  64.             // Handle change set requests
  65.             if ($this->shouldRequestChangeSet($entityName) && $command instanceof UpdateCommand) {
  66.                 $command->requestChangeSet();
  67.             }
  68.             // Handle product visibility
  69.             // if one of the sales channels is removed from the product
  70.             if ($entityName === ProductVisibilityDefinition::ENTITY_NAME
  71.                 && $command instanceof DeleteCommand
  72.             ) {
  73.                 if ($isProductFieldUpdated) {
  74.                     $this->visibilityAlsoRemoved true;
  75.                 } else {
  76.                     $this->onlyVisibilityRemoved true;
  77.                 }
  78.             }
  79.             // Collect product deletions
  80.             if ($entityName === ProductDefinition::ENTITY_NAME
  81.                 && $command instanceof DeleteCommand
  82.                 && !empty($command->getPrimaryKey()['id'])
  83.             ) {
  84.                 $productId Uuid::fromBytesToHex($command->getPrimaryKey()['id']);
  85.                 $productDeletableIds[] = $productId;
  86.                 $childrenIds $this->productSyncService->getChildrenIds($productId$event->getContext());
  87.                 $productDeletableIds array_merge($productDeletableIds$childrenIds);
  88.             }
  89.             // Collect category deletion
  90.             if ($entityName === CategoryDefinition::ENTITY_NAME
  91.                 && $command instanceof DeleteCommand
  92.                 && !empty($command->getPrimaryKey()['id'])
  93.             ) {
  94.                 $catId Uuid::fromBytesToHex($command->getPrimaryKey()['id']);
  95.                 $categoryDeletableIds[] = $catId;
  96.                 $childrenIds $this->categorySyncService->getChildrenIds($catId$event->getContext());
  97.                 $categoryDeletableIds array_merge($categoryDeletableIds$childrenIds);
  98.             }
  99.         }
  100.         foreach ($productDeletableIds as $id) {
  101.             $this->deleteProducts[$id] = $this->productSyncService->getEntity($id$event->getContext());
  102.         }
  103.         if (!empty($this->deleteProducts)) {
  104.             $this->productSyncService->setDeleteEntities($this->deleteProducts);
  105.         }
  106.         foreach ($categoryDeletableIds as $id) {
  107.             $this->deleteCategories[$id] = $this->categorySyncService->getEntity($id$event->getContext());
  108.         }
  109.         if (!empty($this->deleteCategories)) {
  110.             $this->categorySyncService->setDeleteEntities($this->deleteCategories);
  111.         }
  112.         $event->getContext()->addExtension('brevoPreWriteEvent', new ArrayStruct([
  113.             'visibilityAlsoRemoved' => $this->visibilityAlsoRemoved,
  114.             'onlyVisibilityRemoved' => $this->onlyVisibilityRemoved,
  115.             'deleteProducts' => $this->deleteProducts,
  116.             'deleteCategories' => $this->deleteCategories
  117.         ]));
  118.     }
  119.     private function shouldRequestChangeSet(string $entityName): bool
  120.     {
  121.         return in_array($entityName, [
  122.             ProductDefinition::ENTITY_NAME,
  123.             ProductTranslationDefinition::ENTITY_NAME,
  124.             CategoryDefinition::ENTITY_NAME,
  125.             CategoryTranslationDefinition::ENTITY_NAME
  126.         ], true);
  127.     }
  128. }