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

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