custom/plugins/SwagPayPal/src/Installment/Banner/InstallmentBannerSubscriber.php line 135

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