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

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\Repository\SubscriptionRepository;
  5. use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\SubscriptionEntity;
  6. use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\SubscriptionEvents;
  7. use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\SubscriptionStatus;
  8. use Kiener\MolliePayments\Gateway\MollieGatewayInterface;
  9. use Kiener\MolliePayments\Service\SettingsService;
  10. use Psr\Log\LoggerInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class LiveDataEnricher implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var SettingsService
  17.      */
  18.     private $pluginSettings;
  19.     /**
  20.      * @var MollieGatewayInterface
  21.      */
  22.     private $gwMollie;
  23.     /**
  24.      * @var SubscriptionRepository
  25.      */
  26.     private $repoSubscriptions;
  27.     /**
  28.      * @var LoggerInterface
  29.      */
  30.     private $logger;
  31.     /**
  32.      * @param SettingsService $pluginSettings
  33.      * @param MollieGatewayInterface $gwMollie
  34.      * @param SubscriptionRepository $repoSubscriptions
  35.      * @param LoggerInterface $logger
  36.      */
  37.     public function __construct(SettingsService $pluginSettingsMollieGatewayInterface $gwMollieSubscriptionRepository $repoSubscriptionsLoggerInterface $logger)
  38.     {
  39.         $this->pluginSettings $pluginSettings;
  40.         $this->gwMollie $gwMollie;
  41.         $this->repoSubscriptions $repoSubscriptions;
  42.         $this->logger $logger;
  43.     }
  44.     /**
  45.      * @return string[]
  46.      */
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             SubscriptionEvents::SUBSCRIPTIONS_LOADED_EVENT => 'onSubscriptionsLoaded'
  51.         ];
  52.     }
  53.     /**
  54.      * @param EntityLoadedEvent $event
  55.      */
  56.     public function onSubscriptionsLoaded(EntityLoadedEvent $event): void
  57.     {
  58.         /** @var SubscriptionEntity $subscription */
  59.         foreach ($event->getEntities() as $subscription) {
  60.             try {
  61.                 # ----------------------------------------------------------------------------------------------------
  62.                 # set the cancellation until-date depending on our plugin configuration
  63.                 $settings $this->pluginSettings->getSettings($subscription->getSalesChannelId());
  64.                 $cancellationDays $settings->getSubscriptionsCancellationDays();
  65.                 if ($cancellationDays <= 0) {
  66.                     # use the next payment date
  67.                     $subscription->setCancelUntil($subscription->getNextPaymentAt());
  68.                 } else {
  69.                     # remove x days from the next renewal date (if existing)
  70.                     $nextPayment $subscription->getNextPaymentAt();
  71.                     $lastPossibleDate null;
  72.                     if ($nextPayment instanceof \DateTimeImmutable) {
  73.                         $lastPossibleDate $nextPayment->sub(new DateInterval('P' $cancellationDays 'D'));
  74.                     }
  75.                     $subscription->setCancelUntil($lastPossibleDate);
  76.                 }
  77.                 # ----------------------------------------------------------------------------------------------------
  78.                 # now get the mollie status if we don't have one in our subscription
  79.                 # this is for backward compatibility, because our local status is new
  80.                 if ($subscription->getStatus() === '') {
  81.                     $this->gwMollie->switchClient($subscription->getSalesChannelId());
  82.                     $mollieSubscription $this->gwMollie->getSubscription($subscription->getMollieId(), $subscription->getMollieCustomerId());
  83.                     # convert into our internal one
  84.                     # and update in our database
  85.                     $internalStatus SubscriptionStatus::fromMollieStatus($mollieSubscription->status);
  86.                     $subscription->setStatus($internalStatus);
  87.                     $this->repoSubscriptions->updateStatus($subscription->getId(), $mollieSubscription->status$event->getContext());
  88.                 }
  89.             } catch (\Throwable $ex) {
  90.                 $this->logger->error(
  91.                     'Error when enriching Subscription with additional data',
  92.                     [
  93.                         'exception' => $ex,
  94.                     ]
  95.                 );
  96.             }
  97.         }
  98.     }
  99. }