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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Iwv\IwvDatevV6\Subscriber;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  11. use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskDefinition;
  12. use Iwv\IwvDatevV6\Service\ScheduledTask\ScheduledExportTask;
  13. use Iwv\IwvDatevV6\Core\Content\IwvDatev\ScheduledExportEvents;
  14. use Iwv\IwvDatevV6\Core\Content\IwvDatev\ScheduledExportDefinition;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use League\Flysystem\FilesystemInterface;
  17. class ScheduledExportSubscriber implements EventSubscriberInterface
  18. {
  19.     /** @var EntityRepositoryInterface */
  20.     protected $scheduledTaskRepository;
  21.     /** @var FilesystemInterface $filesystemPrivate */
  22.     private $filesystemPrivate;
  23.     /**
  24.      * @param EntityRepositoryInterface $scheduledTaskRepository
  25.      * @param FilesystemInterface $filesystemPrivate
  26.      */
  27.     public function __construct(
  28.         EntityRepositoryInterface $scheduledTaskRepository,
  29.         FilesystemInterface $filesystemPrivate
  30.     ) {
  31.         $this->scheduledTaskRepository $scheduledTaskRepository;
  32.         $this->filesystemPrivate $filesystemPrivate;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             PreWriteValidationEvent::class => 'onPreWriteValidationEvent',
  38.             ScheduledExportEvents::ENTITY_DELETED_EVENT => 'onEntityDeleted',
  39.             ScheduledExportEvents::ENTITY_WRITTEN_EVENT => 'onEntityWritten',
  40.         ];
  41.     }
  42.     public function onPreWriteValidationEvent(PreWriteValidationEvent $event): void
  43.     {
  44.         foreach ($event->getCommands() as $command) {
  45.             if ($command->getDefinition()->getEntityName() !== ScheduledExportDefinition::ENTITY_NAME) {
  46.                 continue;
  47.             }
  48.             if (!$command instanceof DeleteCommand) {
  49.                 continue;
  50.             }
  51.             $command->requestChangeSet();
  52.         }
  53.     }
  54.     public function onEntityDeleted(EntityDeletedEvent $event): void
  55.     {
  56.         foreach ($event->getIds() as $id) {
  57.             if ($this->filesystemPrivate->has($id)) {
  58.                 $this->filesystemPrivate->delete($id);
  59.             }
  60.         }
  61.     }
  62.     public function onEntityWritten(EntityWrittenEvent $event): void
  63.     {
  64.         $context $event->getContext();
  65.         /** @var \Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskEntity $taskDB */
  66.         if ($taskDB $this->scheduledTaskRepository
  67.             ->search((new Criteria)
  68.                 ->addFilter(new EqualsFilter('name'ScheduledExportTask::getTaskName()))
  69.                 ->addFilter(new EqualsFilter('status'ScheduledTaskDefinition::STATUS_QUEUED))
  70.                 , $context)
  71.             ->first()) {
  72.             $this->scheduledTaskRepository->update([
  73.                 [
  74.                     'id' => $taskDB->getId(),
  75.                     'status' => ScheduledTaskDefinition::STATUS_SCHEDULED,
  76.                 ]
  77.                 ]
  78.                 , $context);
  79.         }
  80.     }
  81. }