custom/plugins/PickwareErpStarter/src/ImportExport/ModelSubscriber/DeleteDocumentSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\PickwareErpStarter\ImportExport\ModelSubscriber;
  11. use Pickware\DalBundle\EntityManager;
  12. use Pickware\DalBundle\EntityPreWriteValidationEvent;
  13. use Pickware\DalBundle\EntityPreWriteValidationEventDispatcher;
  14. use Pickware\DocumentBundle\Model\DocumentDefinition;
  15. use Pickware\PickwareErpStarter\ImportExport\Model\ImportExportDefinition;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class DeleteDocumentSubscriber implements EventSubscriberInterface
  20. {
  21.     private EntityManager $entityManager;
  22.     public function __construct(EntityManager $entityManager)
  23.     {
  24.         $this->entityManager $entityManager;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             ImportExportDefinition::EVENT_DELETED => 'onImportExportDeleted',
  30.             EntityPreWriteValidationEventDispatcher::getEventName(ImportExportDefinition::ENTITY_NAME) => 'onPreWriteValidationEvent',
  31.         ];
  32.     }
  33.     public function onPreWriteValidationEvent($event): void
  34.     {
  35.         if (!($event instanceof EntityPreWriteValidationEvent)) {
  36.             // The subscriber is probably instantiated in its old version (with the Shopware PreWriteValidationEvent) in
  37.             // the container and will be updated on the next container rebuild (next request). Early return.
  38.             return;
  39.         }
  40.         foreach ($event->getCommands() as $command) {
  41.             if ($command instanceof DeleteCommand) {
  42.                 $command->requestChangeSet();
  43.             }
  44.         }
  45.     }
  46.     public function onImportExportDeleted(EntityDeletedEvent $event): void
  47.     {
  48.         $documentIdsToRemove = [];
  49.         foreach ($event->getWriteResults() as $writeResult) {
  50.             $changeSet $writeResult->getChangeSet();
  51.             if ($changeSet->getBefore('document_id') !== null) {
  52.                 $documentIdsToRemove[] = bin2hex($changeSet->getBefore('document_id'));
  53.             }
  54.         }
  55.         $this->entityManager->delete(DocumentDefinition::class, $documentIdsToRemove$event->getContext());
  56.     }
  57. }