vendor/store.shopware.com/swagpaypal/src/Installment/Banner/InstallmentBannerSubscriber.php line 125

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\Installment\Banner;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPage;
  10. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  12. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPage;
  14. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPage;
  16. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  17. use Shopware\Storefront\Page\PageLoadedEvent;
  18. use Shopware\Storefront\Page\Product\ProductPage;
  19. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  20. use Shopware\Storefront\Pagelet\Footer\FooterPagelet;
  21. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  22. use Shopware\Storefront\Pagelet\PageletLoadedEvent;
  23. use Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPagelet;
  24. use Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPageletLoadedEvent;
  25. use Swag\PayPal\Checkout\Cart\Service\ExcludedProductValidator;
  26. use Swag\PayPal\Installment\Banner\Service\BannerDataServiceInterface;
  27. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  28. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  29. use Swag\PayPal\Util\PaymentMethodUtil;
  30. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  31. /**
  32.  * @internal
  33.  */
  34. class InstallmentBannerSubscriber implements EventSubscriberInterface
  35. {
  36.     public const PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID 'payPalInstallmentBannerData';
  37.     public const PAYPAL_INSTALLMENT_BANNER_DATA_CART_PAGE_EXTENSION_ID 'payPalInstallmentBannerDataCheckoutCart';
  38.     private SettingsValidationServiceInterface $settingsValidationService;
  39.     private PaymentMethodUtil $paymentMethodUtil;
  40.     private BannerDataServiceInterface $bannerDataService;
  41.     private LoggerInterface $logger;
  42.     private ExcludedProductValidator $excludedProductValidator;
  43.     public function __construct(
  44.         SettingsValidationServiceInterface $settingsValidationService,
  45.         PaymentMethodUtil $paymentMethodUtil,
  46.         BannerDataServiceInterface $bannerDataService,
  47.         ExcludedProductValidator $excludedProductValidator,
  48.         LoggerInterface $logger
  49.     ) {
  50.         $this->settingsValidationService $settingsValidationService;
  51.         $this->paymentMethodUtil $paymentMethodUtil;
  52.         $this->bannerDataService $bannerDataService;
  53.         $this->excludedProductValidator $excludedProductValidator;
  54.         $this->logger $logger;
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             CheckoutCartPageLoadedEvent::class => 'addInstallmentBanner',
  60.             CheckoutConfirmPageLoadedEvent::class => 'addInstallmentBanner',
  61.             CheckoutRegisterPageLoadedEvent::class => 'addInstallmentBanner',
  62.             OffcanvasCartPageLoadedEvent::class => 'addInstallmentBanner',
  63.             ProductPageLoadedEvent::class => 'addInstallmentBanner',
  64.             FooterPageletLoadedEvent::class => 'addInstallmentBannerPagelet',
  65.             QuickviewPageletLoadedEvent::class => 'addInstallmentBannerPagelet',
  66.         ];
  67.     }
  68.     public function addInstallmentBanner(PageLoadedEvent $pageLoadedEvent): void
  69.     {
  70.         $salesChannelContext $pageLoadedEvent->getSalesChannelContext();
  71.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext) === false) {
  72.             return;
  73.         }
  74.         try {
  75.             $this->settingsValidationService->validate($salesChannelContext->getSalesChannel()->getId());
  76.         } catch (PayPalSettingsInvalidException $e) {
  77.             return;
  78.         }
  79.         /** @var CheckoutCartPage|CheckoutConfirmPage|CheckoutRegisterPage|OffcanvasCartPage|ProductPage $page */
  80.         $page $pageLoadedEvent->getPage();
  81.         if ($page instanceof ProductPage
  82.             && $this->excludedProductValidator->isProductExcluded($page->getProduct(), $pageLoadedEvent->getSalesChannelContext())) {
  83.             return;
  84.         }
  85.         if (!$page instanceof ProductPage
  86.             && $this->excludedProductValidator->cartContainsExcludedProduct($page->getCart(), $pageLoadedEvent->getSalesChannelContext())) {
  87.             return;
  88.         }
  89.         $bannerData $this->bannerDataService->getInstallmentBannerData($page$salesChannelContext);
  90.         if ($page instanceof CheckoutCartPage) {
  91.             $productTableBannerData = clone $bannerData;
  92.             $productTableBannerData->setLayout('flex');
  93.             $productTableBannerData->setColor('grey');
  94.             $productTableBannerData->setRatio('20x1');
  95.             $page->addExtension(self::PAYPAL_INSTALLMENT_BANNER_DATA_CART_PAGE_EXTENSION_ID$productTableBannerData);
  96.         }
  97.         $page->addExtension(
  98.             self::PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID,
  99.             $bannerData
  100.         );
  101.         $this->logger->debug('Added data to {page}', ['page' => \get_class($pageLoadedEvent)]);
  102.     }
  103.     public function addInstallmentBannerPagelet(PageletLoadedEvent $pageletLoadedEvent): void
  104.     {
  105.         $salesChannelContext $pageletLoadedEvent->getSalesChannelContext();
  106.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext) === false) {
  107.             return;
  108.         }
  109.         try {
  110.             $this->settingsValidationService->validate($salesChannelContext->getSalesChannelId());
  111.         } catch (PayPalSettingsInvalidException $e) {
  112.             return;
  113.         }
  114.         if ($pageletLoadedEvent instanceof QuickviewPageletLoadedEvent
  115.             && $this->excludedProductValidator->isProductExcluded($pageletLoadedEvent->getPagelet()->getProduct(), $pageletLoadedEvent->getSalesChannelContext())) {
  116.             return;
  117.         }
  118.         /** @var FooterPagelet|QuickviewPagelet $pagelet */
  119.         $pagelet $pageletLoadedEvent->getPagelet();
  120.         $bannerData $this->bannerDataService->getInstallmentBannerData($pagelet$salesChannelContext);
  121.         $pagelet->addExtension(
  122.             self::PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID,
  123.             $bannerData
  124.         );
  125.     }
  126. }