DataMapperResolver.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model\Adapter\DataMapper;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Elasticsearch\Model\Adapter\DataMapperInterface;
  9. use Magento\Elasticsearch\Model\Config;
  10. /**
  11. * @deprecated 100.2.0
  12. * @see \Magento\Elasticsearch\Model\Adapter\BatchDataMapperInterface
  13. */
  14. class DataMapperResolver implements DataMapperInterface
  15. {
  16. /**
  17. * Object Manager instance
  18. *
  19. * @var ObjectManagerInterface
  20. */
  21. private $objectManager;
  22. /**
  23. * @var string[]
  24. */
  25. private $dataMappers;
  26. /**
  27. * Data Mapper instance
  28. *
  29. * @var DataMapperInterface
  30. */
  31. private $dataMapperEntity;
  32. /**
  33. * @param ObjectManagerInterface $objectManager
  34. * @param string[] $dataMappers
  35. */
  36. public function __construct(
  37. ObjectManagerInterface $objectManager,
  38. array $dataMappers = []
  39. ) {
  40. $this->objectManager = $objectManager;
  41. $this->dataMappers = $dataMappers;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function map(
  47. $entityId,
  48. array $entityIndexData,
  49. $storeId,
  50. $context = []
  51. ) {
  52. $entityType = isset($context['entityType']) ? $context['entityType'] : Config::ELASTICSEARCH_TYPE_DEFAULT;
  53. return $this->getEntity($entityType)->map($entityId, $entityIndexData, $storeId, $context);
  54. }
  55. /**
  56. * Get instance of current data mapper
  57. *
  58. * @param string $entityType
  59. * @return DataMapperInterface
  60. * @throws \Exception
  61. */
  62. private function getEntity($entityType = '')
  63. {
  64. if (empty($this->dataMapperEntity)) {
  65. if (empty($entityType)) {
  66. throw new \Exception(
  67. 'No entity type given'
  68. );
  69. }
  70. if (!isset($this->dataMappers[$entityType])) {
  71. throw new \LogicException(
  72. 'There is no such data mapper: ' . $entityType
  73. );
  74. }
  75. $dataMapperClass = $this->dataMappers[$entityType];
  76. $this->dataMapperEntity = $this->objectManager->create($dataMapperClass);
  77. if (!($this->dataMapperEntity instanceof DataMapperInterface)) {
  78. throw new \InvalidArgumentException(
  79. 'Data mapper must implement \Magento\Elasticsearch\Model\Adapter\DataMapperInterface'
  80. );
  81. }
  82. }
  83. return $this->dataMapperEntity;
  84. }
  85. }