custom/plugins/MolliePayments/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php line 54

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Compatibility\Storefront\Route\PaymentMethodRoute\Cache;
  3. use Kiener\MolliePayments\Service\Cart\Voucher\VoucherCartCollector;
  4. use Kiener\MolliePayments\Service\SettingsService;
  5. use Kiener\MolliePayments\Struct\LineItem\LineItemAttributes;
  6. use Shopware\Core\Checkout\Cart\Cart;
  7. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  8. use Shopware\Core\Checkout\Payment\Event\PaymentMethodRouteCacheKeyEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CachedPaymentMethodRoute64 implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var CartService
  14.      */
  15.     private $cartService;
  16.     /**
  17.      * @var SettingsService
  18.      */
  19.     private $pluginSettings;
  20.     /**
  21.      * @param SettingsService $pluginSettings
  22.      * @param CartService $cartService
  23.      */
  24.     public function __construct(SettingsService $pluginSettingsCartService $cartService)
  25.     {
  26.         $this->pluginSettings $pluginSettings;
  27.         $this->cartService $cartService;
  28.     }
  29.     /**
  30.      * @return string[]
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             PaymentMethodRouteCacheKeyEvent::class => 'onGenerateCacheKey',
  36.         ];
  37.     }
  38.     /**
  39.      * This function will make sure that we have a working cache key for all dynamic payment method
  40.      * situations that could occur.
  41.      * So we need to determine if we have a voucher product in it, or not...otherwise the dynamic display
  42.      * of these payment methods (in other route handlers) would not work.
  43.      * @param PaymentMethodRouteCacheKeyEvent $event
  44.      */
  45.     public function onGenerateCacheKey(PaymentMethodRouteCacheKeyEvent $event): void
  46.     {
  47.         $cart $this->cartService->getCart($event->getContext()->getToken(), $event->getContext());
  48.         $parts $event->getParts();
  49.         $parts $this->addVoucherKey($cart$parts);
  50.         $parts $this->addMollieLimitsKey($parts);
  51.         $parts $this->addSubscriptionKey($cart$parts);
  52.         $event->setParts($parts);
  53.     }
  54.     /**
  55.      * @param Cart $cart
  56.      * @param array<mixed> $parts
  57.      *
  58.      * @return array<mixed>
  59.      */
  60.     private function addVoucherKey(Cart $cart, array $parts): array
  61.     {
  62.         $voucherPermitted = (bool)$cart->getData()->get(VoucherCartCollector::VOUCHER_PERMITTED);
  63.         if ($voucherPermitted) {
  64.             $parts[] = 'with-voucher';
  65.         } else {
  66.             $parts[] = 'without-voucher';
  67.         }
  68.         return $parts;
  69.     }
  70.     /**
  71.      * @param array<mixed> $parts
  72.      * @return array<mixed>
  73.      */
  74.     private function addMollieLimitsKey(array $parts): array
  75.     {
  76.         $settings $this->pluginSettings->getSettings();
  77.         if ($settings->getUseMolliePaymentMethodLimits()) {
  78.             $parts[] = 'with-limits';
  79.         } else {
  80.             $parts[] = 'without-limits';
  81.         }
  82.         return $parts;
  83.     }
  84.     /**
  85.      * @param Cart $cart
  86.      * @param array<mixed> $parts
  87.      *
  88.      * @return array<mixed>
  89.      */
  90.     private function addSubscriptionKey(Cart $cart, array $parts): array
  91.     {
  92.         $hasSubscriptionItems $this->isSubscriptionCart($cart);
  93.         if ($hasSubscriptionItems) {
  94.             $parts[] = 'with-subscription';
  95.         } else {
  96.             $parts[] = 'without-subscription';
  97.         }
  98.         return $parts;
  99.     }
  100.     /**
  101.      * @param Cart $cart
  102.      * @return bool
  103.      */
  104.     private function isSubscriptionCart(Cart $cart): bool
  105.     {
  106.         foreach ($cart->getLineItems() as $lineItem) {
  107.             $attribute = new LineItemAttributes($lineItem);
  108.             if ($attribute->isSubscriptionProduct()) {
  109.                 return true;
  110.             }
  111.         }
  112.         return false;
  113.     }
  114. }