custom/plugins/PickwareDhl/src/Config/Subscriber/ConfigureShippingMethodInStorefrontSubscriber.php line 50

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\PickwareDhl\Config\Subscriber;
  11. use Pickware\DalBundle\EntityManager;
  12. use Pickware\PickwareDhl\Config\DhlConfig;
  13. use Pickware\ShippingBundle\Config\ConfigException;
  14. use Pickware\ShippingBundle\Config\ConfigService;
  15. use Pickware\ShippingBundle\Config\Model\ShippingMethodConfigDefinition;
  16. use Pickware\ShippingBundle\Config\Model\ShippingMethodConfigEntity;
  17. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  18. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
  19. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  21. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  22. use Shopware\Storefront\Page\PageLoadedEvent;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class ConfigureShippingMethodInStorefrontSubscriber implements EventSubscriberInterface
  25. {
  26.     private EntityManager $entityManager;
  27.     private ConfigService $configService;
  28.     public function __construct(EntityManager $entityManagerConfigService $configService)
  29.     {
  30.         $this->entityManager $entityManager;
  31.         $this->configService $configService;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CheckoutRegisterPageLoadedEvent::class => 'onPageWithShippingMethodConfigDependencyLoaded',
  37.             CheckoutConfirmPageLoadedEvent::class => 'onPageWithShippingMethodConfigDependencyLoaded',
  38.             AddressListingPageLoadedEvent::class => 'onPageWithShippingMethodConfigDependencyLoaded',
  39.             AddressDetailPageLoadedEvent::class => 'onPageWithShippingMethodConfigDependencyLoaded',
  40.             AccountLoginPageLoadedEvent::class => 'onPageWithShippingMethodConfigDependencyLoaded',
  41.         ];
  42.     }
  43.     public function onPageWithShippingMethodConfigDependencyLoaded(PageLoadedEvent $event): void
  44.     {
  45.         // Check that dhl was configured completely before adding any dhl extensions to the storefront
  46.         $dhlConfig = new DhlConfig($this->configService->getConfigForSalesChannel(
  47.             DhlConfig::CONFIG_DOMAIN,
  48.             $event->getSalesChannelContext()->getSalesChannelId(),
  49.         ));
  50.         try {
  51.             $dhlConfig->assertConfigurationIsComplete();
  52.         } catch (ConfigException $e) {
  53.             return;
  54.         }
  55.         /** @var ShippingMethodConfigEntity[] $shippingMethodConfigurations */
  56.         $shippingMethodConfigurations $this->entityManager->findAll(
  57.             ShippingMethodConfigDefinition::class,
  58.             $event->getContext(),
  59.         )->getElements();
  60.         $configurationsByShippingMethodId = [];
  61.         foreach ($shippingMethodConfigurations as $configuration) {
  62.             $configurationsByShippingMethodId[$configuration->getShippingMethodId()] = $configuration;
  63.         }
  64.         $dhlConfigurationPageExtension = new DhlConfigurationPageExtension();
  65.         $dhlConfigurationPageExtension->assign([
  66.             'shippingMethodConfigurations' => $configurationsByShippingMethodId,
  67.             'dhlConfig' => $dhlConfig,
  68.         ]);
  69.         $event->getPage()->addExtension(
  70.             DhlConfigurationPageExtension::PAGE_EXTENSION_NAME,
  71.             $dhlConfigurationPageExtension,
  72.         );
  73.     }
  74. }