Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateHandler.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository;
14 
21 {
25  private $attributeRepository;
26 
30  private $metadataPool;
31 
35  private $searchCriteriaBuilder;
36 
40  private $attributePersistor;
41 
45  private $readSnapshot;
46 
50  private $scopeResolver;
51 
55  private $readHandler;
56 
60  private $attributeLoader;
61 
72  public function __construct(
73  AttributeRepository $attributeRepository,
74  MetadataPool $metadataPool,
75  SearchCriteriaBuilder $searchCriteriaBuilder,
76  AttributePersistor $attributePersistor,
77  ReadSnapshot $readSnapshot,
78  ScopeResolver $scopeResolver,
79  AttributeLoader $attributeLoader = null
80  ) {
81  $this->attributeRepository = $attributeRepository;
82  $this->metadataPool = $metadataPool;
83  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
84  $this->attributePersistor = $attributePersistor;
85  $this->readSnapshot = $readSnapshot;
86  $this->scopeResolver = $scopeResolver;
87  $this->attributeLoader = $attributeLoader ?: ObjectManager::getInstance()->get(AttributeLoader::class);
88  }
89 
95  protected function getAttributes($entityType, $attributeSetId = null)
96  {
97  return $this->attributeLoader->getAttributes($entityType, $attributeSetId);
98  }
99 
111  public function execute($entityType, $entityData, $arguments = [])
112  {
114  $metadata = $this->metadataPool->getMetadata($entityType);
115  if ($metadata->getEavEntityType()) {
116  $context = $this->scopeResolver->getEntityContext($entityType, $entityData);
117  $entityDataForSnapshot = [$metadata->getLinkField() => $entityData[$metadata->getLinkField()]];
118  foreach ($context as $scope) {
119  if (isset($entityData[$scope->getIdentifier()])) {
120  $entityDataForSnapshot[$scope->getIdentifier()] = $entityData[$scope->getIdentifier()];
121  }
122  }
124  ? $entityData[AttributeLoader::ATTRIBUTE_SET_ID]
125  : null; // @todo verify is it normal to not have attribute_set_id
126  if (!isset($entityDataForSnapshot['attribute_set_id'])) {
127  $entityDataForSnapshot['attribute_set_id'] = $attributeSetId;
128  }
129  $snapshot = $this->readSnapshot->execute($entityType, $entityDataForSnapshot);
130  foreach ($this->getAttributes($entityType, $attributeSetId) as $attribute) {
131  $code = $attribute->getAttributeCode();
132  $isAllowedValueType = array_key_exists($code, $entityData)
133  && (is_scalar($entityData[$code]) || $entityData[$code] === null);
134 
135  if ($attribute->isStatic() || !$isAllowedValueType) {
136  continue;
137  }
138 
139  $newValue = $entityData[$code];
140  $isValueEmpty = $attribute->isValueEmpty($newValue);
141  $isAllowedEmptyStringValue = $attribute->isAllowedEmptyTextValue($newValue);
142 
143  if (array_key_exists($code, $snapshot)) {
144  $snapshotValue = $snapshot[$code];
148  if ($snapshotValue === false) {
149  continue;
150  }
151 
152  if (!$isValueEmpty || $isAllowedEmptyStringValue) {
156  if ($snapshotValue === $newValue) {
157  continue;
158  }
159 
160  $this->attributePersistor->registerUpdate(
161  $entityType,
162  $entityData[$metadata->getLinkField()],
163  $code,
164  $newValue
165  );
166  } else {
167  $this->attributePersistor->registerDelete(
168  $entityType,
169  $entityData[$metadata->getLinkField()],
170  $code
171  );
172  }
173  } else {
177  if (!$isValueEmpty || $isAllowedEmptyStringValue) {
178  $this->attributePersistor->registerInsert(
179  $entityType,
180  $entityData[$metadata->getLinkField()],
181  $code,
182  $newValue
183  );
184  }
185  }
186  }
187  $this->attributePersistor->flush($entityType, $context);
188  }
189 
190  return $this->getReadHandler()->execute($entityType, $entityData, $arguments);
191  }
192 
200  protected function getReadHandler()
201  {
202  if (!$this->readHandler) {
203  $this->readHandler = ObjectManager::getInstance()->get(ReadHandler::class);
204  }
205 
206  return $this->readHandler;
207  }
208 }
execute($entityType, $entityData, $arguments=[])
__construct(AttributeRepository $attributeRepository, MetadataPool $metadataPool, SearchCriteriaBuilder $searchCriteriaBuilder, AttributePersistor $attributePersistor, ReadSnapshot $readSnapshot, ScopeResolver $scopeResolver, AttributeLoader $attributeLoader=null)
$searchCriteriaBuilder
$arguments
getAttributes($entityType, $attributeSetId=null)
$code
Definition: info.phtml:12