custom/plugins/SwagPayPal/src/Webhook/Registration/WebhookSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Webhook\Registration;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  10. use Shopware\Core\System\SalesChannel\SalesChannelEvents;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Swag\PayPal\Setting\Settings;
  13. use Swag\PayPal\Webhook\WebhookServiceInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16.  * @internal
  17.  */
  18. class WebhookSubscriber implements EventSubscriberInterface
  19. {
  20.     private LoggerInterface $logger;
  21.     private SystemConfigService $systemConfigService;
  22.     private WebhookServiceInterface $webhookService;
  23.     public function __construct(
  24.         LoggerInterface $logger,
  25.         SystemConfigService $systemConfigService,
  26.         WebhookServiceInterface $webhookService
  27.     ) {
  28.         $this->logger $logger;
  29.         $this->systemConfigService $systemConfigService;
  30.         $this->webhookService $webhookService;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             SalesChannelEvents::SALES_CHANNEL_DELETED => 'removeSalesChannelWebhookConfiguration',
  36.         ];
  37.     }
  38.     public function removeSalesChannelWebhookConfiguration(EntityDeletedEvent $event): void
  39.     {
  40.         $generalWebhookId $this->systemConfigService->getString(Settings::WEBHOOK_ID);
  41.         foreach ($event->getIds() as $salesChannelId) {
  42.             $webhookId $this->systemConfigService->getString(Settings::WEBHOOK_ID$salesChannelId);
  43.             try {
  44.                 if ($webhookId !== $generalWebhookId) {
  45.                     $this->webhookService->deregisterWebhook($salesChannelId);
  46.                 }
  47.             } catch (\Throwable $e) {
  48.                 $this->logger->error($e->getMessage(), ['error' => $e]);
  49.             }
  50.         }
  51.     }
  52. }