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. if (isset($payload['salesChannelId'])){        
  56. $configurationSalesChannelId $payload['salesChannelId'];
  57. }
  58.         if ($configurationKey !== self::SETTING_ALLOWED_KLARNA_PAYMENTS_CODES) {
  59.             return;
  60.         }
  61.         $activeMethodCodes $configurationValue;
  62.         if ($configurationSalesChannelId !== null) {
  63.             array_push($activeMethodCodes, ...$this->getActiveMethodCodes());
  64.         }
  65.         $salesChannelIds $this->salesChannelRepository->searchIds(new Criteria(), $context);
  66.         foreach ($salesChannelIds->getIds() as $checkSalesChannelId) {
  67.             if (is_string($checkSalesChannelId) && $checkSalesChannelId !== $configurationSalesChannelId) {
  68.                 array_push($activeMethodCodes, ...$this->getActiveMethodCodes($checkSalesChannelId));
  69.             }
  70.         }
  71.         $activeMethodCodes   array_filter(array_unique($activeMethodCodes));
  72.         $inactiveMethodCodes array_filter(array_values(array_diff(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES$activeMethodCodes)));
  73.         $upsertStatement     = [];
  74.         foreach (PaymentMethodInstaller::KLARNA_PAYMENTS_CODES as $paymentMethodId => $code) {
  75.             $upsertStatement[] = [
  76.                 'id'     => $paymentMethodId,
  77.                 'active' => !in_array($code$inactiveMethodCodestrue),
  78.             ];
  79.         }
  80.         $this->paymentMethodRepository->update($upsertStatement$context);
  81.     }
  82.     /**
  83.      * @return string[]
  84.      */
  85.     private function getActiveMethodCodes(?string $salesChannelId null): array
  86.     {
  87.         $activeMethodCodes = [];
  88.         $values            $this->systemConfigService->get(self::SETTING_ALLOWED_KLARNA_PAYMENTS_CODES$salesChannelId);
  89.         if (!is_array($values)) {
  90.             return [];
  91.         }
  92.         foreach ($values as $code) {
  93.             if ($code === PaymentMethodInstaller::KLARNA_PAYMENTS_PAY_NOW_CODE) {
  94.                 $activeMethodCodes[] = PaymentMethodInstaller::KLARNA_PAYMENTS_PAY_NOW_CODE;
  95.                 foreach (PaymentMethodInstaller::KLARNA_PAYMENTS_CODES_PAY_NOW_STANDALONE as $standalonePaymentId) {
  96.                     $activeMethodCodes[] = PaymentMethodInstaller::KLARNA_PAYMENTS_CODES[$standalonePaymentId];
  97.                 }
  98.                 continue;
  99.             }
  100.             $activeMethodCodes[] = $code;
  101.         }
  102.         return $activeMethodCodes;
  103.     }
  104. }