|
| 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\CatalogExport\Model; |
| 9 | + |
| 10 | +use Magento\CatalogExportApi\Api\Data\ProductVariant; |
| 11 | +use Magento\CatalogExportApi\Api\Data\ProductVariantFactory; |
| 12 | +use Magento\CatalogExportApi\Api\ProductVariantRepositoryInterface; |
| 13 | +use Magento\DataExporter\Model\FeedPool; |
| 14 | +use Magento\Framework\App\DeploymentConfig; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Product variant entity repository |
| 19 | + */ |
| 20 | +class ProductVariantRepository implements ProductVariantRepositoryInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Constant value for setting max items in response |
| 24 | + */ |
| 25 | + private const MAX_ITEMS_IN_RESPONSE = 250; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ProductVariantFactory |
| 29 | + */ |
| 30 | + private $productVariantFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var DtoMapper |
| 34 | + */ |
| 35 | + private $dtoMapper; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var DeploymentConfig |
| 39 | + */ |
| 40 | + private $deploymentConfig; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var LoggerInterface |
| 44 | + */ |
| 45 | + private $logger; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var FeedPool |
| 49 | + */ |
| 50 | + private $feedPool; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param FeedPool $feedPool |
| 54 | + * @param ProductVariantFactory $productVariantFactory |
| 55 | + * @param DtoMapper $dtoMapper |
| 56 | + * @param DeploymentConfig $deploymentConfig |
| 57 | + * @param LoggerInterface $logger |
| 58 | + */ |
| 59 | + public function __construct( |
| 60 | + FeedPool $feedPool, |
| 61 | + ProductVariantFactory $productVariantFactory, |
| 62 | + DtoMapper $dtoMapper, |
| 63 | + DeploymentConfig $deploymentConfig, |
| 64 | + LoggerInterface $logger |
| 65 | + ) { |
| 66 | + $this->feedPool = $feedPool; |
| 67 | + $this->dtoMapper = $dtoMapper; |
| 68 | + $this->productVariantFactory = $productVariantFactory; |
| 69 | + $this->deploymentConfig = $deploymentConfig; |
| 70 | + $this->logger = $logger; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @inheritdoc |
| 75 | + */ |
| 76 | + public function get(array $ids): array |
| 77 | + { |
| 78 | + if (count($ids) > $this->getMaxItemsInResponse()) { |
| 79 | + throw new \InvalidArgumentException( |
| 80 | + 'Max items in the response can\'t exceed ' |
| 81 | + . $this->getMaxItemsInResponse() |
| 82 | + . '.' |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + $productsVariants = []; |
| 87 | + $feedData = $this->feedPool->getFeed('variants')->getFeedByIds($ids); |
| 88 | + if (empty($feedData['feed'])) { |
| 89 | + $this->logger->error( |
| 90 | + \sprintf('Cannot find products variants data in catalog feed with ids "%s"', \implode(',', $ids)) |
| 91 | + ); |
| 92 | + return $productsVariants; |
| 93 | + } |
| 94 | + |
| 95 | + foreach ($feedData['feed'] as $feedItem) { |
| 96 | + $productVariant = $this->productVariantFactory->create(); |
| 97 | + $feedItem = $this->cleanUpNullValues($feedItem); |
| 98 | + $this->dtoMapper->populateWithArray( |
| 99 | + $productVariant, |
| 100 | + $feedItem, |
| 101 | + ProductVariant::class |
| 102 | + ); |
| 103 | + $productsVariants[] = $productVariant; |
| 104 | + } |
| 105 | + return $productsVariants; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @inheritdoc |
| 110 | + */ |
| 111 | + public function getByProductIds(array $productIds): array |
| 112 | + { |
| 113 | + if (count($productIds) > $this->getMaxItemsInResponse()) { |
| 114 | + throw new \InvalidArgumentException( |
| 115 | + 'Max items in the response can\'t exceed ' |
| 116 | + . $this->getMaxItemsInResponse() |
| 117 | + . '.' |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + $productsVariants = []; |
| 122 | + $feedData = $this->feedPool->getFeed('variants')->getFeedByProductIds($productIds); |
| 123 | + if (empty($feedData['feed'])) { |
| 124 | + $this->logger->error( |
| 125 | + \sprintf( |
| 126 | + 'Cannot find products variants data in catalog feed with product ids "%s"', |
| 127 | + \implode(',', $productIds) |
| 128 | + ) |
| 129 | + ); |
| 130 | + return $productsVariants; |
| 131 | + } |
| 132 | + |
| 133 | + foreach ($feedData['feed'] as $feedItem) { |
| 134 | + $productVariant = $this->productVariantFactory->create(); |
| 135 | + $feedItem = $this->cleanUpNullValues($feedItem); |
| 136 | + $this->dtoMapper->populateWithArray( |
| 137 | + $productVariant, |
| 138 | + $feedItem, |
| 139 | + ProductVariant::class |
| 140 | + ); |
| 141 | + $productsVariants[] = $productVariant; |
| 142 | + } |
| 143 | + return $productsVariants; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get max items in response |
| 148 | + * |
| 149 | + * @return int |
| 150 | + */ |
| 151 | + private function getMaxItemsInResponse(): int |
| 152 | + { |
| 153 | + try { |
| 154 | + $maxItemsInResponse = (int)$this->deploymentConfig->get('catalog_export/max_items_in_response'); |
| 155 | + } catch (\Exception $e) { |
| 156 | + $this->logger->error( |
| 157 | + \sprintf('Cannot retrieve catalog export max items in response for product variants. ' . $e) |
| 158 | + ); |
| 159 | + return self::MAX_ITEMS_IN_RESPONSE; |
| 160 | + } |
| 161 | + return $maxItemsInResponse ?: self::MAX_ITEMS_IN_RESPONSE; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Unset null values in provided array recursively |
| 166 | + * |
| 167 | + * @param array $array |
| 168 | + * @return array |
| 169 | + */ |
| 170 | + private function cleanUpNullValues(array $array): array |
| 171 | + { |
| 172 | + $result = []; |
| 173 | + foreach ($array as $key => $value) { |
| 174 | + if ($value === null || $value === '') { |
| 175 | + continue; |
| 176 | + } |
| 177 | + |
| 178 | + $result[$key] = is_array($value) ? $this->cleanUpNullValues($value) : $value; |
| 179 | + } |
| 180 | + return $result; |
| 181 | + } |
| 182 | +} |
0 commit comments