custom/plugins/AcrisProductInfoTextCS/src/Components/ProductInfoText/InfoTextGateway.php line 113

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ProductInfoText\Components\ProductInfoText;
  3. use Acris\ProductInfoText\Components\ProductInfoText\Struct\ProductStreamStruct;
  4. use Acris\ProductInfoText\Custom\ProductInfoTextDefinition;
  5. use Acris\ProductInfoText\Subscriber\InfoTextTagSubscriber;
  6. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupDefinition;
  7. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  18. use Shopware\Core\System\Currency\CurrencyDefinition;
  19. use Shopware\Core\System\Language\LanguageDefinition;
  20. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  21. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  22. use Symfony\Contracts\Cache\ItemInterface;
  23. class InfoTextGateway
  24. {
  25.     public const BUILD_NAME 'acris-product-info-text';
  26.     private EntityRepositoryInterface $infoTextRepository;
  27.     private EntityRepositoryInterface $productStreamMappingRepository;
  28.     private TagAwareAdapterInterface $cache;
  29.     public function __construct(
  30.         EntityRepositoryInterface $infoTextRepository,
  31.         EntityRepositoryInterface $productStreamMappingRepository,
  32.         TagAwareAdapterInterface $cache
  33.     ) {
  34.         $this->infoTextRepository $infoTextRepository;
  35.         $this->productStreamMappingRepository $productStreamMappingRepository;
  36.         $this->cache $cache;
  37.     }
  38.     public function getProductDetailInfoText(array $productStreamIdsSalesChannelContext $salesChannelContext): ?EntitySearchResult
  39.     {
  40.         return $this->getCachedValue('detail-'$salesChannelContext$productStreamIds,'displayProductDetail'ProductInfoTextDefinition::DISPLAY_PRODUCT_NO);
  41.     }
  42.     public function getCartDetailInfoText(array $productStreamIdsSalesChannelContext $salesChannelContext): ?EntitySearchResult
  43.     {
  44.         return $this->getCachedValue('cart-'$salesChannelContext$productStreamIds,'displayCheckout'ProductInfoTextDefinition::DISPLAY_CHECKOUT_NOT);
  45.     }
  46.     public function getProductStreamIds(array $productIdsContext $context): array
  47.     {
  48.         $productStreamIds = [];
  49.         $idSearchResult $this->productStreamMappingRepository->searchIds((new Criteria())->addFilter(new EqualsAnyFilter('productId'$productIds)), $context);
  50.         if ($idSearchResult->getTotal() > 0) {
  51.             foreach ($idSearchResult->getIds() as $ids) {
  52.                 if (!empty($ids) && is_array($ids) && array_key_exists('productStreamId'$ids) && !empty($ids['productStreamId'])) {
  53.                     $productStreamIds[] = $ids['productStreamId'];
  54.                 }
  55.             }
  56.         }
  57.         return $productStreamIds;
  58.     }
  59.     public function getAllProductStreamIds(ProductStreamStruct $productStreamStruct, array $productIdsContext $context): void
  60.     {
  61.         $idSearchResult $this->productStreamMappingRepository->searchIds((new Criteria())->addFilter(new EqualsAnyFilter('productId'$productIds)), $context);
  62.         if ($idSearchResult->getTotal() > 0) {
  63.             foreach ($idSearchResult->getIds() as $ids) {
  64.                 if (!empty($ids) && is_array($ids) && array_key_exists('productStreamId'$ids) && !empty($ids['productStreamId']) && array_key_exists('productId'$ids) && !empty($ids['productId'])) {
  65.                     $productStreamStruct->addProductStreamId($ids['productId'], $ids['productStreamId']);
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     private function getCachedValue(string $keyPrefixSalesChannelContext $salesChannelContext, array $productStreamIdsstring $displayTypeNamestring $displayTypeValue): ?EntitySearchResult
  71.     {
  72.         if(empty($productStreamIds)) {
  73.             return null;
  74.         }
  75.         $salesChannelId $salesChannelContext->getSalesChannelId();
  76.         $ruleIds $salesChannelContext->getRuleIds();
  77.         $key $this->getCacheKey($keyPrefix$salesChannelContext$productStreamIds$ruleIds);
  78.         $value $this->cache->get($key, function (ItemInterface $item) use ($productStreamIds$salesChannelContext$salesChannelId$ruleIds$displayTypeName$displayTypeValue) {
  79.             $infoTexts $this->infoTextRepository->search((new Criteria())
  80.                 ->addSorting(new FieldSorting('priority',FieldSorting::DESCENDINGtrue))->addFilter(
  81.                     new MultiFilter(MultiFilter::CONNECTION_AND, [
  82.                         new EqualsFilter('active'true),
  83.                         new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter($displayTypeName$displayTypeValue)]),
  84.                         new OrFilter([new EqualsFilter('rules.id'null), new EqualsAnyFilter('rules.id'$ruleIds)]),
  85.                         new EqualsFilter('salesChannels.id'$salesChannelId),
  86.                         new EqualsAnyFilter('productStream.id'$productStreamIds),
  87.                         new NotFilter(NotFilter::CONNECTION_AND, [
  88.                             new EqualsFilter('productStream.id'null)
  89.                         ])
  90.                     ])
  91.                 )->addAssociation('productStream')->addAssociation('salesChannels')->addAssociation('rules'), $salesChannelContext->getContext());
  92.             $item->tag([InfoTextTagSubscriber::ACRIS_INFO_TEXT_ENTITY]);
  93.             $this->addCacheTagsForSalesChannelContext($item$salesChannelContext);
  94.             return CacheValueCompressor::compress($infoTexts);
  95.         });
  96.         return CacheValueCompressor::uncompress($value);
  97.     }
  98.     private function getCacheKey(string $prefixSalesChannelContext $salesChannelContext, array $productStreamIds, array $ruleIds): string
  99.     {
  100.         return self::BUILD_NAME '-' md5(\json_encode([
  101.             $prefix,
  102.             $salesChannelContext->getSalesChannel()->getId(),
  103.             $salesChannelContext->getCurrentCustomerGroup()->getId(),
  104.             $salesChannelContext->getCurrency()->getId(),
  105.             $salesChannelContext->getContext()->getLanguageId(),
  106.             $ruleIds,
  107.             $productStreamIds
  108.         ]));
  109.     }
  110.     private function addCacheTagsForSalesChannelContext(ItemInterface $itemSalesChannelContext $salesChannelContext)
  111.     {
  112.         $item->tag(CustomerGroupDefinition::ENTITY_NAME '-' $salesChannelContext->getCurrentCustomerGroup()->getId());
  113.         $item->tag(CurrencyDefinition::ENTITY_NAME '-' $salesChannelContext->getCurrency()->getId());
  114.         $item->tag(LanguageDefinition::ENTITY_NAME '-' $salesChannelContext->getContext()->getLanguageId());
  115.     }
  116. }