UpdateAttributes.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager\Operation\Update;
  7. use Magento\Framework\EntityManager\TypeResolver;
  8. use Magento\Framework\EntityManager\HydratorPool;
  9. use Magento\Framework\EntityManager\Operation\AttributePool;
  10. /**
  11. * Class UpdateAttributes
  12. */
  13. class UpdateAttributes
  14. {
  15. /**
  16. * @var TypeResolver
  17. */
  18. private $typeResolver;
  19. /**
  20. * @var HydratorPool
  21. */
  22. private $hydratorPool;
  23. /**
  24. * @var AttributePool
  25. */
  26. private $attributePool;
  27. /**
  28. * @param TypeResolver $typeResolver
  29. * @param HydratorPool $hydratorPool
  30. * @param AttributePool $attributePool
  31. */
  32. public function __construct(
  33. TypeResolver $typeResolver,
  34. HydratorPool $hydratorPool,
  35. AttributePool $attributePool
  36. ) {
  37. $this->typeResolver = $typeResolver;
  38. $this->hydratorPool = $hydratorPool;
  39. $this->attributePool = $attributePool;
  40. }
  41. /**
  42. * @param object $entity
  43. * @param array $arguments
  44. * @return object
  45. */
  46. public function execute($entity, $arguments = [])
  47. {
  48. $entityType = $this->typeResolver->resolve($entity);
  49. $hydrator = $this->hydratorPool->getHydrator($entityType);
  50. $entityData = array_merge($hydrator->extract($entity), $arguments);
  51. $actions = $this->attributePool->getActions($entityType, 'update');
  52. foreach ($actions as $action) {
  53. $entityData = $action->execute($entityType, $entityData, $arguments);
  54. }
  55. $entity = $hydrator->hydrate($entity, $entityData);
  56. return $entity;
  57. }
  58. }