AdapterFactory.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Model;
  7. use Magento\Framework\Search\EngineResolverInterface;
  8. /**
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class AdapterFactory
  13. {
  14. /**
  15. * Scope configuration
  16. *
  17. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  18. * @deprecated 101.0.0 since it is not used anymore
  19. */
  20. protected $scopeConfig;
  21. /**
  22. * Object Manager instance
  23. *
  24. * @var \Magento\Framework\ObjectManagerInterface
  25. */
  26. protected $objectManager;
  27. /**
  28. * Config path
  29. *
  30. * @var string
  31. * @deprecated 101.0.0 since it is not used anymore
  32. */
  33. protected $path;
  34. /**
  35. * Config Scope
  36. * @deprecated 101.0.0 since it is not used anymore
  37. */
  38. protected $scope;
  39. /**
  40. * Pool of existing adapters
  41. *
  42. * @var array
  43. */
  44. private $adapterPool;
  45. /**
  46. * @var EngineResolverInterface
  47. */
  48. private $engineResolver;
  49. /**
  50. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  51. * @param array $adapters
  52. * @param EngineResolverInterface $engineResolver
  53. */
  54. public function __construct(
  55. \Magento\Framework\ObjectManagerInterface $objectManager,
  56. array $adapters,
  57. EngineResolverInterface $engineResolver
  58. ) {
  59. $this->objectManager = $objectManager;
  60. $this->adapterPool = $adapters;
  61. $this->engineResolver = $engineResolver;
  62. }
  63. /**
  64. * Create Adapter instance
  65. *
  66. * @param array $data
  67. * @return \Magento\Framework\Search\AdapterInterface
  68. */
  69. public function create(array $data = [])
  70. {
  71. $currentAdapter = $this->engineResolver->getCurrentSearchEngine();
  72. if (!isset($this->adapterPool[$currentAdapter])) {
  73. throw new \LogicException(
  74. 'There is no such adapter: ' . $currentAdapter
  75. );
  76. }
  77. $adapterClass = $this->adapterPool[$currentAdapter];
  78. $adapter = $this->objectManager->create($adapterClass, $data);
  79. if (!($adapter instanceof \Magento\Framework\Search\AdapterInterface)) {
  80. throw new \InvalidArgumentException(
  81. 'Adapter must implement \Magento\Framework\Search\AdapterInterface'
  82. );
  83. }
  84. return $adapter;
  85. }
  86. }