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

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 Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CachedPaymentMethodRoute64 implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var CartService
  15.      */
  16.     private $cartService;
  17.     /**
  18.      * @var SettingsService
  19.      */
  20.     private $pluginSettings;
  21.     /**
  22.      * @param SettingsService $pluginSettings
  23.      * @param CartService $cartService
  24.      */
  25.     public function __construct(SettingsService $pluginSettingsCartService $cartService)
  26.     {
  27.         $this->pluginSettings $pluginSettings;
  28.         $this->cartService $cartService;
  29.     }
  30.     /**
  31.      * @return string[]
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             PaymentMethodRouteCacheKeyEvent::class => 'onGenerateCacheKey',
  37.         ];
  38.     }
  39.     /**
  40.      * This function will make sure that we have a working cache key for all dynamic payment method
  41.      * situations that could occur.
  42.      * So we need to determine if we have a voucher product in it, or not...otherwise the dynamic display
  43.      * of these payment methods (in other route handlers) would not work.
  44.      * @param PaymentMethodRouteCacheKeyEvent $event
  45.      */
  46.     public function onGenerateCacheKey(PaymentMethodRouteCacheKeyEvent $event): void
  47.     {
  48.         $originalRuleIds $event->getContext()->getRuleIds();
  49.         /**
  50.          * the cart service changes the rule ids based on cart.
  51.          * after failed payment we are not in cart anymore but instead on edit order
  52.          * in this case the cart is empty so the rules will be reset
  53.          */
  54.         $cart $this->cartService->getCart($event->getContext()->getToken(), $event->getContext());
  55.         /** we have to collect the original rules before cart service is called and set them again */
  56.         $event->getContext()->setRuleIds($originalRuleIds);
  57.         $parts $event->getParts();
  58.         $cacheParts = [];
  59.         $cacheParts $this->addVoucherKey($cart$cacheParts);
  60.         $cacheParts $this->addMollieLimitsKey($cacheParts);
  61.         $cacheParts $this->addSubscriptionKey($cart$cacheParts);
  62.         $cacheParts $this->addCartAmountKey($cart$cacheParts);
  63.         $cacheParts $this->addCurrencyCodeKey($event->getContext(), $cacheParts);
  64.         $cacheParts $this->addBillingAddressKey($event->getContext(), $cacheParts);
  65.         $parts[] = md5(implode('-'$cacheParts));
  66.         $event->setParts($parts);
  67.     }
  68.     /**
  69.      * @param Cart $cart
  70.      * @param array<mixed> $parts
  71.      *
  72.      * @return array<mixed>
  73.      */
  74.     private function addVoucherKey(Cart $cart, array $parts): array
  75.     {
  76.         $voucherPermitted = (bool)$cart->getData()->get(VoucherCartCollector::VOUCHER_PERMITTED);
  77.         if ($voucherPermitted) {
  78.             $parts[] = 'with-voucher';
  79.         } else {
  80.             $parts[] = 'without-voucher';
  81.         }
  82.         return $parts;
  83.     }
  84.     /**
  85.      * @param array<mixed> $parts
  86.      * @return array<mixed>
  87.      */
  88.     private function addMollieLimitsKey(array $parts): array
  89.     {
  90.         $settings $this->pluginSettings->getSettings();
  91.         if ($settings->getUseMolliePaymentMethodLimits()) {
  92.             $parts[] = 'with-limits';
  93.         } else {
  94.             $parts[] = 'without-limits';
  95.         }
  96.         return $parts;
  97.     }
  98.     /**
  99.      * @param Cart $cart
  100.      * @param array<mixed> $parts
  101.      *
  102.      * @return array<mixed>
  103.      */
  104.     private function addSubscriptionKey(Cart $cart, array $parts): array
  105.     {
  106.         $hasSubscriptionItems $this->isSubscriptionCart($cart);
  107.         if ($hasSubscriptionItems) {
  108.             $parts[] = 'with-subscription';
  109.         } else {
  110.             $parts[] = 'without-subscription';
  111.         }
  112.         return $parts;
  113.     }
  114.     /**
  115.      * @param Cart $cart
  116.      * @return bool
  117.      */
  118.     private function isSubscriptionCart(Cart $cart): bool
  119.     {
  120.         foreach ($cart->getLineItems() as $lineItem) {
  121.             $attribute = new LineItemAttributes($lineItem);
  122.             if ($attribute->isSubscriptionProduct()) {
  123.                 return true;
  124.             }
  125.         }
  126.         return false;
  127.     }
  128.     /**
  129.      * @param Cart $cart
  130.      * @param array<mixed> $cacheParts
  131.      * @return array<mixed>
  132.      */
  133.     private function addCartAmountKey(Cart $cart, array $cacheParts): array
  134.     {
  135.         $cacheParts[] = $cart->getPrice()->getTotalPrice();
  136.         return $cacheParts;
  137.     }
  138.     /**
  139.      * @param SalesChannelContext $context
  140.      * @param array<mixed> $cacheParts
  141.      * @return array<mixed>
  142.      */
  143.     private function addCurrencyCodeKey(SalesChannelContext $context, array $cacheParts):array
  144.     {
  145.         $cacheParts[] = $context->getCurrency()->getIsoCode();
  146.         return $cacheParts;
  147.     }
  148.     /**
  149.      * @param SalesChannelContext $context
  150.      * @param array<mixed> $cacheParts
  151.      * @return array<mixed>
  152.      */
  153.     private function addBillingAddressKey(SalesChannelContext $context, array $cacheParts):array
  154.     {
  155.         $customer $context->getCustomer();
  156.         if ($customer === null) {
  157.             return $cacheParts;
  158.         }
  159.         $billingAddress $customer->getActiveBillingAddress();
  160.         if ($billingAddress === null) {
  161.             return $cacheParts;
  162.         }
  163.         $cacheParts[]=$billingAddress->getId();
  164.         return $cacheParts;
  165.     }
  166. }