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

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\Installment\Banner\Service\BannerDataServiceInterface;
  27. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  28. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  29. use Swag\PayPal\Setting\Settings;
  30. use Swag\PayPal\Util\PaymentMethodUtil;
  31. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  32. class InstallmentBannerSubscriber implements EventSubscriberInterface
  33. {
  34.     public const PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID 'payPalInstallmentBannerData';
  35.     public const PAYPAL_INSTALLMENT_BANNER_DATA_CART_PAGE_EXTENSION_ID 'payPalInstallmentBannerDataCheckoutCart';
  36.     private SettingsValidationServiceInterface $settingsValidationService;
  37.     private SystemConfigService $systemConfigService;
  38.     private PaymentMethodUtil $paymentMethodUtil;
  39.     private BannerDataServiceInterface $bannerDataService;
  40.     private LoggerInterface $logger;
  41.     public function __construct(
  42.         SettingsValidationServiceInterface $settingsValidationService,
  43.         SystemConfigService $systemConfigService,
  44.         PaymentMethodUtil $paymentMethodUtil,
  45.         BannerDataServiceInterface $bannerDataService,
  46.         LoggerInterface $logger
  47.     ) {
  48.         $this->settingsValidationService $settingsValidationService;
  49.         $this->systemConfigService $systemConfigService;
  50.         $this->paymentMethodUtil $paymentMethodUtil;
  51.         $this->bannerDataService $bannerDataService;
  52.         $this->logger $logger;
  53.     }
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             CheckoutCartPageLoadedEvent::class => 'addInstallmentBanner',
  58.             CheckoutConfirmPageLoadedEvent::class => 'addInstallmentBanner',
  59.             CheckoutRegisterPageLoadedEvent::class => 'addInstallmentBanner',
  60.             OffcanvasCartPageLoadedEvent::class => 'addInstallmentBanner',
  61.             ProductPageLoadedEvent::class => 'addInstallmentBanner',
  62.             FooterPageletLoadedEvent::class => 'addInstallmentBannerPagelet',
  63.             QuickviewPageletLoadedEvent::class => 'addInstallmentBannerPagelet',
  64.         ];
  65.     }
  66.     public function addInstallmentBanner(PageLoadedEvent $pageLoadedEvent): void
  67.     {
  68.         $salesChannelContext $pageLoadedEvent->getSalesChannelContext();
  69.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext) === false) {
  70.             return;
  71.         }
  72.         try {
  73.             $this->settingsValidationService->validate($salesChannelContext->getSalesChannel()->getId());
  74.         } catch (PayPalSettingsInvalidException $e) {
  75.             return;
  76.         }
  77.         if (!$this->systemConfigService->getBool(Settings::INSTALLMENT_BANNER_ENABLED)) {
  78.             return;
  79.         }
  80.         /** @var CheckoutCartPage|CheckoutConfirmPage|CheckoutRegisterPage|OffcanvasCartPage|ProductPage $page */
  81.         $page $pageLoadedEvent->getPage();
  82.         $bannerData $this->bannerDataService->getInstallmentBannerData($page$salesChannelContext);
  83.         if ($page instanceof CheckoutCartPage) {
  84.             $productTableBannerData = new BannerData(
  85.                 $bannerData->getPaymentMethodId(),
  86.                 $bannerData->getClientId(),
  87.                 $bannerData->getAmount(),
  88.                 $bannerData->getCurrency(),
  89.                 'flex',
  90.                 'grey',
  91.                 '20x1'
  92.             );
  93.             $page->addExtension(self::PAYPAL_INSTALLMENT_BANNER_DATA_CART_PAGE_EXTENSION_ID$productTableBannerData);
  94.         }
  95.         $page->addExtension(
  96.             self::PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID,
  97.             $bannerData
  98.         );
  99.         $this->logger->debug('Added data to {page}', ['page' => \get_class($pageLoadedEvent)]);
  100.     }
  101.     public function addInstallmentBannerPagelet(PageletLoadedEvent $pageletLoadedEvent): void
  102.     {
  103.         $salesChannelContext $pageletLoadedEvent->getSalesChannelContext();
  104.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext) === false) {
  105.             return;
  106.         }
  107.         try {
  108.             $this->settingsValidationService->validate($salesChannelContext->getSalesChannelId());
  109.         } catch (PayPalSettingsInvalidException $e) {
  110.             return;
  111.         }
  112.         if (!$this->systemConfigService->getBool(Settings::INSTALLMENT_BANNER_ENABLED$salesChannelContext->getSalesChannelId())) {
  113.             return;
  114.         }
  115.         /** @var FooterPagelet|QuickviewPagelet $pagelet */
  116.         $pagelet $pageletLoadedEvent->getPagelet();
  117.         $bannerData $this->bannerDataService->getInstallmentBannerData($pagelet$salesChannelContext);
  118.         $pagelet->addExtension(
  119.             self::PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID,
  120.             $bannerData
  121.         );
  122.     }
  123. }