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

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