custom/plugins/MolliePayments/src/Subscriber/WebhookTimezoneSubscriber.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Kiener\MolliePayments\Service\Router\RoutingDetector;
  4. use Kiener\MolliePayments\Service\TransactionService;
  5. use Kiener\MolliePayments\Struct\Order\OrderAttributes;
  6. use Psr\Log\LoggerInterface;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  8. use Shopware\Core\Checkout\Order\OrderEntity;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Storefront\Framework\Twig\TwigDateRequestListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class WebhookTimezoneSubscriber implements EventSubscriberInterface
  15. {
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             # Route gets matched in a subscriber with priority 32, so we need to have a lower priority than that.
  20.             # But priority needs to be higher than 0 as Shopware's timezone listener will run at that priority.
  21.             KernelEvents::REQUEST => ['fixWebhookTimezone'31],
  22.         ];
  23.     }
  24.     /**
  25.      * @var TransactionService
  26.      */
  27.     private $transactionService;
  28.     /**
  29.      * @var RoutingDetector
  30.      */
  31.     private $routeDetector;
  32.     /**
  33.      * @var LoggerInterface
  34.      */
  35.     private $logger;
  36.     /**
  37.      * @param TransactionService $transactionService
  38.      * @param RoutingDetector $routeDetector
  39.      * @param LoggerInterface $logger
  40.      */
  41.     public function __construct(TransactionService $transactionServiceRoutingDetector $routeDetectorLoggerInterface $logger)
  42.     {
  43.         $this->transactionService $transactionService;
  44.         $this->routeDetector $routeDetector;
  45.         $this->logger $logger;
  46.     }
  47.     /**
  48.      * @param RequestEvent $event
  49.      * @return void
  50.      */
  51.     public function fixWebhookTimezone(RequestEvent $event): void
  52.     {
  53.         # we only fix the timezone when being called from the
  54.         # Storefront Webhook Route or API Webhook Route (headless).
  55.         if (!$this->routeDetector->isStorefrontWebhookRoute() && !$this->routeDetector->isApiWebhookRoute()) {
  56.             return;
  57.         }
  58.         $request $event->getRequest();
  59.         $routeParams $request->get('_route_params');
  60.         $transactionId $routeParams['swTransactionId'] ?? '';
  61.         if (!Uuid::isValid($transactionId)) {
  62.             $this->logger->warning(sprintf('Webhook Timezone Fixer: TransactionId %s is not valid'$transactionId), [
  63.                 'transactionId' => $transactionId,
  64.             ]);
  65.             return;
  66.         }
  67.         $transaction $this->transactionService->getTransactionById($transactionId);
  68.         if (!$transaction instanceof OrderTransactionEntity) {
  69.             $this->logger->error(sprintf('Transaction for id %s does not exist'$transactionId));
  70.             return;
  71.         }
  72.         $order $transaction->getOrder();
  73.         if (!$order instanceof OrderEntity) {
  74.             $this->logger->error(sprintf('Could not get order from transaction %s'$transactionId));
  75.             return;
  76.         }
  77.         $orderAttributes = new OrderAttributes($order);
  78.         if (empty($orderAttributes->getTimezone())) {
  79.             return;
  80.         }
  81.         // Set the timezone cookie on the request and let Shopware handle the rest.
  82.         $request->cookies->set(TwigDateRequestListener::TIMEZONE_COOKIE$orderAttributes->getTimezone());
  83.     }
  84. }