AttributeSetRepository.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model;
  7. use Magento\Eav\Api\AttributeSetRepositoryInterface;
  8. use Magento\Eav\Api\Data\AttributeSetInterface;
  9. use Magento\Eav\Model\Config as EavConfig;
  10. use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
  11. use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
  12. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set as AttributeSetResource;
  13. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;
  14. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  15. use Magento\Framework\Exception\CouldNotDeleteException;
  16. use Magento\Framework\Exception\CouldNotSaveException;
  17. use Magento\Framework\Exception\NoSuchEntityException;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class AttributeSetRepository implements AttributeSetRepositoryInterface
  22. {
  23. /**
  24. * @var AttributeSetResource
  25. */
  26. private $attributeSetResource;
  27. /**
  28. * @var AttributeSetFactory
  29. */
  30. private $attributeSetFactory;
  31. /**
  32. * @var CollectionFactory
  33. */
  34. private $collectionFactory;
  35. /**
  36. * @var EavConfig
  37. */
  38. private $eavConfig;
  39. /**
  40. * @var \Magento\Eav\Api\Data\AttributeSetSearchResultsInterfaceFactory
  41. */
  42. private $searchResultsFactory;
  43. /**
  44. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  45. */
  46. protected $joinProcessor;
  47. /**
  48. * @var CollectionProcessorInterface
  49. */
  50. private $collectionProcessor;
  51. /**
  52. * @param AttributeSetResource $attributeSetResource
  53. * @param AttributeSetFactory $attributeSetFactory
  54. * @param CollectionFactory $collectionFactory
  55. * @param Config $eavConfig
  56. * @param \Magento\Eav\Api\Data\AttributeSetSearchResultsInterfaceFactory $searchResultFactory
  57. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
  58. * @param CollectionProcessorInterface $collectionProcessor
  59. * @codeCoverageIgnore
  60. */
  61. public function __construct(
  62. AttributeSetResource $attributeSetResource,
  63. AttributeSetFactory $attributeSetFactory,
  64. CollectionFactory $collectionFactory,
  65. EavConfig $eavConfig,
  66. \Magento\Eav\Api\Data\AttributeSetSearchResultsInterfaceFactory $searchResultFactory,
  67. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
  68. CollectionProcessorInterface $collectionProcessor = null
  69. ) {
  70. $this->attributeSetResource = $attributeSetResource;
  71. $this->attributeSetFactory = $attributeSetFactory;
  72. $this->collectionFactory = $collectionFactory;
  73. $this->eavConfig = $eavConfig;
  74. $this->searchResultsFactory = $searchResultFactory;
  75. $this->joinProcessor = $joinProcessor;
  76. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function save(AttributeSetInterface $attributeSet)
  82. {
  83. try {
  84. $this->attributeSetResource->save($attributeSet);
  85. } catch (\Exception $exception) {
  86. throw new CouldNotSaveException(
  87. __(
  88. 'The attribute set couldn\'t be saved due to an error. '
  89. . 'Verify your information and try again. If the error persists, please try again later.'
  90. )
  91. );
  92. }
  93. return $attributeSet;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  99. {
  100. /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $collection */
  101. $collection = $this->collectionFactory->create();
  102. $this->joinProcessor->process($collection);
  103. $this->collectionProcessor->process($searchCriteria, $collection);
  104. /** @var \Magento\Eav\Api\Data\AttributeSetSearchResultsInterface $searchResults */
  105. $searchResults = $this->searchResultsFactory->create();
  106. $searchResults->setSearchCriteria($searchCriteria);
  107. $searchResults->setItems($collection->getItems());
  108. $searchResults->setTotalCount($collection->getSize());
  109. return $searchResults;
  110. }
  111. /**
  112. * Retrieve entity type code from search criteria
  113. *
  114. * @deprecated 101.0.0
  115. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  116. * @return null|string
  117. */
  118. protected function getEntityTypeCode(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  119. {
  120. foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
  121. foreach ($filterGroup->getFilters() as $filter) {
  122. if ($filter->getField() == 'entity_type_code') {
  123. return $filter->getValue();
  124. }
  125. }
  126. }
  127. return null;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function get($attributeSetId)
  133. {
  134. /** @var AttributeSet $attributeSet */
  135. $attributeSet = $this->attributeSetFactory->create();
  136. $this->attributeSetResource->load($attributeSet, $attributeSetId);
  137. if (!$attributeSet->getId()) {
  138. throw NoSuchEntityException::singleField('id', $attributeSetId);
  139. }
  140. return $attributeSet;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function delete(AttributeSetInterface $attributeSet)
  146. {
  147. try {
  148. $this->attributeSetResource->delete($attributeSet);
  149. } catch (\Magento\Framework\Exception\StateException $exception) {
  150. throw new CouldNotDeleteException(__('The default attribute set can\'t be deleted.'));
  151. } catch (\Exception $exception) {
  152. throw new CouldNotDeleteException(
  153. __(
  154. 'The attribute set couldn\'t be deleted due to an error. '
  155. . 'Try again — if the error persists, please try again later.'
  156. )
  157. );
  158. }
  159. return true;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function deleteById($attributeSetId)
  165. {
  166. return $this->delete($this->get($attributeSetId));
  167. }
  168. /**
  169. * Retrieve collection processor
  170. *
  171. * @deprecated 101.0.0
  172. * @return CollectionProcessorInterface
  173. */
  174. private function getCollectionProcessor()
  175. {
  176. if (!$this->collectionProcessor) {
  177. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  178. 'Magento\Eav\Model\Api\SearchCriteria\AttributeSetCollectionProcessor'
  179. );
  180. }
  181. return $this->collectionProcessor;
  182. }
  183. }