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

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\EntityRepository;
  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.     // TODO: Adjust this if compatibility is at least > 6.4.0.0
  16.     /** @var EntityRepository|\Shopware\Core\Checkout\Payment\DataAbstractionLayer\PaymentMethodRepositoryDecorator */
  17.     private $paymentMethodRepository;
  18.     /** @var EntityRepository */
  19.     private $salesChannelRepository;
  20.     /** @var SystemConfigService */
  21.     private $systemConfigService;
  22.     // TODO: Adjust this if compatibility is at least > 6.4.0.0
  23.     /**
  24.      * @param EntityRepository|\Shopware\Core\Checkout\Payment\DataAbstractionLayer\PaymentMethodRepositoryDecorator $paymentMethodRepository
  25.      */
  26.     public function __construct(
  27.         $paymentMethodRepository,
  28.         EntityRepository $salesChannelRepository,
  29.         SystemConfigService $systemConfigService
  30.     ) {
  31.         $this->paymentMethodRepository $paymentMethodRepository;
  32.         $this->salesChannelRepository  $salesChannelRepository;
  33.         $this->systemConfigService     $systemConfigService;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             EntityWrittenContainerEvent::class => 'onEntityWrittenContainerEvent',
  39.         ];
  40.     }
  41.     public function onEntityWrittenContainerEvent(EntityWrittenContainerEvent $containerEvent): void
  42.     {
  43.         $event $containerEvent->getEventByEntityName(SystemConfigDefinition::ENTITY_NAME);
  44.         if ($event === null || $event->hasErrors() === true
  45.             || $event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  46.             return;
  47.         }
  48.         $context $event->getContext();
  49.         $writeResults $event->getWriteResults();
  50.         /** @var \Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult $writeResult */
  51.         $writeResult end($writeResults);
  52.         $payload                     $writeResult->getPayload();
  53.         $configurationKey            $payload['configurationKey'];
  54.         $configurationValue          $payload['configurationValue'];
  55.         $configurationSalesChannelId $payload['salesChannelId'];
  56.         if ($configurationKey !== self::SETTING_ALLOWED_KLARNA_PAYMENTS_CODES) {
  57.             return;
  58.         }
  59.         $activeMethodCodes $configurationValue;
  60.         if ($configurationSalesChannelId !== null) {
  61.             array_push($activeMethodCodes, ...$this->getActiveMethodCodes());
  62.         }
  63.         $salesChannelIds $this->salesChannelRepository->searchIds(new Criteria(), $context);
  64.         foreach ($salesChannelIds->getIds() as $checkSalesChannelId) {
  65.             if (is_string($checkSalesChannelId) && $checkSalesChannelId !== $configurationSalesChannelId) {
  66.                 array_push($activeMethodCodes, ...$this->getActiveMethodCodes($checkSalesChannelId));
  67.             }
  68.         }
  69.         $activeMethodCodes   array_filter(array_unique($activeMethodCodes));
  70.         $inactiveMethodCodes array_filter(array_values(array_diff(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES$activeMethodCodes)));
  71.         $upsertStatement     = [];
  72.         foreach (PaymentMethodInstaller::KLARNA_PAYMENTS_CODES as $paymentMethodId => $code) {
  73.             $upsertStatement[] = [
  74.                 'id'     => $paymentMethodId,
  75.                 'active' => !in_array($code$inactiveMethodCodestrue),
  76.             ];
  77.         }
  78.         $this->paymentMethodRepository->update($upsertStatement$context);
  79.     }
  80.     /**
  81.      * @return string[]
  82.      */
  83.     private function getActiveMethodCodes(?string $salesChannelId null): array
  84.     {
  85.         $activeMethodCodes = [];
  86.         $values            $this->systemConfigService->get(self::SETTING_ALLOWED_KLARNA_PAYMENTS_CODES$salesChannelId);
  87.         if (!is_array($values)) {
  88.             return [];
  89.         }
  90.         foreach ($values as $code) {
  91.             if ($code === PaymentMethodInstaller::KLARNA_PAYMENTS_PAY_NOW_CODE) {
  92.                 $activeMethodCodes[] = PaymentMethodInstaller::KLARNA_PAYMENTS_PAY_NOW_CODE;
  93.                 foreach (PaymentMethodInstaller::KLARNA_PAYMENTS_CODES_PAY_NOW_STANDALONE as $standalonePaymentId) {
  94.                     $activeMethodCodes[] = PaymentMethodInstaller::KLARNA_PAYMENTS_CODES[$standalonePaymentId];
  95.                 }
  96.                 continue;
  97.             }
  98.             $activeMethodCodes[] = $code;
  99.         }
  100.         return $activeMethodCodes;
  101.     }
  102. }