PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/Catalog/Model/Product/Option/Repository.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 249 lines | 145 code | 24 blank | 80 comment | 10 complexity | 869af6937e5ec3b0fc3788038a649fcb MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\Product\Option;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Framework\Exception\CouldNotSaveException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Framework\EntityManager\MetadataPool;
  11. use Magento\Framework\EntityManager\HydratorPool;
  12. /**
  13. * Class Repository
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class Repository implements \Magento\Catalog\Api\ProductCustomOptionRepositoryInterface
  17. {
  18. /**
  19. * @var \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory
  20. */
  21. protected $collectionFactory;
  22. /**
  23. * @var \Magento\Catalog\Model\Product\OptionFactory
  24. */
  25. protected $optionFactory;
  26. /**
  27. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  28. */
  29. protected $productRepository;
  30. /**
  31. * @var \Magento\Catalog\Model\ResourceModel\Product\Option
  32. */
  33. protected $optionResource;
  34. /**
  35. * @var MetadataPool
  36. */
  37. protected $metadataPool;
  38. /**
  39. * @var HydratorPool
  40. */
  41. protected $hydratorPool;
  42. /**
  43. * @var Converter
  44. */
  45. protected $converter;
  46. /**
  47. * Repository constructor.
  48. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  49. * @param \Magento\Catalog\Model\ResourceModel\Product\Option $optionResource
  50. * @param Converter $converter
  51. */
  52. public function __construct(
  53. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  54. \Magento\Catalog\Model\ResourceModel\Product\Option $optionResource,
  55. \Magento\Catalog\Model\Product\Option\Converter $converter
  56. ) {
  57. $this->productRepository = $productRepository;
  58. $this->optionResource = $optionResource;
  59. $this->converter = $converter;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getList($sku)
  65. {
  66. $product = $this->productRepository->get($sku, true);
  67. return $product->getOptions() ?: [];
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getProductOptions(ProductInterface $product, $requiredOnly = false)
  73. {
  74. return $this->getCollectionFactory()->create()->getProductOptions(
  75. $product->getEntityId(),
  76. $product->getStoreId(),
  77. $requiredOnly
  78. );
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function get($sku, $optionId)
  84. {
  85. $product = $this->productRepository->get($sku);
  86. $option = $product->getOptionById($optionId);
  87. if ($option === null) {
  88. throw NoSuchEntityException::singleField('optionId', $optionId);
  89. }
  90. return $option;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function delete(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $entity)
  96. {
  97. $this->optionResource->delete($entity);
  98. return true;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function duplicate(
  104. \Magento\Catalog\Api\Data\ProductInterface $product,
  105. \Magento\Catalog\Api\Data\ProductInterface $duplicate
  106. ) {
  107. $hydrator = $this->getHydratorPool()->getHydrator(ProductInterface::class);
  108. $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
  109. return $this->optionResource->duplicate(
  110. $this->getOptionFactory()->create([]),
  111. $hydrator->extract($product)[$metadata->getLinkField()],
  112. $hydrator->extract($duplicate)[$metadata->getLinkField()]
  113. );
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option)
  119. {
  120. $productSku = $option->getProductSku();
  121. if (!$productSku) {
  122. throw new CouldNotSaveException(__('ProductSku should be specified'));
  123. }
  124. $product = $this->productRepository->get($productSku);
  125. $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
  126. $option->setData('product_id', $product->getData($metadata->getLinkField()));
  127. $option->setOptionId(null);
  128. $option->save();
  129. return $option;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function deleteByIdentifier($sku, $optionId)
  135. {
  136. $product = $this->productRepository->get($sku, true);
  137. $options = $product->getOptions();
  138. $option = $product->getOptionById($optionId);
  139. if ($option === null) {
  140. throw NoSuchEntityException::singleField('optionId', $optionId);
  141. }
  142. unset($options[$optionId]);
  143. try {
  144. $this->delete($option);
  145. if (empty($options)) {
  146. $this->productRepository->save($product);
  147. }
  148. } catch (\Exception $e) {
  149. throw new CouldNotSaveException(__('Could not remove custom option'));
  150. }
  151. return true;
  152. }
  153. /**
  154. * Mark original values for removal if they are absent among new values
  155. *
  156. * @param $newValues array
  157. * @param $originalValues \Magento\Catalog\Model\Product\Option\Value[]
  158. * @return array
  159. */
  160. protected function markRemovedValues($newValues, $originalValues)
  161. {
  162. $existingValuesIds = [];
  163. foreach ($newValues as $newValue) {
  164. if (array_key_exists('option_type_id', $newValue)) {
  165. $existingValuesIds[] = $newValue['option_type_id'];
  166. }
  167. }
  168. /** @var $originalValue \Magento\Catalog\Model\Product\Option\Value */
  169. foreach ($originalValues as $originalValue) {
  170. if (!in_array($originalValue->getData('option_type_id'), $existingValuesIds)) {
  171. $originalValue->setData('is_delete', 1);
  172. $newValues[] = $originalValue->getData();
  173. }
  174. }
  175. return $newValues;
  176. }
  177. /**
  178. * @return \Magento\Catalog\Model\Product\OptionFactory
  179. * @deprecated
  180. */
  181. private function getOptionFactory()
  182. {
  183. if (null === $this->optionFactory) {
  184. $this->optionFactory = \Magento\Framework\App\ObjectManager::getInstance()
  185. ->get(\Magento\Catalog\Model\Product\OptionFactory::class);
  186. }
  187. return $this->optionFactory;
  188. }
  189. /**
  190. * @return \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory
  191. * @deprecated
  192. */
  193. private function getCollectionFactory()
  194. {
  195. if (null === $this->collectionFactory) {
  196. $this->collectionFactory = \Magento\Framework\App\ObjectManager::getInstance()
  197. ->get(\Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory::class);
  198. }
  199. return $this->collectionFactory;
  200. }
  201. /**
  202. * @return \Magento\Framework\EntityManager\MetadataPool
  203. * @deprecated
  204. */
  205. private function getMetadataPool()
  206. {
  207. if (null === $this->metadataPool) {
  208. $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
  209. ->get(\Magento\Framework\EntityManager\MetadataPool::class);
  210. }
  211. return $this->metadataPool;
  212. }
  213. /**
  214. * @return \Magento\Framework\EntityManager\HydratorPool
  215. * @deprecated
  216. */
  217. private function getHydratorPool()
  218. {
  219. if (null === $this->hydratorPool) {
  220. $this->hydratorPool = \Magento\Framework\App\ObjectManager::getInstance()
  221. ->get(\Magento\Framework\EntityManager\HydratorPool::class);
  222. }
  223. return $this->hydratorPool;
  224. }
  225. }