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

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