vendor/store.shopware.com/molliepayments/src/Subscriber/SubscriptionSubscriber.php line 60

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