custom/plugins/MolliePayments/src/Subscriber/SubscriptionSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kiener\MolliePayments\Subscriber;
  4. use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\Struct\IntervalType;
  5. use Kiener\MolliePayments\Service\SettingsService;
  6. use Kiener\MolliePayments\Storefront\Struct\SubscriptionCartExtensionStruct;
  7. use Kiener\MolliePayments\Storefront\Struct\SubscriptionDataExtensionStruct;
  8. use Kiener\MolliePayments\Struct\LineItem\LineItemAttributes;
  9. use Kiener\MolliePayments\Struct\Product\ProductAttributes;
  10. use Shopware\Core\Checkout\Cart\Event\CartBeforeSerializationEvent;
  11. use Shopware\Storefront\Event\StorefrontRenderEvent;
  12. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\PageLoadedEvent;
  15. use Shopware\Storefront\Page\Product\ProductPage;
  16. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class SubscriptionSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var SettingsService
  23.      */
  24.     private $settingsService;
  25.     /**
  26.      * @var TranslatorInterface
  27.      */
  28.     private $translator;
  29.     public function __construct(SettingsService $settingsServiceTranslatorInterface $translator)
  30.     {
  31.         $this->settingsService $settingsService;
  32.         $this->translator $translator;
  33.     }
  34.     /**
  35.      * @return string[]
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             CartBeforeSerializationEvent::class => 'onBeforeSerializeCart',
  41.             // ------------------------------------------------------------------------
  42.             StorefrontRenderEvent::class => 'onStorefrontRender',
  43.             ProductPageLoadedEvent::class => 'addSubscriptionData',
  44.             CheckoutConfirmPageLoadedEvent::class => 'addSubscriptionData',
  45.         ];
  46.     }
  47.     /**
  48.      * this is required to allow our custom fields
  49.      * if we don't add them in here, then they will be removed for cart lineItems
  50.      * https://github.com/shopware/platform/blob/trunk/UPGRADE-6.5.md
  51.      */
  52.     public function onBeforeSerializeCart(CartBeforeSerializationEvent $event): void
  53.     {
  54.         $allowed $event->getCustomFieldAllowList();
  55.         foreach (LineItemAttributes::getKeyList() as $key) {
  56.             $allowed[] = $key;
  57.         }
  58.         $event->setCustomFieldAllowList($allowed);
  59.     }
  60.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  61.     {
  62.         $settings $this->settingsService->getSettings($event->getSalesChannelContext()->getSalesChannel()->getId());
  63.         $event->setParameter('mollie_subscriptions_enabled'$settings->isSubscriptionsEnabled());
  64.     }
  65.     public function addSubscriptionData(PageLoadedEvent $event): void
  66.     {
  67.         $settings $this->settingsService->getSettings($event->getSalesChannelContext()->getSalesChannel()->getId());
  68.         if (! $settings->isSubscriptionsEnabled()) {
  69.             $struct = new SubscriptionDataExtensionStruct(
  70.                 false,
  71.                 '',
  72.                 false
  73.             );
  74.             $event->getPage()->addExtension('mollieSubscription'$struct);
  75.             return;
  76.         }
  77.         $page $event->getPage();
  78.         if ($page instanceof ProductPage) {
  79.             $product $page->getProduct();
  80.             $productAttributes = new ProductAttributes($product);
  81.             $isSubscription $productAttributes->isSubscriptionProduct();
  82.             // only load our data if we really
  83.             // have a subscription product
  84.             if ($isSubscription) {
  85.                 $interval = (int) $productAttributes->getSubscriptionInterval();
  86.                 $unit = (string) $productAttributes->getSubscriptionIntervalUnit();
  87.                 $repetition = (int) $productAttributes->getSubscriptionRepetitionCount();
  88.                 $translatedInterval $this->getTranslatedInterval($interval$unit$repetition);
  89.                 $showIndicator $settings->isSubscriptionsShowIndicator();
  90.             } else {
  91.                 $translatedInterval '';
  92.                 $showIndicator false;
  93.             }
  94.             $struct = new SubscriptionDataExtensionStruct(
  95.                 $isSubscription,
  96.                 $translatedInterval,
  97.                 $showIndicator
  98.             );
  99.             $event->getPage()->addExtension('mollieSubscription'$struct);
  100.             return;
  101.         }
  102.         if ($page instanceof CheckoutConfirmPage) {
  103.             $subscriptionFound false;
  104.             foreach ($page->getCart()->getLineItems()->getFlat() as $lineItem) {
  105.                 $lineItemAttributes = new LineItemAttributes($lineItem);
  106.                 $isSubscription $lineItemAttributes->isSubscriptionProduct();
  107.                 if ($isSubscription) {
  108.                     $subscriptionFound true;
  109.                     $interval = (int) $lineItemAttributes->getSubscriptionInterval();
  110.                     $unit = (string) $lineItemAttributes->getSubscriptionIntervalUnit();
  111.                     $repetition = (int) $lineItemAttributes->getSubscriptionRepetition();
  112.                     $translatedInterval $this->getTranslatedInterval($interval$unit$repetition);
  113.                     $struct = new SubscriptionDataExtensionStruct(
  114.                         $isSubscription,
  115.                         $translatedInterval,
  116.                         false
  117.                     );
  118.                     $lineItem->addExtension('mollieSubscription'$struct);
  119.                 }
  120.             }
  121.             // we need this for some checks on the cart
  122.             $cartStruct = new SubscriptionCartExtensionStruct($subscriptionFound);
  123.             $event->getPage()->addExtension('mollieSubscriptionCart'$cartStruct);
  124.         }
  125.     }
  126.     private function getTranslatedInterval(int $intervalstring $unitint $repetition): string
  127.     {
  128.         $snippetKey '';
  129.         switch ($unit) {
  130.             case IntervalType::DAYS:
  131.                 if ($interval === 1) {
  132.                     $snippetKey 'molliePayments.subscriptions.options.everyDay';
  133.                 } else {
  134.                     $snippetKey 'molliePayments.subscriptions.options.everyDays';
  135.                 }
  136.                 break;
  137.             case IntervalType::WEEKS:
  138.                 if ($interval === 1) {
  139.                     $snippetKey 'molliePayments.subscriptions.options.everyWeek';
  140.                 } else {
  141.                     $snippetKey 'molliePayments.subscriptions.options.everyWeeks';
  142.                 }
  143.                 break;
  144.             case IntervalType::MONTHS:
  145.                 if ($interval === 1) {
  146.                     $snippetKey 'molliePayments.subscriptions.options.everyMonth';
  147.                 } else {
  148.                     $snippetKey 'molliePayments.subscriptions.options.everyMonths';
  149.                 }
  150.                 break;
  151.         }
  152.         $mainText $this->translator->trans($snippetKey, ['%value%' => $interval]);
  153.         if ($repetition >= 1) {
  154.             $mainText .= ', ' $this->translator->trans('molliePayments.subscriptions.options.repetitionCount', ['%value%' => $repetition]);
  155.         }
  156.         return $mainText;
  157.     }
  158. }