platform/src/Core/Framework/Webhook/Subscriber/RetryWebhookMessageFailedSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Webhook\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Webhook\Event\RetryWebhookMessageFailedEvent;
  7. use Shopware\Core\Framework\Webhook\EventLog\WebhookEventLogDefinition;
  8. use Shopware\Core\Framework\Webhook\Message\WebhookEventMessage;
  9. use Shopware\Core\Framework\Webhook\WebhookEntity;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class RetryWebhookMessageFailedSubscriber implements EventSubscriberInterface
  12. {
  13.     private const MAX_WEBHOOK_ERROR_COUNT 10;
  14.     private const MAX_DEAD_MESSAGE_ERROR_COUNT 2;
  15.     private EntityRepositoryInterface $deadMessageRepository;
  16.     private EntityRepositoryInterface $webhookRepository;
  17.     private EntityRepositoryInterface $webhookEventLogRepository;
  18.     public function __construct(
  19.         EntityRepositoryInterface $deadMessageRepository,
  20.         EntityRepositoryInterface $webhookRepository,
  21.         EntityRepositoryInterface $webhookEventLogRepository
  22.     ) {
  23.         $this->deadMessageRepository $deadMessageRepository;
  24.         $this->webhookRepository $webhookRepository;
  25.         $this->webhookEventLogRepository $webhookEventLogRepository;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [RetryWebhookMessageFailedEvent::class => ['handleWebhookMessageFail']];
  30.     }
  31.     public function handleWebhookMessageFail(RetryWebhookMessageFailedEvent $event): void
  32.     {
  33.         $deadMessage $event->getDeadMessage();
  34.         $context $event->getContext();
  35.         if ($deadMessage->getErrorCount() < self::MAX_DEAD_MESSAGE_ERROR_COUNT) {
  36.             return;
  37.         }
  38.         /** @var WebhookEventMessage $webhookEventMessage */
  39.         $webhookEventMessage $deadMessage->getOriginalMessage();
  40.         $webhookId $webhookEventMessage->getWebhookId();
  41.         $webhookEventLogId $webhookEventMessage->getWebhookEventId();
  42.         $this->deleteDeadMessage($deadMessage->getId(), $context);
  43.         $this->markWebhookEventFailed($webhookEventLogId$context);
  44.         /** @var WebhookEntity|null $webhook */
  45.         $webhook $this->webhookRepository
  46.             ->search(new Criteria([$webhookId]), $context)
  47.             ->get($webhookId);
  48.         if ($webhook === null || !$webhook->isActive()) {
  49.             return;
  50.         }
  51.         $webhookErrorCount $webhook->getErrorCount() + 1;
  52.         $params = [
  53.             'id' => $webhook->getId(),
  54.             'errorCount' => $webhookErrorCount,
  55.         ];
  56.         if ($webhookErrorCount >= self::MAX_WEBHOOK_ERROR_COUNT) {
  57.             $params array_merge($params, [
  58.                 'errorCount' => 0,
  59.                 'active' => false,
  60.             ]);
  61.         }
  62.         $this->webhookRepository->update([$params], $context);
  63.     }
  64.     private function deleteDeadMessage(string $deadMessageIdContext $context): void
  65.     {
  66.         $this->deadMessageRepository->delete([
  67.             [
  68.                 'id' => $deadMessageId,
  69.             ],
  70.         ], $context);
  71.     }
  72.     private function markWebhookEventFailed(string $webhookEventLogIdContext $context): void
  73.     {
  74.         $this->webhookEventLogRepository->update([
  75.             [
  76.                 'id' => $webhookEventLogId,
  77.                 'deliveryStatus' => WebhookEventLogDefinition::STATUS_FAILED,
  78.             ],
  79.         ], $context);
  80.     }
  81. }