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

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.     /** @var CacheItemPoolInterface */
  21.     private $cachePool;
  22.     /** @var SalesChannelRepositoryInterface */
  23.     private $paymentMethodRepository;
  24.     /** @var EntityRepositoryInterface */
  25.     private $salesChannelRepository;
  26.     public function __construct(
  27.         CacheItemPoolInterface $cachePool,
  28.         SalesChannelRepositoryInterface $repository,
  29.         EntityRepositoryInterface $salesChannelRepository
  30.     ) {
  31.         $this->cachePool               $cachePool;
  32.         $this->paymentMethodRepository $repository;
  33.         $this->salesChannelRepository  $salesChannelRepository;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             StorefrontRenderEvent::class       => 'onRender',
  39.             EntityWrittenContainerEvent::class => 'onEntityWritten',
  40.         ];
  41.     }
  42.     public function onRender(StorefrontRenderEvent $event): void
  43.     {
  44.         $cacheKey $this->generateCacheKey(
  45.             $event->getSalesChannelContext()->getSalesChannel()->getId()
  46.         );
  47.         $activePaymentMethods $this->cachePool->getItem($cacheKey);
  48.         if ($activePaymentMethods->get() === null) {
  49.             $activePaymentMethods->set(
  50.                 $this->collectActivePayonePaymentMethods(
  51.                     $event->getSalesChannelContext()
  52.                 )
  53.             );
  54.             $this->cachePool->save($activePaymentMethods);
  55.         }
  56.         $event->setParameter('activePaymentPaymentMethods'$activePaymentMethods->get());
  57.     }
  58.     public function onEntityWritten(EntityWrittenContainerEvent $event): void
  59.     {
  60.         $ids = [];
  61.         $paymentMethodEvents $event->getEventByEntityName(PaymentMethodDefinition::ENTITY_NAME);
  62.         if (null !== $paymentMethodEvents) {
  63.             $ids array_merge($ids$this->collectPrimaryKeys($paymentMethodEvents->getIds()));
  64.         }
  65.         $salesChannelEvents $event->getEventByEntityName(SalesChannelPaymentMethodDefinition::ENTITY_NAME);
  66.         if (null !== $salesChannelEvents) {
  67.             $ids array_merge($ids$this->collectPrimaryKeys($salesChannelEvents->getIds()));
  68.         }
  69.         if (empty($ids)) {
  70.             return;
  71.         }
  72.         $clearCache false;
  73.         foreach (PaymentMethodInstaller::PAYMENT_METHODS as $paymentMethod) {
  74.             if (in_array($paymentMethod::UUID$idstrue)) {
  75.                 $clearCache true;
  76.             }
  77.         }
  78.         if ($clearCache) {
  79.             $this->clearCache($event->getContext());
  80.         }
  81.     }
  82.     private function collectPrimaryKeys(array $primaryKeys): array
  83.     {
  84.         $ids = [];
  85.         foreach ($primaryKeys as $key) {
  86.             if (is_array($key)) {
  87.                 $ids array_merge($idsarray_values($key));
  88.             } else {
  89.                 $ids[] = $key;
  90.             }
  91.         }
  92.         return $ids;
  93.     }
  94.     private function collectActivePayonePaymentMethods(SalesChannelContext $context): array
  95.     {
  96.         $criteria = new Criteria();
  97.         $criteria->addFilter(new ContainsFilter('handlerIdentifier''PayonePayment'));
  98.         $criteria->addFilter(new EqualsFilter('active'true));
  99.         return $this->paymentMethodRepository->search($criteria$context)->getIds();
  100.     }
  101.     private function generateCacheKey(string $salesChannel): string
  102.     {
  103.         return 'payone_payment.menu_state.' $salesChannel;
  104.     }
  105.     private function clearCache(Context $context): void
  106.     {
  107.         $cacheKeys = [];
  108.         /** @var string[] $salesChannels */
  109.         $salesChannels $this->salesChannelRepository->searchIds(new Criteria(), $context)->getIds();
  110.         foreach ($salesChannels as $salesChannel) {
  111.             $cacheKeys[] = $this->generateCacheKey($salesChannel);
  112.         }
  113.         if (empty($cacheKeys)) {
  114.             return;
  115.         }
  116.         $this->cachePool->deleteItems($cacheKeys);
  117.     }
  118. }