custom/plugins/PickwareErpStarter/vendor/pickware/document-bundle/src/Model/Subscriber/DeleteFileSubscriber.php line 54

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\DocumentBundle\Model\Subscriber;
  11. use League\Flysystem\FilesystemInterface;
  12. use Pickware\DalBundle\EntityPreWriteValidationEvent;
  13. use Pickware\DalBundle\EntityPreWriteValidationEventDispatcher;
  14. use Pickware\DocumentBundle\Model\DocumentDefinition;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class DeleteFileSubscriber implements EventSubscriberInterface
  19. {
  20.     private FilesystemInterface $privateFileSystem;
  21.     public function __construct(FilesystemInterface $privateFileSystem)
  22.     {
  23.         $this->privateFileSystem $privateFileSystem;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             DocumentDefinition::ENTITY_DELETED_EVENT => 'onDocumentEntityDeleted',
  29.             EntityPreWriteValidationEventDispatcher::getEventName(DocumentDefinition::ENTITY_NAME) => 'onPreWriteValidationEvent',
  30.         ];
  31.     }
  32.     public function onPreWriteValidationEvent($event): void
  33.     {
  34.         if (!($event instanceof EntityPreWriteValidationEvent)) {
  35.             // The subscriber is probably instantiated in its old version (with the Shopware PreWriteValidationEvent) in
  36.             // the container and will be updated on the next container rebuild (next request). Early return.
  37.             return;
  38.         }
  39.         foreach ($event->getCommands() as $command) {
  40.             if ($command instanceof DeleteCommand) {
  41.                 $command->requestChangeSet();
  42.             }
  43.         }
  44.     }
  45.     public function onDocumentEntityDeleted(EntityDeletedEvent $event): void
  46.     {
  47.         foreach ($event->getWriteResults() as $writeResult) {
  48.             $changeSet $writeResult->getChangeSet();
  49.             $filePath $changeSet->getBefore('path_in_private_file_system');
  50.             if ($filePath === null) {
  51.                 $filePath 'documents/' bin2hex($changeSet->getBefore('id'));
  52.             }
  53.             if ($this->privateFileSystem->has($filePath)) {
  54.                 $this->privateFileSystem->delete($filePath);
  55.             }
  56.         }
  57.     }
  58. }