|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Adyen Payment module (https://www.adyen.com/) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) |
| 7 | + * See LICENSE.txt for license details. |
| 8 | + * |
| 9 | + * Author: Adyen <[email protected]> |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Adyen\Payment\Cron; |
| 13 | + |
| 14 | +use Adyen\Payment\Api\Repository\AdyenNotificationRepositoryInterface; |
| 15 | +use Adyen\Payment\Cron\Providers\WebhooksProviderInterface; |
| 16 | +use Adyen\Payment\Helper\Config; |
| 17 | +use Adyen\Payment\Logger\AdyenLogger; |
| 18 | +use Exception; |
| 19 | + |
| 20 | +class RemoveProcessedWebhooks |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @param WebhooksProviderInterface[] $providers |
| 24 | + * @param AdyenLogger $adyenLogger |
| 25 | + * @param Config $configHelper |
| 26 | + * @param AdyenNotificationRepositoryInterface $adyenNotificationRepository |
| 27 | + */ |
| 28 | + public function __construct( |
| 29 | + private readonly array $providers, |
| 30 | + private readonly AdyenLogger $adyenLogger, |
| 31 | + private readonly Config $configHelper, |
| 32 | + private readonly AdyenNotificationRepositoryInterface $adyenNotificationRepository |
| 33 | + ) { } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function execute(): void |
| 39 | + { |
| 40 | + $isWebhookCleanupEnabled = $this->configHelper->getIsProcessedWebhookRemovalEnabled(); |
| 41 | + |
| 42 | + if ($isWebhookCleanupEnabled === true) { |
| 43 | + $numberOfItemsRemoved = 0; |
| 44 | + |
| 45 | + foreach ($this->providers as $provider) { |
| 46 | + $webhookIdsToRemove = $provider->provide(); |
| 47 | + $numberOfWebhooksProvided = count($webhookIdsToRemove); |
| 48 | + |
| 49 | + if ($numberOfWebhooksProvided > 0) { |
| 50 | + try { |
| 51 | + $this->adyenNotificationRepository->deleteByIds($webhookIdsToRemove); |
| 52 | + $numberOfItemsRemoved += $numberOfWebhooksProvided; |
| 53 | + } catch (Exception $e) { |
| 54 | + $message = __( |
| 55 | + '%1: An error occurred while deleting webhooks! %2', |
| 56 | + $provider->getProviderName(), |
| 57 | + $e->getMessage() |
| 58 | + ); |
| 59 | + |
| 60 | + $this->adyenLogger->error($message); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if ($numberOfItemsRemoved > 0) { |
| 66 | + $successMessage = __( |
| 67 | + '%1 processed webhooks have been removed by the RemoveProcessedWebhooks cronjob.', |
| 68 | + $numberOfItemsRemoved |
| 69 | + ); |
| 70 | + |
| 71 | + $this->adyenLogger->addAdyenNotification($successMessage); |
| 72 | + } else { |
| 73 | + $debugMessage = __( |
| 74 | + 'There is no webhooks to be removed by RemoveProcessedWebhooks cronjob.', |
| 75 | + $numberOfItemsRemoved |
| 76 | + ); |
| 77 | + |
| 78 | + $this->adyenLogger->addAdyenDebug($debugMessage); |
| 79 | + } |
| 80 | + } else { |
| 81 | + $message = __('Processed webhook removal feature is disabled. The cronjob has been skipped!'); |
| 82 | + $this->adyenLogger->addAdyenDebug($message); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments