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