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

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