custom/plugins/NewsletterSendinblue/src/Subscriber/MailerSettingsChangeSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace NewsletterSendinblue\Subscriber;
  3. use Exception;
  4. use Monolog\Logger;
  5. use NewsletterSendinblue\NewsletterSendinblue;
  6. use NewsletterSendinblue\Service\ApiClientService;
  7. use NewsletterSendinblue\Service\ConfigService;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class MailerSettingsChangeSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var ConfigService
  14.      */
  15.     private $configService;
  16.     /**
  17.      * @var ApiClientService
  18.      */
  19.     private $apiClientService;
  20.     /**
  21.      * @var Logger
  22.      */
  23.     private $logger;
  24.     public function __construct(
  25.         ConfigService    $configService,
  26.         ApiClientService $apiClientService,
  27.         Logger           $logger
  28.     )
  29.     {
  30.         $this->configService $configService;
  31.         $this->apiClientService $apiClientService;
  32.         $this->logger $logger;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             EntityWrittenContainerEvent::class => 'onMailerSettingsChange'
  38.         ];
  39.     }
  40.     public function onMailerSettingsChange(EntityWrittenContainerEvent $event): void
  41.     {
  42.         if ($event->getContext()->hasExtension(NewsletterSendinblue::IGNORE_EVENT)) {
  43.             return;
  44.         }
  45.         try {
  46.             $entityWrittenEvent $event->getEventByEntityName('system_config');
  47.             if (!$entityWrittenEvent) {
  48.                 return;
  49.             }
  50.             foreach ($entityWrittenEvent->getWriteResults() as $writeResult) {
  51.                 if ($writeResult->getOperation() !== 'update') {
  52.                     continue;
  53.                 }
  54.                 $payload $writeResult->getPayload();
  55.                 if (empty($payload['configurationKey'])) {
  56.                     return;
  57.                 }
  58.                 $checkConnection = !($payload['configurationKey'] === ConfigService::CONFIG_USER_CONNECTION_ID);
  59.                 $this->configService->setSalesChannelId($payload['salesChannelId'] ?? null$checkConnection);
  60.                 if (!$this->configService->isSmtpEnabled() || !$this->configService->getSmtpHost()) {
  61.                     continue;
  62.                 }
  63.                 if (($payload['configurationKey'] == ConfigService::CORE_MAILER_HOST_CONFIG
  64.                         && $payload['configurationValue'] !== $this->configService->getSmtpHost())
  65.                     || ($payload['configurationKey'] == ConfigService::CORE_MAILER_AGENT_CONFIG
  66.                         && $payload['configurationValue'] !== ConfigService::CORE_MAILER_AGENT_VALUE)
  67.                 ) {
  68.                     $this->apiClientService->disableSmtp(
  69.                         [ConfigService::CONFIG_API_KEY => $this->configService->getApiKey()],
  70.                         $event->getContext()
  71.                     );
  72.                     $this->configService->setIsSmtpEnabled(false);
  73.                 }
  74.             }
  75.         } catch (Exception $e) {
  76.             $this->logger->addRecord(
  77.                 Logger::ERROR,
  78.                 sprintf('onMailerSettingsChange Error: %s'$e->getMessage())
  79.             );
  80.         }
  81.     }
  82. }