custom/plugins/SwagPayPal/src/Checkout/SPBCheckout/SPBMarksSubscriber.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Checkout\SPBCheckout;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  10. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  13. use Swag\PayPal\Checkout\ExpressCheckout\SalesChannel\ExpressPrepareCheckoutRoute;
  14. use Swag\PayPal\Checkout\SPBCheckout\Service\SPBMarksDataServiceInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class SPBMarksSubscriber implements EventSubscriberInterface
  17. {
  18.     public const PAYPAL_SMART_PAYMENT_MARKS_DATA_EXTENSION_ID 'payPalSpbMarksData';
  19.     private SPBMarksDataServiceInterface $spbMarksDataService;
  20.     private LoggerInterface $logger;
  21.     public function __construct(
  22.         SPBMarksDataServiceInterface $spbMarksDataService,
  23.         LoggerInterface $logger
  24.     ) {
  25.         $this->logger $logger;
  26.         $this->spbMarksDataService $spbMarksDataService;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             AccountEditOrderPageLoadedEvent::class => 'addMarksExtension',
  32.             AccountPaymentMethodPageLoadedEvent::class => 'addMarksExtension',
  33.             FooterPageletLoadedEvent::class => 'addMarksExtension',
  34.             CheckoutConfirmPageLoadedEvent::class => 'addMarksExtension',
  35.         ];
  36.     }
  37.     /**
  38.      * @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|FooterPageletLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  39.      */
  40.     public function addMarksExtension($event): void
  41.     {
  42.         $spbMarksData $this->spbMarksDataService->getSpbMarksData($event->getSalesChannelContext());
  43.         if ($spbMarksData === null) {
  44.             return;
  45.         }
  46.         $this->logger->debug('Adding SPB marks to {page}', ['page' => \get_class($event)]);
  47.         if ($event instanceof CheckoutConfirmPageLoadedEvent) {
  48.             $confirmPage $event->getPage();
  49.             if ($confirmPage->getCart()->getExtension(ExpressPrepareCheckoutRoute::PAYPAL_EXPRESS_CHECKOUT_CART_EXTENSION_ID) !== null) {
  50.                 return;
  51.             }
  52.             $confirmPage->addExtension(self::PAYPAL_SMART_PAYMENT_MARKS_DATA_EXTENSION_ID$spbMarksData);
  53.             return;
  54.         }
  55.         if ($event instanceof AccountPaymentMethodPageLoadedEvent || $event instanceof AccountEditOrderPageLoadedEvent) {
  56.             $event->getPage()->addExtension(self::PAYPAL_SMART_PAYMENT_MARKS_DATA_EXTENSION_ID$spbMarksData);
  57.             return;
  58.         }
  59.         $event->getPagelet()->addExtension(self::PAYPAL_SMART_PAYMENT_MARKS_DATA_EXTENSION_ID$spbMarksData);
  60.     }
  61. }