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\DocumentBundle\Model\DocumentDefinition;
  13. use Pickware\PickwareErpStarter\ImportExport\Model\ImportExportDefinition;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class DeleteDocumentSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var EntityManager
  22.      */
  23.     private $entityManager;
  24.     public function __construct(EntityManager $entityManager)
  25.     {
  26.         $this->entityManager $entityManager;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ImportExportDefinition::EVENT_DELETED => 'onImportExportDeleted',
  32.             PreWriteValidationEvent::class => 'onPreWriteValidationEvent',
  33.         ];
  34.     }
  35.     public function onPreWriteValidationEvent(PreWriteValidationEvent $event): void
  36.     {
  37.         foreach ($event->getCommands() as $command) {
  38.             if ($command->getDefinition()->getEntityName() !== ImportExportDefinition::ENTITY_NAME) {
  39.                 continue;
  40.             }
  41.             if (!$command instanceof DeleteCommand) {
  42.                 continue;
  43.             }
  44.             $command->requestChangeSet();
  45.         }
  46.     }
  47.     public function onImportExportDeleted(EntityDeletedEvent $event): void
  48.     {
  49.         $documentIdsToRemove = [];
  50.         foreach ($event->getWriteResults() as $writeResult) {
  51.             $changeSet $writeResult->getChangeSet();
  52.             if ($changeSet->getBefore('document_id') !== null) {
  53.                 $documentIdsToRemove[] = bin2hex($changeSet->getBefore('document_id'));
  54.             }
  55.         }
  56.         $this->entityManager->delete(DocumentDefinition::class, $documentIdsToRemove$event->getContext());
  57.     }
  58. }