AttributeManagement.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\Framework\App\ObjectManager;
  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 AttributeManagement implements \Magento\Eav\Api\AttributeManagementInterface
  15. {
  16. /**
  17. * @var \Magento\Eav\Api\AttributeSetRepositoryInterface
  18. */
  19. protected $setRepository;
  20. /**
  21. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  22. * @deprecated 100.2.0 please use instead \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory
  23. * @see $attributeCollectionFactory
  24. */
  25. protected $attributeCollection;
  26. /**
  27. * @var \Magento\Eav\Model\Config
  28. */
  29. protected $eavConfig;
  30. /**
  31. * @var \Magento\Eav\Model\ConfigFactory
  32. */
  33. protected $entityTypeFactory;
  34. /**
  35. * @var \Magento\Eav\Api\AttributeGroupRepositoryInterface
  36. */
  37. protected $groupRepository;
  38. /**
  39. * @var AttributeRepository
  40. */
  41. protected $attributeRepository;
  42. /**
  43. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
  44. */
  45. protected $attributeResource;
  46. /**
  47. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory
  48. */
  49. private $attributeCollectionFactory;
  50. /**
  51. * Constructor
  52. *
  53. * @param \Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository
  54. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection
  55. * @param Config $eavConfig
  56. * @param ConfigFactory $entityTypeFactory
  57. * @param \Magento\Eav\Api\AttributeGroupRepositoryInterface $groupRepository
  58. * @param \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository
  59. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute $attributeResource
  60. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory|null $attributeCollectionFactory
  61. */
  62. public function __construct(
  63. \Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository,
  64. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection,
  65. \Magento\Eav\Model\Config $eavConfig,
  66. \Magento\Eav\Model\ConfigFactory $entityTypeFactory,
  67. \Magento\Eav\Api\AttributeGroupRepositoryInterface $groupRepository,
  68. \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository,
  69. \Magento\Eav\Model\ResourceModel\Entity\Attribute $attributeResource,
  70. \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory $attributeCollectionFactory = null
  71. ) {
  72. $this->setRepository = $setRepository;
  73. $this->attributeCollection = $attributeCollection;
  74. $this->eavConfig = $eavConfig;
  75. $this->entityTypeFactory = $entityTypeFactory;
  76. $this->groupRepository = $groupRepository;
  77. $this->attributeRepository = $attributeRepository;
  78. $this->attributeResource = $attributeResource;
  79. $this->attributeCollectionFactory = $attributeCollectionFactory ?: ObjectManager::getInstance()
  80. ->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory::class);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function assign($entityTypeCode, $attributeSetId, $attributeGroupId, $attributeCode, $sortOrder)
  86. {
  87. try {
  88. $attributeSet = $this->setRepository->get($attributeSetId);
  89. } catch (NoSuchEntityException $ex) {
  90. throw new NoSuchEntityException(
  91. __(
  92. 'The AttributeSet with a "%1" ID doesn\'t exist. Verify the attributeSet and try again.',
  93. $attributeSetId
  94. )
  95. );
  96. }
  97. $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
  98. if ($setEntityType->getEntityTypeCode() != $entityTypeCode) {
  99. throw new InputException(__('The attribute set ID is incorrect. Verify the ID and try again.'));
  100. }
  101. //Check if group exists. If not - expected exception
  102. $attributeGroup = $this->groupRepository->get($attributeGroupId);
  103. if ($attributeGroup->getAttributeSetId() != $attributeSetId) {
  104. throw new InputException(__('The attribute group doesn\'t belong to the attribute set.'));
  105. }
  106. /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
  107. $attribute = $this->attributeRepository->get($entityTypeCode, $attributeCode);
  108. $this->attributeResource->saveInSetIncluding(
  109. $attribute,
  110. $attribute->getAttributeId(),
  111. $attributeSetId,
  112. $attributeGroupId,
  113. $sortOrder
  114. );
  115. $attribute->setAttributeSetId($attributeSetId);
  116. return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function unassign($attributeSetId, $attributeCode)
  122. {
  123. try {
  124. $attributeSet = $this->setRepository->get($attributeSetId);
  125. } catch (NoSuchEntityException $e) {
  126. throw new NoSuchEntityException(
  127. __('The "%1" attribute set wasn\'t found. Verify and try again.', $attributeSetId)
  128. );
  129. }
  130. $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
  131. /** @var \Magento\Eav\Model\Entity\Attribute $attribute */
  132. $attribute = $this->attributeRepository->get($setEntityType->getEntityTypeCode(), $attributeCode);
  133. // Check if attribute is in set
  134. $attribute->setAttributeSetId($attributeSet->getAttributeSetId());
  135. $attribute->loadEntityAttributeIdBySet();
  136. if (!$attribute->getEntityAttributeId()) {
  137. throw new InputException(
  138. __(
  139. 'The "%1" attribute wasn\'t found in the "%2" attribute set. Enter the attribute and try again.',
  140. $attributeCode,
  141. $attributeSetId
  142. )
  143. );
  144. }
  145. if (!$attribute->getIsUserDefined()) {
  146. throw new StateException(__("The system attribute can't be deleted."));
  147. }
  148. $attribute->deleteEntity();
  149. return true;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getAttributes($entityType, $attributeSetId)
  155. {
  156. /** @var \Magento\Eav\Api\Data\AttributeSetInterface $attributeSet */
  157. $attributeSet = $this->setRepository->get($attributeSetId);
  158. $requiredEntityTypeId = $this->eavConfig->getEntityType($entityType)->getId();
  159. if (!$attributeSet->getAttributeSetId() || $attributeSet->getEntityTypeId() != $requiredEntityTypeId) {
  160. throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
  161. }
  162. $attributeCollection = $this->attributeCollectionFactory->create();
  163. $attributeCollection->setAttributeSetFilter($attributeSet->getAttributeSetId())->load();
  164. return $attributeCollection->getItems();
  165. }
  166. }