Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataMapperResolver.php
Go to the documentation of this file.
1 <?php
7 
11 
17 {
23  private $objectManager;
24 
28  private $dataMappers;
29 
35  private $dataMapperEntity;
36 
41  public function __construct(
42  ObjectManagerInterface $objectManager,
43  array $dataMappers = []
44  ) {
45  $this->objectManager = $objectManager;
46  $this->dataMappers = $dataMappers;
47  }
48 
52  public function map(
53  $entityId,
54  array $entityIndexData,
55  $storeId,
56  $context = []
57  ) {
58  $entityType = isset($context['entityType']) ? $context['entityType'] : Config::ELASTICSEARCH_TYPE_DEFAULT;
59  return $this->getEntity($entityType)->map($entityId, $entityIndexData, $storeId, $context);
60  }
61 
69  private function getEntity($entityType = '')
70  {
71  if (empty($this->dataMapperEntity)) {
72  if (empty($entityType)) {
73  throw new \Exception(
74  'No entity type given'
75  );
76  }
77  if (!isset($this->dataMappers[$entityType])) {
78  throw new \LogicException(
79  'There is no such data mapper: ' . $entityType
80  );
81  }
82  $dataMapperClass = $this->dataMappers[$entityType];
83  $this->dataMapperEntity = $this->objectManager->create($dataMapperClass);
84  if (!($this->dataMapperEntity instanceof DataMapperInterface)) {
85  throw new \InvalidArgumentException(
86  'Data mapper must implement \Magento\Elasticsearch\Model\Adapter\DataMapperInterface'
87  );
88  }
89  }
90  return $this->dataMapperEntity;
91  }
92 }
__construct(ObjectManagerInterface $objectManager, array $dataMappers=[])
$objectManager
Definition: bootstrap.php:17
map( $entityId, array $entityIndexData, $storeId, $context=[])