custom/plugins/KlarnaPayment/src/Components/EventListener/ConfigWrittenSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Installer\Modules\PaymentMethodInstaller;
  5. use Shopware\Core\Defaults;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\System\SystemConfig\SystemConfigDefinition;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ConfigWrittenSubscriber implements EventSubscriberInterface
  13. {
  14.     private const SETTING_ALLOWED_KLARNA_PAYMENTS_CODES 'KlarnaPayment.settings.allowedKlarnaPaymentsCodes';
  15.     /** @var EntityRepositoryInterface */
  16.     private $paymentMethodRepository;
  17.     /** @var EntityRepositoryInterface */
  18.     private $salesChannelRepository;
  19.     /** @var SystemConfigService */
  20.     private $systemConfigService;
  21.     public function __construct(
  22.         EntityRepositoryInterface $paymentMethodRepository,
  23.         EntityRepositoryInterface $salesChannelRepository,
  24.         SystemConfigService $systemConfigService
  25.     ) {
  26.         $this->paymentMethodRepository $paymentMethodRepository;
  27.         $this->salesChannelRepository  $salesChannelRepository;
  28.         $this->systemConfigService     $systemConfigService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             EntityWrittenContainerEvent::class => 'onEntityWrittenContainerEvent',
  34.         ];
  35.     }
  36.     public function onEntityWrittenContainerEvent(EntityWrittenContainerEvent $containerEvent): void
  37.     {
  38.         $event $containerEvent->getEventByEntityName(SystemConfigDefinition::ENTITY_NAME);
  39.         if ($event === null || $event->hasErrors() === true
  40.             || $event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  41.             return;
  42.         }
  43.         $context $event->getContext();
  44.         $writeResults $event->getWriteResults();
  45.         /** @var \Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult $writeResult */
  46.         $writeResult end($writeResults);
  47.         $payload                     $writeResult->getPayload();
  48.         $configurationKey            $payload['configurationKey'];
  49.         $configurationValue          $payload['configurationValue'];
  50.         $configurationSalesChannelId $payload['salesChannelId'];
  51.         if ($configurationKey !== self::SETTING_ALLOWED_KLARNA_PAYMENTS_CODES) {
  52.             return;
  53.         }
  54.         $activeMethodCodes $configurationValue;
  55.         if ($configurationSalesChannelId !== null) {
  56.             array_push($activeMethodCodes, ...$this->getActiveMethodCodes());
  57.         }
  58.         $salesChannelIds $this->salesChannelRepository->searchIds(new Criteria(), $context);
  59.         foreach ($salesChannelIds->getIds() as $checkSalesChannelId) {
  60.             if (is_string($checkSalesChannelId) && $checkSalesChannelId !== $configurationSalesChannelId) {
  61.                 array_push($activeMethodCodes, ...$this->getActiveMethodCodes($checkSalesChannelId));
  62.             }
  63.         }
  64.         $activeMethodCodes   array_filter(array_unique($activeMethodCodes));
  65.         $inactiveMethodCodes array_filter(array_values(array_diff(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES$activeMethodCodes)));
  66.         $upsertStatement     = [];
  67.         foreach (PaymentMethodInstaller::KLARNA_PAYMENTS_CODES as $paymentMethodId => $code) {
  68.             $upsertStatement[] = [
  69.                 'id'     => $paymentMethodId,
  70.                 'active' => !in_array($code$inactiveMethodCodestrue),
  71.             ];
  72.         }
  73.         if (empty($upsertStatement)) {
  74.             return;
  75.         }
  76.         $this->paymentMethodRepository->update($upsertStatement$context);
  77.     }
  78.     private function getActiveMethodCodes(?string $salesChannelId null): array
  79.     {
  80.         $activeMethodCodes = [];
  81.         $values            $this->systemConfigService->get(self::SETTING_ALLOWED_KLARNA_PAYMENTS_CODES$salesChannelId);
  82.         if (!is_array($values)) {
  83.             return [];
  84.         }
  85.         foreach ($values as $code) {
  86.             if ($code === PaymentMethodInstaller::KLARNA_PAYMENTS_PAY_NOW_CODE) {
  87.                 $activeMethodCodes[] = PaymentMethodInstaller::KLARNA_PAYMENTS_PAY_NOW_CODE;
  88.                 foreach (PaymentMethodInstaller::KLARNA_PAYMENTS_CODES_PAY_NOW_STANDALONE as $standalonePaymentId) {
  89.                     $activeMethodCodes[] = PaymentMethodInstaller::KLARNA_PAYMENTS_CODES[$standalonePaymentId];
  90.                 }
  91.                 continue;
  92.             }
  93.             $activeMethodCodes[] = $code;
  94.         }
  95.         return $activeMethodCodes;
  96.     }
  97. }