IndexStructureFactory.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\Indexer;
  7. use Magento\Framework\Indexer\IndexStructureInterface;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\Framework\Search\EngineResolverInterface;
  10. /**
  11. * Index structure factory
  12. *
  13. * @api
  14. * @since 100.1.0
  15. */
  16. class IndexStructureFactory
  17. {
  18. /**
  19. * Object Manager instance
  20. *
  21. * @var ObjectManagerInterface
  22. * @since 100.1.0
  23. */
  24. protected $objectManager = null;
  25. /**
  26. * Instance name to create
  27. *
  28. * @var string
  29. * @since 100.1.0
  30. */
  31. protected $structures = null;
  32. /**
  33. * @var EngineResolverInterface
  34. */
  35. private $engineResolver;
  36. /**
  37. * Factory constructor
  38. *
  39. * @param ObjectManagerInterface $objectManager
  40. * @param EngineResolverInterface $engineResolver
  41. * @param string[] $structures
  42. */
  43. public function __construct(
  44. ObjectManagerInterface $objectManager,
  45. EngineResolverInterface $engineResolver,
  46. array $structures = []
  47. ) {
  48. $this->objectManager = $objectManager;
  49. $this->structures = $structures;
  50. $this->engineResolver = $engineResolver;
  51. }
  52. /**
  53. * Create index structure
  54. *
  55. * @param array $data
  56. * @return IndexStructureInterface
  57. * @since 100.1.0
  58. */
  59. public function create(array $data = [])
  60. {
  61. $currentStructure = $this->engineResolver->getCurrentSearchEngine();
  62. if (!isset($this->structures[$currentStructure])) {
  63. throw new \LogicException(
  64. 'There is no such index structure: ' . $currentStructure
  65. );
  66. }
  67. $indexStructure = $this->objectManager->create($this->structures[$currentStructure], $data);
  68. if (!$indexStructure instanceof IndexStructureInterface) {
  69. throw new \InvalidArgumentException(
  70. $currentStructure . ' index structure doesn\'t implement '. IndexStructureInterface::class
  71. );
  72. }
  73. return $indexStructure;
  74. }
  75. }