-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathAttributeManagement.php
191 lines (168 loc) · 7.17 KB
/
AttributeManagement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AttributeManagement implements \Magento\Eav\Api\AttributeManagementInterface
{
/**
* @var \Magento\Eav\Api\AttributeSetRepositoryInterface
*/
protected $setRepository;
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
* @deprecated 100.2.0 please use instead \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory
* @see $attributeCollectionFactory
*/
protected $attributeCollection;
/**
* @var \Magento\Eav\Model\Config
*/
protected $eavConfig;
/**
* @var \Magento\Eav\Model\ConfigFactory
*/
protected $entityTypeFactory;
/**
* @var \Magento\Eav\Api\AttributeGroupRepositoryInterface
*/
protected $groupRepository;
/**
* @var AttributeRepository
*/
protected $attributeRepository;
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
*/
protected $attributeResource;
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory
*/
private $attributeCollectionFactory;
/**
* Constructor
*
* @param \Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection
* @param Config $eavConfig
* @param ConfigFactory $entityTypeFactory
* @param \Magento\Eav\Api\AttributeGroupRepositoryInterface $groupRepository
* @param \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute $attributeResource
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory|null $attributeCollectionFactory
*/
public function __construct(
\Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Eav\Model\ConfigFactory $entityTypeFactory,
\Magento\Eav\Api\AttributeGroupRepositoryInterface $groupRepository,
\Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository,
\Magento\Eav\Model\ResourceModel\Entity\Attribute $attributeResource,
?\Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory $attributeCollectionFactory = null
) {
$this->setRepository = $setRepository;
$this->attributeCollection = $attributeCollection;
$this->eavConfig = $eavConfig;
$this->entityTypeFactory = $entityTypeFactory;
$this->groupRepository = $groupRepository;
$this->attributeRepository = $attributeRepository;
$this->attributeResource = $attributeResource;
$this->attributeCollectionFactory = $attributeCollectionFactory ?: ObjectManager::getInstance()
->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory::class);
}
/**
* {@inheritdoc}
*/
public function assign($entityTypeCode, $attributeSetId, $attributeGroupId, $attributeCode, $sortOrder)
{
try {
$attributeSet = $this->setRepository->get($attributeSetId);
} catch (NoSuchEntityException $ex) {
throw new NoSuchEntityException(
__(
'The AttributeSet with a "%1" ID doesn\'t exist. Verify the attributeSet and try again.',
$attributeSetId
)
);
}
$setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
if ($setEntityType->getEntityTypeCode() != $entityTypeCode) {
throw new InputException(__('The attribute set ID is incorrect. Verify the ID and try again.'));
}
//Check if group exists. If not - expected exception
$attributeGroup = $this->groupRepository->get($attributeGroupId);
if ($attributeGroup->getAttributeSetId() != $attributeSetId) {
throw new InputException(__('The attribute group doesn\'t belong to the attribute set.'));
}
/** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
$attribute = $this->attributeRepository->get($entityTypeCode, $attributeCode);
$this->attributeResource->saveInSetIncluding(
$attribute,
$attribute->getAttributeId(),
$attributeSetId,
$attributeGroupId,
$sortOrder
);
$attribute->setAttributeSetId($attributeSetId);
return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
}
/**
* {@inheritdoc}
*/
public function unassign($attributeSetId, $attributeCode)
{
try {
$attributeSet = $this->setRepository->get($attributeSetId);
} catch (NoSuchEntityException $e) {
throw new NoSuchEntityException(
__('The "%1" attribute set wasn\'t found. Verify and try again.', $attributeSetId)
);
}
$setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
$attribute = $this->attributeRepository->get($setEntityType->getEntityTypeCode(), $attributeCode);
// Check if attribute is in set
$attribute->setAttributeSetId($attributeSet->getAttributeSetId());
$attribute->loadEntityAttributeIdBySet();
if (!$attribute->getEntityAttributeId()) {
throw new InputException(
__(
'The "%1" attribute wasn\'t found in the "%2" attribute set. Enter the attribute and try again.',
$attributeCode,
$attributeSetId
)
);
}
if (!$attribute->getIsUserDefined()) {
throw new StateException(__("The system attribute can't be deleted."));
}
$attribute->deleteEntity();
return true;
}
/**
* {@inheritdoc}
*/
public function getAttributes($entityType, $attributeSetId)
{
/** @var \Magento\Eav\Api\Data\AttributeSetInterface $attributeSet */
$attributeSet = $this->setRepository->get($attributeSetId);
$requiredEntityTypeId = $this->eavConfig->getEntityType($entityType)->getId();
if (!$attributeSet->getAttributeSetId() || $attributeSet->getEntityTypeId() != $requiredEntityTypeId) {
throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
}
$attributeCollection = $this->attributeCollectionFactory->create();
$attributeCollection->setAttributeSetFilter($attributeSet->getAttributeSetId())
->addFieldToFilter('entity_attribute.entity_type_id', $requiredEntityTypeId)
->load();
return $attributeCollection->getItems();
}
}