custom/plugins/PickwareErpStarter/src/Reorder/Subscriber/SystemConfigSubscriber.php line 42

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\PickwareErpStarter\Reorder\Subscriber;
  11. use DateInterval;
  12. use DateTime;
  13. use DateTimeZone;
  14. use Pickware\DalBundle\EntityManager;
  15. use Pickware\PickwareErpStarter\Reorder\ScheduledTask\ReorderNotificationTask;
  16. use Shopware\Core\Framework\Context;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  18. use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskDefinition;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class SystemConfigSubscriber implements EventSubscriberInterface
  21. {
  22.     public const REORDER_NOTIFICATION_TIME_CONFIGURATION_KEY 'PickwareErpStarter.global-plugin-config.reorderNotificationTime';
  23.     private EntityManager $entityManager;
  24.     public function __construct(EntityManager $entityManager)
  25.     {
  26.         $this->entityManager $entityManager;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             'system_config.written' => 'afterSystemConfigWritten',
  32.         ];
  33.     }
  34.     public function afterSystemConfigWritten(EntityWrittenEvent $event): void
  35.     {
  36.         $writeResults $event->getWriteResults();
  37.         foreach ($writeResults as $writeResult) {
  38.             $payload $writeResult->getPayload();
  39.             // Since we only support a global configuration (i.e. not for a specific sales channel), ignore
  40.             // configurations that have a sales channel id.
  41.             $isSalesChannelConfiguration $payload['salesChannelId'] !== null;
  42.             $isReorderNotificationTimeConfiguration $payload['configurationKey'] === self::REORDER_NOTIFICATION_TIME_CONFIGURATION_KEY;
  43.             if ($isSalesChannelConfiguration || !$isReorderNotificationTimeConfiguration) {
  44.                 continue;
  45.             }
  46.             $timeNowInUTC = new DateTime('now', new DateTimeZone('UTC'));
  47.             $nextExecutionTimeInUTC DateTime::createFromFormat('H:i:s'$payload['configurationValue'], new DateTimeZone('UTC'));
  48.             if ($timeNowInUTC $nextExecutionTimeInUTC) {
  49.                 // New reorder notification time is older than now. Set next execution to tomorrow on that time.
  50.                 $nextExecutionTimeInUTC->add(new DateInterval('P1D'));
  51.             }
  52.             $this->updateNextExecutionTimeOfScheduledTask(
  53.                 ReorderNotificationTask::class,
  54.                 $nextExecutionTimeInUTC,
  55.                 $event->getContext(),
  56.             );
  57.         }
  58.     }
  59.     private function updateNextExecutionTimeOfScheduledTask(
  60.         string $taskClass,
  61.         DateTime $nextExecutionTimeInUTC,
  62.         Context $context
  63.     ): void {
  64.         $scheduledTask $this->entityManager->findOneBy(ScheduledTaskDefinition::class, [
  65.             'scheduledTaskClass' => $taskClass,
  66.         ], $context);
  67.         if (!$scheduledTask) {
  68.             return;
  69.         }
  70.         // Shopware expects the "nextExecutionTime" of a scheduled task to be in UTC
  71.         $this->entityManager->update(
  72.             ScheduledTaskDefinition::class,
  73.             [
  74.                 [
  75.                     'id' => $scheduledTask->getId(),
  76.                     'nextExecutionTime' => $nextExecutionTimeInUTC,
  77.                 ],
  78.             ],
  79.             $context,
  80.         );
  81.     }
  82. }