GroupRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Attribute;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Framework\Exception\InputException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Framework\Exception\StateException;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class GroupRepository implements \Magento\Eav\Api\AttributeGroupRepositoryInterface
  15. {
  16. /**
  17. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group
  18. */
  19. protected $groupResource;
  20. /**
  21. * @var \Magento\Eav\Model\Entity\Attribute\GroupFactory
  22. */
  23. protected $groupFactory;
  24. /**
  25. * @var \Magento\Eav\Api\AttributeSetRepositoryInterface
  26. */
  27. protected $setRepository;
  28. /**
  29. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
  30. */
  31. protected $groupListFactory;
  32. /**
  33. * @var \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterfaceFactory
  34. */
  35. protected $searchResultsFactory;
  36. /**
  37. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  38. */
  39. protected $joinProcessor;
  40. /**
  41. * @var CollectionProcessorInterface
  42. */
  43. private $collectionProcessor;
  44. /**
  45. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group $groupResource
  46. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupListFactory
  47. * @param \Magento\Eav\Model\Entity\Attribute\GroupFactory $groupFactory
  48. * @param \Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository
  49. * @param \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterfaceFactory $searchResultsFactory
  50. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
  51. * @param CollectionProcessorInterface $collectionProcessor
  52. * @codeCoverageIgnore
  53. */
  54. public function __construct(
  55. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group $groupResource,
  56. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupListFactory,
  57. \Magento\Eav\Model\Entity\Attribute\GroupFactory $groupFactory,
  58. \Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository,
  59. \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterfaceFactory $searchResultsFactory,
  60. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
  61. CollectionProcessorInterface $collectionProcessor = null
  62. ) {
  63. $this->groupResource = $groupResource;
  64. $this->groupListFactory = $groupListFactory;
  65. $this->groupFactory = $groupFactory;
  66. $this->setRepository = $setRepository;
  67. $this->searchResultsFactory = $searchResultsFactory;
  68. $this->joinProcessor = $joinProcessor;
  69. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function save(\Magento\Eav\Api\Data\AttributeGroupInterface $group)
  75. {
  76. try {
  77. $this->setRepository->get($group->getAttributeSetId());
  78. } catch (NoSuchEntityException $ex) {
  79. throw NoSuchEntityException::singleField('attributeSetId', $group->getAttributeSetId());
  80. }
  81. if ($group->getAttributeGroupId()) {
  82. /** @var \Magento\Eav\Model\Entity\Attribute\Group $existingGroup */
  83. $existingGroup = $this->groupFactory->create();
  84. $this->groupResource->load($existingGroup, $group->getAttributeGroupId());
  85. if (!$existingGroup->getId()) {
  86. throw NoSuchEntityException::singleField('attributeGroupId', $existingGroup->getId());
  87. }
  88. if ($existingGroup->getAttributeSetId() != $group->getAttributeSetId()) {
  89. throw new StateException(
  90. __("The attribute group doesn't belong to the provided attribute set.")
  91. );
  92. }
  93. }
  94. try {
  95. $this->groupResource->save($group);
  96. } catch (\Exception $e) {
  97. throw new StateException(__("The attributeGroup can't be saved."));
  98. }
  99. return $group;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  105. {
  106. /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection $collection */
  107. $collection = $this->groupListFactory->create();
  108. $this->joinProcessor->process($collection);
  109. $this->collectionProcessor->process($searchCriteria, $collection);
  110. /** @var \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterface $searchResult */
  111. $searchResult = $this->searchResultsFactory->create();
  112. $searchResult->setSearchCriteria($searchCriteria);
  113. $searchResult->setItems($collection->getItems());
  114. $searchResult->setTotalCount($collection->getSize());
  115. return $searchResult;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function get($groupId)
  121. {
  122. /** @var \Magento\Eav\Model\Entity\Attribute\Group $group */
  123. $group = $this->groupFactory->create();
  124. $this->groupResource->load($group, $groupId);
  125. if (!$group->getId()) {
  126. throw new NoSuchEntityException(
  127. __('The group with the "%1" ID doesn\'t exist. Verify the ID and try again.', $groupId)
  128. );
  129. }
  130. return $group;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function delete(\Magento\Eav\Api\Data\AttributeGroupInterface $group)
  136. {
  137. try {
  138. $this->groupResource->delete($group);
  139. } catch (\Exception $e) {
  140. throw new StateException(
  141. __(
  142. 'The attribute group with id "%1" can\'t be deleted.',
  143. $group->getId()
  144. ),
  145. $e
  146. );
  147. }
  148. return true;
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function deleteById($groupId)
  154. {
  155. $this->delete(
  156. $this->get($groupId)
  157. );
  158. return true;
  159. }
  160. /**
  161. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  162. * @return null|string
  163. * @deprecated 101.0.3
  164. */
  165. protected function retrieveAttributeSetIdFromSearchCriteria(
  166. \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  167. ) {
  168. foreach ($searchCriteria->getFilterGroups() as $group) {
  169. foreach ($group->getFilters() as $filter) {
  170. if ($filter->getField() == 'attribute_set_id') {
  171. return $filter->getValue();
  172. }
  173. }
  174. }
  175. return null;
  176. }
  177. /**
  178. * Retrieve collection processor
  179. *
  180. * @deprecated 101.0.0
  181. * @return CollectionProcessorInterface
  182. */
  183. private function getCollectionProcessor()
  184. {
  185. if (!$this->collectionProcessor) {
  186. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  187. 'Magento\Eav\Model\Api\SearchCriteria\AttributeGroupCollectionProcessor'
  188. );
  189. }
  190. return $this->collectionProcessor;
  191. }
  192. }