DataMapperFactory.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\ObjectManagerInterface;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\Exception\ConfigurationMismatchException;
  10. use Magento\Elasticsearch\Model\Adapter\BatchDataMapperInterface;
  11. /**
  12. * Data mapper factory
  13. */
  14. class DataMapperFactory
  15. {
  16. /**
  17. * Object Manager instance
  18. *
  19. * @var ObjectManagerInterface
  20. */
  21. private $objectManager;
  22. /**
  23. * @var string[]
  24. */
  25. private $dataMappers;
  26. /**
  27. * @param ObjectManagerInterface $objectManager
  28. * @param string[] $dataMappers
  29. */
  30. public function __construct(
  31. ObjectManagerInterface $objectManager,
  32. array $dataMappers = []
  33. ) {
  34. $this->objectManager = $objectManager;
  35. $this->dataMappers = $dataMappers;
  36. }
  37. /**
  38. * Create instance of data mapper for specified entity type
  39. *
  40. * @param string $entityType
  41. * @return BatchDataMapperInterface
  42. * @throws NoSuchEntityException
  43. * @throws ConfigurationMismatchException
  44. */
  45. public function create($entityType)
  46. {
  47. if (!isset($this->dataMappers[$entityType])) {
  48. throw new NoSuchEntityException(
  49. __(
  50. 'There is no such data mapper "%1" for interface %2',
  51. $entityType,
  52. BatchDataMapperInterface::class
  53. )
  54. );
  55. }
  56. $dataMapperClass = $this->dataMappers[$entityType];
  57. $dataMapperEntity = $this->objectManager->create($dataMapperClass);
  58. if (!$dataMapperEntity instanceof BatchDataMapperInterface) {
  59. throw new ConfigurationMismatchException(
  60. __(
  61. 'Data mapper "%1" must implement interface %2',
  62. $dataMapperClass,
  63. BatchDataMapperInterface::class
  64. )
  65. );
  66. }
  67. return $dataMapperEntity;
  68. }
  69. }