custom/plugins/KlarnaPayment/src/Components/EventListener/FooterBadgeEventListener.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Components\ConfigReader\ConfigReaderInterface;
  5. use KlarnaPayment\Components\Helper\PaymentHelper\PaymentHelperInterface;
  6. use KlarnaPayment\Components\Struct\Configuration;
  7. use KlarnaPayment\Components\Struct\PaymentMethodBadge;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class FooterBadgeEventListener implements EventSubscriberInterface
  12. {
  13.     /** @var ConfigReaderInterface */
  14.     private $configReader;
  15.     /** @var PaymentHelperInterface */
  16.     private $paymentHelper;
  17.     public function __construct(ConfigReaderInterface $configReaderPaymentHelperInterface $paymentHelper)
  18.     {
  19.         $this->configReader  $configReader;
  20.         $this->paymentHelper $paymentHelper;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             FooterPageletLoadedEvent::class => 'addFooterBadge',
  26.         ];
  27.     }
  28.     public function addFooterBadge(FooterPageletLoadedEvent $event): void
  29.     {
  30.         $context $event->getSalesChannelContext();
  31.         $config  $this->configReader->read($context->getSalesChannel()->getId());
  32.         if ($this->displayKlarnaCheckoutBadge($config$context)) {
  33.             $badge = new PaymentMethodBadge();
  34.             $badge->assign([
  35.                 'paymentType' => PaymentMethodBadge::TYPE_CHECKOUT,
  36.                 'countryCode' => $this->getFooterBadgeCountry($context),
  37.                 'style'       => (string) $config->get('kcoFooterBadgeStyle'),
  38.                 'width'       => (int) $config->get('kcoFooterBadgeWidth'),
  39.             ]);
  40.             $event->getPagelet()->addExtension(PaymentMethodBadge::EXTENSION_NAME$badge);
  41.         } elseif ($this->displayKlarnaPaymentsBadge($config$context)) {
  42.             $badge = new PaymentMethodBadge();
  43.             $badge->assign([
  44.                 'paymentType' => PaymentMethodBadge::TYPE_PAYMENTS,
  45.             ]);
  46.             $event->getPagelet()->addExtension(PaymentMethodBadge::EXTENSION_NAME$badge);
  47.         }
  48.     }
  49.     private function displayKlarnaCheckoutBadge(Configuration $configurationSalesChannelContext $context): bool
  50.     {
  51.         if (!$configuration->get('kcoDisplayFooterBadge')) {
  52.             return false;
  53.         }
  54.         if (!$this->paymentHelper->isKlarnaCheckoutEnabled($context)) {
  55.             return false;
  56.         }
  57.         return true;
  58.     }
  59.     private function displayKlarnaPaymentsBadge(Configuration $configurationSalesChannelContext $context): bool
  60.     {
  61.         if (!$configuration->get('kpDisplayFooterBadge')) {
  62.             return false;
  63.         }
  64.         if (!$this->paymentHelper->isKlarnaPaymentsEnabled($context)) {
  65.             return false;
  66.         }
  67.         return true;
  68.     }
  69.     private function getFooterBadgeCountry(SalesChannelContext $context): string
  70.     {
  71.         $validCountries = [
  72.             'de-AT',
  73.             'fr-BE',
  74.             'nl-BE',
  75.             'da-DK',
  76.             'fi-FI',
  77.             'fr-FR',
  78.             'de-DE',
  79.             'it-IT',
  80.             'nl-NL',
  81.             'nb-NO',
  82.             'pl-PL',
  83.             'es-ES',
  84.             'sv-SE',
  85.             'fr-CH',
  86.             'de-CH',
  87.             'it-CH',
  88.             'en-GB',
  89.             'en-US',
  90.         ];
  91.         $countryCode $this->paymentHelper->getSalesChannelLocale($context)->getCode();
  92.         if (in_array($countryCode$validCountriestrue)) {
  93.             return strtolower(str_replace('-''_'$countryCode));
  94.         }
  95.         return 'xx_XX';
  96.     }
  97. }