ClientResolver.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdvancedSearch\Model\Client;
  7. use \Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\Search\EngineResolverInterface;
  9. /**
  10. * @api
  11. * @since 100.1.0
  12. */
  13. class ClientResolver
  14. {
  15. /**
  16. * Scope configuration
  17. *
  18. * @var ScopeConfigInterface
  19. * @since 100.1.0
  20. * @deprecated 100.3.0 since it is not used anymore
  21. */
  22. protected $scopeConfig;
  23. /**
  24. * Object Manager instance
  25. *
  26. * @var ObjectManagerInterface
  27. * @since 100.1.0
  28. */
  29. protected $objectManager;
  30. /**
  31. * Pool of existing client factories
  32. *
  33. * @var array
  34. */
  35. private $clientFactoryPool;
  36. /**
  37. * Pool of client option classes
  38. *
  39. * @var array
  40. */
  41. private $clientOptionsPool;
  42. /**
  43. * @var EngineResolver
  44. */
  45. private $engineResolver;
  46. /**
  47. * Config path
  48. *
  49. * @var string
  50. * @since 100.1.0
  51. * @deprecated 100.3.0 since it is not used anymore
  52. */
  53. protected $path;
  54. /**
  55. * Config Scope
  56. * @since 100.1.0
  57. * @deprecated 100.3.0 since it is not used anymore
  58. */
  59. protected $scope;
  60. /**
  61. * @param ObjectManagerInterface $objectManager
  62. * @param array $clientFactories
  63. * @param array $clientOptions
  64. * @param EngineResolverInterface $engineResolver
  65. */
  66. public function __construct(
  67. ObjectManagerInterface $objectManager,
  68. array $clientFactories,
  69. array $clientOptions,
  70. EngineResolverInterface $engineResolver
  71. ) {
  72. $this->objectManager = $objectManager;
  73. $this->clientFactoryPool = $clientFactories;
  74. $this->clientOptionsPool = $clientOptions;
  75. $this->engineResolver = $engineResolver;
  76. }
  77. /**
  78. * Returns configured search engine
  79. *
  80. * @return string
  81. * @since 100.1.0
  82. */
  83. public function getCurrentEngine()
  84. {
  85. return $this->engineResolver->getCurrentSearchEngine();
  86. }
  87. /**
  88. * Create client instance
  89. *
  90. * @param string $engine
  91. * @param array $data
  92. * @return ClientInterface
  93. * @since 100.1.0
  94. */
  95. public function create($engine = '', array $data = [])
  96. {
  97. $engine = $engine ?: $this->getCurrentEngine();
  98. if (!isset($this->clientFactoryPool[$engine])) {
  99. throw new \LogicException(
  100. 'There is no such client factory: ' . $engine
  101. );
  102. }
  103. $factoryClass = $this->clientFactoryPool[$engine];
  104. $factory = $this->objectManager->create($factoryClass);
  105. if (!($factory instanceof ClientFactoryInterface)) {
  106. throw new \InvalidArgumentException(
  107. 'Client factory must implement \Magento\AdvancedSearch\Model\Client\ClientFactoryInterface'
  108. );
  109. }
  110. $optionsClass = $this->clientOptionsPool[$engine];
  111. $clientOptions = $this->objectManager->create($optionsClass);
  112. if (!($clientOptions instanceof ClientOptionsInterface)) {
  113. throw new \InvalidArgumentException(
  114. 'Client options must implement \Magento\AdvancedSearch\Model\Client\ClientInterface'
  115. );
  116. }
  117. $client = $factory->create($clientOptions->prepareClientOptions($data));
  118. return $client;
  119. }
  120. }