custom/plugins/MolliePayments/src/Components/Subscription/DAL/Subscription/Enricher/LiveDataEnricher.php line 59

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Components\Subscription\DAL\Subscription\Enricher;
  3. use DateInterval;
  4. use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\SubscriptionEntity;
  5. use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\SubscriptionEvents;
  6. use Kiener\MolliePayments\Gateway\MollieGatewayInterface;
  7. use Kiener\MolliePayments\Service\SettingsService;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class LiveDataEnricher implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SettingsService
  15.      */
  16.     private $pluginSettings;
  17.     /**
  18.      * @var MollieGatewayInterface
  19.      */
  20.     private $gwMollie;
  21.     /**
  22.      * @var LoggerInterface
  23.      */
  24.     private $logger;
  25.     /**
  26.      * @param SettingsService $pluginSettings
  27.      * @param MollieGatewayInterface $gwMollie
  28.      * @param LoggerInterface $logger
  29.      */
  30.     public function __construct(SettingsService $pluginSettingsMollieGatewayInterface $gwMollieLoggerInterface $logger)
  31.     {
  32.         $this->pluginSettings $pluginSettings;
  33.         $this->gwMollie $gwMollie;
  34.         $this->logger $logger;
  35.     }
  36.     /**
  37.      * @return string[]
  38.      */
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             SubscriptionEvents::SUBSCRIPTIONS_LOADED_EVENT => 'onSubscriptionsLoaded'
  43.         ];
  44.     }
  45.     /**
  46.      * @param EntityLoadedEvent $event
  47.      */
  48.     public function onSubscriptionsLoaded(EntityLoadedEvent $event): void
  49.     {
  50.         /** @var SubscriptionEntity $subscription */
  51.         foreach ($event->getEntities() as $subscription) {
  52.             try {
  53.                 # ----------------------------------------------------------------------------------------------------
  54.                 # set the cancellation until-date depending on our plugin configuration
  55.                 $settings $this->pluginSettings->getSettings($subscription->getSalesChannelId());
  56.                 $cancellationDays $settings->getSubscriptionsCancellationDays();
  57.                 if ($cancellationDays <= 0) {
  58.                     # use the next payment date
  59.                     $subscription->setCancelUntil($subscription->getNextPaymentAt());
  60.                 } else {
  61.                     # remove x days from the next renewal date (if existing)
  62.                     $nextPayment $subscription->getNextPaymentAt();
  63.                     $lastPossibleDate null;
  64.                     if ($nextPayment instanceof \DateTimeImmutable) {
  65.                         $lastPossibleDate $nextPayment->sub(new DateInterval('P' $cancellationDays 'D'));
  66.                     }
  67.                     $subscription->setCancelUntil($lastPossibleDate);
  68.                 }
  69.                 # ----------------------------------------------------------------------------------------------------
  70.                 # now also get the live status from mollie and their API
  71.                 $this->gwMollie->switchClient($subscription->getSalesChannelId());
  72.                 $mollieSubscription $this->gwMollie->getSubscription($subscription->getMollieId(), $subscription->getMollieCustomerId());
  73.                 $subscription->setMollieStatus($mollieSubscription->status);
  74.             } catch (\Throwable $ex) {
  75.                 $this->logger->error(
  76.                     'Error when enriching Subscription with additional data',
  77.                     [
  78.                         'exception' => $ex,
  79.                     ]
  80.                 );
  81.             }
  82.         }
  83.     }
  84. }