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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Model\Product;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\ConfigurableProduct\Helper\Product\Options\Loader;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
/**
* Class ReadHandler
*/
class ReadHandler implements ExtensionInterface
{
/**
* @var Loader
*/
private $optionLoader;
/**
* ReadHandler constructor
*
* @param Loader $optionLoader
*/
public function __construct(Loader $optionLoader)
{
$this->optionLoader = $optionLoader;
}
/**
* @param object $entity
* @param array $arguments
* @return object
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute($entity, $arguments = [])
{
if ($entity->getTypeId() !== Configurable::TYPE_CODE) {
return $entity;
}
$extensionAttributes = $entity->getExtensionAttributes();
$extensionAttributes->setConfigurableProductLinks($this->getLinkedProducts($entity));
$extensionAttributes->setConfigurableProductOptions($this->optionLoader->load($entity));
$entity->setExtensionAttributes($extensionAttributes);
return $entity;
}
/**
* Get linked to configurable simple products
*
* @param ProductInterface $product
* @return int[]
*/
private function getLinkedProducts(ProductInterface $product)
{
/** @var Configurable $typeInstance */
$typeInstance = $product->getTypeInstance();
$childrenIds = $typeInstance->getChildrenIds($product->getId());
if (isset($childrenIds[0])) {
return $childrenIds[0];
} else {
return [];
}
}
}