vendor/lnb/shopware6-lnb-subscription/src/Subscriber/Subscription/SubscriptionHistorySubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace Lnb\Shopware6\LnbSubscription\Subscriber\Subscription;
  3. use Lnb\Shopware6\LnbSubscription\Entity\SubscriptionOrder\SubscriptionOrderDefinition;
  4. use Lnb\Shopware6\LnbSubscription\Service\History\SubscriptionOrderHistoryService;
  5. use Psr\Log\LoggerInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class SubscriptionHistorySubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private readonly SubscriptionOrderHistoryService $subscriptionOrderHistoryService,
  12.         private readonly LoggerInterface $logger
  13.     ) {
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             SubscriptionOrderDefinition::ENTITY_NAME '.written' => 'onSubscriptionWritten'
  19.         ];
  20.     }
  21.     public function onSubscriptionWritten(EntityWrittenEvent $event): void
  22.     {
  23.         $results $event->getWriteResults();
  24.         try {
  25.             $this->subscriptionOrderHistoryService->setUserId($event->getContext()->getSource());
  26.             foreach ($results as $result) {
  27.                 if ($result->getExistence()->exists()) {
  28.                     $this->subscriptionOrderHistoryService->logUpdate($result);
  29.                 } else {
  30.                     $this->subscriptionOrderHistoryService->logInitial($result);
  31.                 }
  32.             }
  33.             $this->subscriptionOrderHistoryService->save();
  34.         } catch (\Throwable $exception) {
  35.             $this->logger->error(
  36.                 sprintf('Could not create history entry for subscription. Error: %s'$exception->getMessage()),
  37.                 [
  38.                     'trace' => $exception->getTraceAsString(),
  39.                     'eventIds' => $event->getIds()
  40.                 ]
  41.             );
  42.         }
  43.     }
  44. }