|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ConfigurableProduct\Model\Plugin\Frontend; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\Data\ProductInterfaceFactory; |
| 12 | +use Magento\Catalog\Model\Category; |
| 13 | +use Magento\Catalog\Model\Product; |
| 14 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 15 | +use Magento\Customer\Model\Session; |
| 16 | +use Magento\Framework\Cache\FrontendInterface; |
| 17 | +use Magento\Framework\EntityManager\MetadataPool; |
| 18 | +use Magento\Framework\Serialize\SerializerInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Cache of used products for configurable product |
| 22 | + */ |
| 23 | +class UsedProductsCache |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var MetadataPool |
| 27 | + */ |
| 28 | + private $metadataPool; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var FrontendInterface |
| 32 | + */ |
| 33 | + private $cache; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var SerializerInterface |
| 37 | + */ |
| 38 | + private $serializer; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var ProductInterfaceFactory |
| 42 | + */ |
| 43 | + private $productFactory; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var Session |
| 47 | + */ |
| 48 | + private $customerSession; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param MetadataPool $metadataPool |
| 52 | + * @param FrontendInterface $cache |
| 53 | + * @param SerializerInterface $serializer |
| 54 | + * @param ProductInterfaceFactory $productFactory |
| 55 | + * @param Session $customerSession |
| 56 | + */ |
| 57 | + public function __construct( |
| 58 | + MetadataPool $metadataPool, |
| 59 | + FrontendInterface $cache, |
| 60 | + SerializerInterface $serializer, |
| 61 | + ProductInterfaceFactory $productFactory, |
| 62 | + Session $customerSession |
| 63 | + ) { |
| 64 | + $this->metadataPool = $metadataPool; |
| 65 | + $this->cache = $cache; |
| 66 | + $this->serializer = $serializer; |
| 67 | + $this->productFactory = $productFactory; |
| 68 | + $this->customerSession = $customerSession; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Retrieve used products for configurable product |
| 73 | + * |
| 74 | + * @param Configurable $subject |
| 75 | + * @param callable $proceed |
| 76 | + * @param Product $product |
| 77 | + * @param array|null $requiredAttributeIds |
| 78 | + * @return ProductInterface[] |
| 79 | + */ |
| 80 | + public function aroundGetUsedProducts( |
| 81 | + Configurable $subject, |
| 82 | + callable $proceed, |
| 83 | + $product, |
| 84 | + $requiredAttributeIds = null |
| 85 | + ) { |
| 86 | + $cacheKey = $this->getCacheKey($product, $requiredAttributeIds); |
| 87 | + $usedProducts = $this->readUsedProductsCacheData($cacheKey); |
| 88 | + if ($usedProducts === null) { |
| 89 | + $usedProducts = $proceed($product, $requiredAttributeIds); |
| 90 | + $this->saveUsedProductsCacheData($product, $usedProducts, $cacheKey); |
| 91 | + } |
| 92 | + |
| 93 | + return $usedProducts; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Generate cache key for product |
| 98 | + * |
| 99 | + * @param Product $product |
| 100 | + * @param array|null $requiredAttributeIds |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + private function getCacheKey($product, $requiredAttributeIds = null): string |
| 104 | + { |
| 105 | + $metadata = $this->metadataPool->getMetadata(ProductInterface::class); |
| 106 | + $keyParts = [ |
| 107 | + 'getUsedProducts', |
| 108 | + $product->getData($metadata->getLinkField()), |
| 109 | + $product->getStoreId(), |
| 110 | + $this->customerSession->getCustomerGroupId(), |
| 111 | + ]; |
| 112 | + if ($requiredAttributeIds !== null) { |
| 113 | + sort($requiredAttributeIds); |
| 114 | + $keyParts[] = implode('', $requiredAttributeIds); |
| 115 | + } |
| 116 | + $cacheKey = sha1(implode('_', $keyParts)); |
| 117 | + |
| 118 | + return $cacheKey; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Read used products data from cache |
| 123 | + * |
| 124 | + * Looking for cache record stored under provided $cacheKey |
| 125 | + * In case data exists turns it into array of products |
| 126 | + * |
| 127 | + * @param string $cacheKey |
| 128 | + * @return ProductInterface[]|null |
| 129 | + */ |
| 130 | + private function readUsedProductsCacheData(string $cacheKey): ?array |
| 131 | + { |
| 132 | + $data = $this->cache->load($cacheKey); |
| 133 | + if (!$data) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + $items = $this->serializer->unserialize($data); |
| 138 | + if (!$items) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + $usedProducts = []; |
| 143 | + foreach ($items as $item) { |
| 144 | + /** @var Product $productItem */ |
| 145 | + $productItem = $this->productFactory->create(); |
| 146 | + $productItem->setData($item); |
| 147 | + $usedProducts[] = $productItem; |
| 148 | + } |
| 149 | + |
| 150 | + return $usedProducts; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Save $subProducts to cache record identified with provided $cacheKey |
| 155 | + * |
| 156 | + * Cached data will be tagged with combined list of product tags and data specific tags i.e. 'price' etc. |
| 157 | + * |
| 158 | + * @param Product $product |
| 159 | + * @param ProductInterface[] $subProducts |
| 160 | + * @param string $cacheKey |
| 161 | + * @return bool |
| 162 | + */ |
| 163 | + private function saveUsedProductsCacheData(Product $product, array $subProducts, string $cacheKey): bool |
| 164 | + { |
| 165 | + $metadata = $this->metadataPool->getMetadata(ProductInterface::class); |
| 166 | + $data = $this->serializer->serialize(array_map( |
| 167 | + function ($item) { |
| 168 | + return $item->getData(); |
| 169 | + }, |
| 170 | + $subProducts |
| 171 | + )); |
| 172 | + $tags = array_merge( |
| 173 | + $product->getIdentities(), |
| 174 | + [ |
| 175 | + Category::CACHE_TAG, |
| 176 | + Product::CACHE_TAG, |
| 177 | + 'price', |
| 178 | + Configurable::TYPE_CODE . '_' . $product->getData($metadata->getLinkField()) |
| 179 | + ] |
| 180 | + ); |
| 181 | + $result = $this->cache->save($data, $cacheKey, $tags); |
| 182 | + |
| 183 | + return (bool) $result; |
| 184 | + } |
| 185 | +} |
0 commit comments