custom/plugins/PickwareErpStarter/vendor/pickware/config-bundle/src/AbstractScheduledTaskExecutionTimeConfigSubscriber.php line 49

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\ConfigBundle;
  11. use DateTime;
  12. use DateTimeZone;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16.  * Use this subscriber to allow setting the execution time (not datetime!) of a scheduled task via plugin configuration.
  17.  */
  18. abstract class AbstractScheduledTaskExecutionTimeConfigSubscriber implements EventSubscriberInterface
  19. {
  20.     private ScheduledTaskExecutionTimeUpdater $scheduledTaskExecutionTimeUpdater;
  21.     private string $configurationKey;
  22.     private string $scheduledTaskClassName;
  23.     public function __construct(
  24.         ScheduledTaskExecutionTimeUpdater $scheduledTaskExecutionTimeUpdater,
  25.         string $configurationKey,
  26.         string $scheduledTaskClassName
  27.     ) {
  28.         $this->scheduledTaskExecutionTimeUpdater $scheduledTaskExecutionTimeUpdater;
  29.         $this->configurationKey $configurationKey;
  30.         $this->scheduledTaskClassName $scheduledTaskClassName;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             'system_config.written' => 'afterSystemConfigWritten',
  36.         ];
  37.     }
  38.     /**
  39.      * Listens to configuration changes and uses the time (not datetime!) input of a config field to update the next
  40.      * execution time of the scheduled task.
  41.      */
  42.     public function afterSystemConfigWritten(EntityWrittenEvent $event): void
  43.     {
  44.         $writeResults $event->getWriteResults();
  45.         foreach ($writeResults as $writeResult) {
  46.             $payload $writeResult->getPayload();
  47.             // Since scheduled tasks are unique, we only support a single global configuration (i.e. not for a specific
  48.             // sales channel) for scheduled task configuration. Ignore configurations that have a sales channel id.
  49.             $isSalesChannelConfiguration $payload['salesChannelId'] !== null;
  50.             if ($isSalesChannelConfiguration || $payload['configurationKey'] !== $this->configurationKey) {
  51.                 continue;
  52.             }
  53.             $nextExecutionTimeInUTC DateTime::createFromFormat('H:i:s'$payload['configurationValue'], new DateTimeZone('UTC'));
  54.             $this->scheduledTaskExecutionTimeUpdater->updateExecutionTimeOfScheduledTask(
  55.                 $this->scheduledTaskClassName,
  56.                 $nextExecutionTimeInUTC,
  57.                 $event->getContext(),
  58.             );
  59.         }
  60.     }
  61. }