ReadHandler.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Model\Product;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\ConfigurableProduct\Helper\Product\Options\Loader;
  9. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  10. use Magento\Framework\EntityManager\Operation\ExtensionInterface;
  11. /**
  12. * Class ReadHandler
  13. */
  14. class ReadHandler implements ExtensionInterface
  15. {
  16. /**
  17. * @var Loader
  18. */
  19. private $optionLoader;
  20. /**
  21. * ReadHandler constructor
  22. *
  23. * @param Loader $optionLoader
  24. */
  25. public function __construct(Loader $optionLoader)
  26. {
  27. $this->optionLoader = $optionLoader;
  28. }
  29. /**
  30. * @param object $entity
  31. * @param array $arguments
  32. * @return object
  33. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  34. */
  35. public function execute($entity, $arguments = [])
  36. {
  37. if ($entity->getTypeId() !== Configurable::TYPE_CODE) {
  38. return $entity;
  39. }
  40. $extensionAttributes = $entity->getExtensionAttributes();
  41. $extensionAttributes->setConfigurableProductLinks($this->getLinkedProducts($entity));
  42. $extensionAttributes->setConfigurableProductOptions($this->optionLoader->load($entity));
  43. $entity->setExtensionAttributes($extensionAttributes);
  44. return $entity;
  45. }
  46. /**
  47. * Get linked to configurable simple products
  48. *
  49. * @param ProductInterface $product
  50. * @return int[]
  51. */
  52. private function getLinkedProducts(ProductInterface $product)
  53. {
  54. /** @var Configurable $typeInstance */
  55. $typeInstance = $product->getTypeInstance();
  56. $childrenIds = $typeInstance->getChildrenIds($product->getId());
  57. if (isset($childrenIds[0])) {
  58. return $childrenIds[0];
  59. } else {
  60. return [];
  61. }
  62. }
  63. }