DataMapperResolver.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model\Adapter\BatchDataMapper;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Framework\Exception\ConfigurationMismatchException;
  9. use Magento\Elasticsearch\Model\Adapter\BatchDataMapperInterface;
  10. use Magento\Elasticsearch\Model\Config;
  11. /**
  12. * Map index data to search engine metadata
  13. */
  14. class DataMapperResolver implements BatchDataMapperInterface
  15. {
  16. /**
  17. * @var BatchDataMapperInterface
  18. */
  19. private $dataMapperEntity;
  20. /**
  21. * @var DataMapperFactory
  22. */
  23. private $dataMapperFactory;
  24. /**
  25. * @param DataMapperFactory $dataMapperFactory
  26. */
  27. public function __construct(DataMapperFactory $dataMapperFactory)
  28. {
  29. $this->dataMapperFactory = $dataMapperFactory;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function map(array $documentData, $storeId, array $context = [])
  35. {
  36. $entityType = isset($context['entityType']) ? $context['entityType'] : Config::ELASTICSEARCH_TYPE_DEFAULT;
  37. return $this->getDataMapper($entityType)->map($documentData, $storeId, $context);
  38. }
  39. /**
  40. * Get instance of data mapper for specified entity type
  41. *
  42. * @param string $entityType
  43. * @return BatchDataMapperInterface
  44. * @throws NoSuchEntityException
  45. * @throws ConfigurationMismatchException
  46. */
  47. private function getDataMapper($entityType)
  48. {
  49. if (!isset($this->dataMapperEntity[$entityType])) {
  50. $this->dataMapperEntity[$entityType] = $this->dataMapperFactory->create($entityType);
  51. }
  52. return $this->dataMapperEntity[$entityType];
  53. }
  54. }