custom/plugins/PayonePayment/src/EventListener/StorefrontRenderEventListener.php line 69

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Installer\PaymentMethodInstaller;
  5. use Psr\Cache\CacheItemPoolInterface;
  6. use Shopware\Core\Checkout\Payment\PaymentMethodDefinition;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelPaymentMethod\SalesChannelPaymentMethodDefinition;
  14. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Storefront\Event\StorefrontRenderEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class StorefrontRenderEventListener implements EventSubscriberInterface
  19. {
  20.     private CacheItemPoolInterface $cachePool;
  21.     private SalesChannelRepositoryInterface $paymentMethodRepository;
  22.     private EntityRepositoryInterface $salesChannelRepository;
  23.     public function __construct(
  24.         CacheItemPoolInterface $cachePool,
  25.         SalesChannelRepositoryInterface $repository,
  26.         EntityRepositoryInterface $salesChannelRepository
  27.     ) {
  28.         $this->cachePool $cachePool;
  29.         $this->paymentMethodRepository $repository;
  30.         $this->salesChannelRepository $salesChannelRepository;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             StorefrontRenderEvent::class => 'onRender',
  36.             EntityWrittenContainerEvent::class => 'onEntityWritten',
  37.         ];
  38.     }
  39.     public function onRender(StorefrontRenderEvent $event): void
  40.     {
  41.         $cacheKey $this->generateCacheKey(
  42.             $event->getSalesChannelContext()->getSalesChannel()->getId()
  43.         );
  44.         $activePaymentMethods $this->cachePool->getItem($cacheKey);
  45.         if ($activePaymentMethods->get() === null) {
  46.             $activePaymentMethods->set(
  47.                 $this->collectActivePayonePaymentMethods(
  48.                     $event->getSalesChannelContext()
  49.                 )
  50.             );
  51.             $this->cachePool->save($activePaymentMethods);
  52.         }
  53.         $event->setParameter('activePaymentPaymentMethods'$activePaymentMethods->get());
  54.     }
  55.     public function onEntityWritten(EntityWrittenContainerEvent $event): void
  56.     {
  57.         $ids = [];
  58.         $paymentMethodEvents $event->getEventByEntityName(PaymentMethodDefinition::ENTITY_NAME);
  59.         if ($paymentMethodEvents !== null) {
  60.             $ids array_merge($ids$this->collectPrimaryKeys($paymentMethodEvents->getIds()));
  61.         }
  62.         $salesChannelEvents $event->getEventByEntityName(SalesChannelPaymentMethodDefinition::ENTITY_NAME);
  63.         if ($salesChannelEvents !== null) {
  64.             $ids array_merge($ids$this->collectPrimaryKeys($salesChannelEvents->getIds()));
  65.         }
  66.         if (empty($ids)) {
  67.             return;
  68.         }
  69.         $clearCache false;
  70.         foreach (PaymentMethodInstaller::PAYMENT_METHODS as $paymentMethod) {
  71.             if (\in_array($paymentMethod::UUID$idstrue)) {
  72.                 $clearCache true;
  73.             }
  74.         }
  75.         if ($clearCache) {
  76.             $this->clearCache($event->getContext());
  77.         }
  78.     }
  79.     private function collectPrimaryKeys(array $primaryKeys): array
  80.     {
  81.         $ids = [];
  82.         foreach ($primaryKeys as $key) {
  83.             if (\is_array($key)) {
  84.                 $ids array_merge($idsarray_values($key));
  85.             } else {
  86.                 $ids[] = $key;
  87.             }
  88.         }
  89.         return $ids;
  90.     }
  91.     private function collectActivePayonePaymentMethods(SalesChannelContext $context): array
  92.     {
  93.         $criteria = new Criteria();
  94.         $criteria->addFilter(new ContainsFilter('handlerIdentifier''PayonePayment'));
  95.         $criteria->addFilter(new EqualsFilter('active'true));
  96.         return $this->paymentMethodRepository->search($criteria$context)->getIds();
  97.     }
  98.     private function generateCacheKey(string $salesChannel): string
  99.     {
  100.         return 'payone_payment.menu_state.' $salesChannel;
  101.     }
  102.     private function clearCache(Context $context): void
  103.     {
  104.         $cacheKeys = [];
  105.         /** @var string[] $salesChannels */
  106.         $salesChannels $this->salesChannelRepository->searchIds(new Criteria(), $context)->getIds();
  107.         foreach ($salesChannels as $salesChannel) {
  108.             $cacheKeys[] = $this->generateCacheKey($salesChannel);
  109.         }
  110.         if (empty($cacheKeys)) {
  111.             return;
  112.         }
  113.         $this->cachePool->deleteItems($cacheKeys);
  114.     }
  115. }