vendor/shopware/core/Framework/Webhook/Subscriber/RetryWebhookMessageFailedSubscriber.php line 39

  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\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Log\Package;
  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. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  12. /**
  13.  * @internal
  14.  */
  15. #[Package('core')]
  16. class RetryWebhookMessageFailedSubscriber implements EventSubscriberInterface
  17. {
  18.     private const MAX_WEBHOOK_ERROR_COUNT 10;
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(
  23.         private readonly EntityRepository $webhookRepository,
  24.         private readonly EntityRepository $webhookEventLogRepository
  25.     ) {
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             WorkerMessageFailedEvent::class => 'failed',
  31.         ];
  32.     }
  33.     public function failed(WorkerMessageFailedEvent $event): void
  34.     {
  35.         if ($event->willRetry()) {
  36.             return;
  37.         }
  38.         $message $event->getEnvelope()->getMessage();
  39.         if (!$message instanceof WebhookEventMessage) {
  40.             return;
  41.         }
  42.         $webhookId $message->getWebhookId();
  43.         $webhookEventLogId $message->getWebhookEventId();
  44.         $this->markWebhookEventFailed($webhookEventLogId);
  45.         /** @var WebhookEntity|null $webhook */
  46.         $webhook $this->webhookRepository
  47.             ->search(new Criteria([$webhookId]), Context::createDefaultContext())
  48.             ->get($webhookId);
  49.         if ($webhook === null || !$webhook->isActive()) {
  50.             return;
  51.         }
  52.         $webhookErrorCount $webhook->getErrorCount() + 1;
  53.         $params = [
  54.             'id' => $webhook->getId(),
  55.             'errorCount' => $webhookErrorCount,
  56.         ];
  57.         if ($webhookErrorCount >= self::MAX_WEBHOOK_ERROR_COUNT) {
  58.             $params array_merge($params, [
  59.                 'errorCount' => 0,
  60.                 'active' => false,
  61.             ]);
  62.         }
  63.         $this->webhookRepository->update([$params], Context::createDefaultContext());
  64.     }
  65.     private function markWebhookEventFailed(string $id): void
  66.     {
  67.         $this->webhookEventLogRepository->update([
  68.             ['id' => $id'deliveryStatus' => WebhookEventLogDefinition::STATUS_FAILED],
  69.         ], Context::createDefaultContext());
  70.     }
  71. }