custom/plugins/IwvDatevV6/src/Subscriber/ScheduledExportSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Iwv\IwvDatevV6\Subscriber;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  8. use Iwv\IwvDatevV6\Core\Content\IwvDatev\ScheduledExportEvents;
  9. use Iwv\IwvDatevV6\Core\Content\IwvDatev\ScheduledExportDefinition;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use League\Flysystem\FilesystemInterface;
  12. class ScheduledExportSubscriber implements EventSubscriberInterface
  13. {
  14.     /** @var FilesystemInterface $filesystemPrivate */
  15.     private $filesystemPrivate;
  16.     /**
  17.      * @param FilesystemInterface $filesystemPrivate
  18.      */
  19.     public function __construct(FilesystemInterface $filesystemPrivate)
  20.     {
  21.         $this->filesystemPrivate $filesystemPrivate;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             PreWriteValidationEvent::class => 'onPreWriteValidationEvent',
  27.             ScheduledExportEvents::ENTITY_DELETED_EVENT => 'onEntityDeleted',
  28.         ];
  29.     }
  30.     public function onPreWriteValidationEvent(PreWriteValidationEvent $event): void
  31.     {
  32.         foreach ($event->getCommands() as $command) {
  33.             if ($command->getDefinition()->getEntityName() !== ScheduledExportDefinition::ENTITY_NAME) {
  34.                 continue;
  35.             }
  36.             if (!$command instanceof DeleteCommand) {
  37.                 continue;
  38.             }
  39.             $command->requestChangeSet();
  40.         }
  41.     }
  42.     public function onEntityDeleted(EntityDeletedEvent $event): void
  43.     {
  44.         foreach ($event->getIds() as $id) {
  45.             if ($this->filesystemPrivate->has($id)) {
  46.                 $this->filesystemPrivate->delete($id);
  47.             }
  48.         }
  49.     }
  50. }