Config.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\Search\EngineResolverInterface;
  9. use Magento\Search\Model\EngineResolver;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\AdvancedSearch\Model\Client\ClientOptionsInterface;
  12. use Magento\AdvancedSearch\Model\Client\ClientResolver;
  13. use Magento\Framework\App\ObjectManager;
  14. /**
  15. * Elasticsearch config model
  16. * @api
  17. * @since 100.1.0
  18. */
  19. class Config implements ClientOptionsInterface
  20. {
  21. /**
  22. * Search engine name
  23. */
  24. const ENGINE_NAME = 'elasticsearch';
  25. /**
  26. * Elasticsearch Entity type
  27. */
  28. const ELASTICSEARCH_TYPE_DOCUMENT = 'document';
  29. /**
  30. * Elasticsearch default Entity type
  31. */
  32. const ELASTICSEARCH_TYPE_DEFAULT = 'product';
  33. /**
  34. * Default Elasticsearch server timeout
  35. */
  36. const ELASTICSEARCH_DEFAULT_TIMEOUT = 15;
  37. /**
  38. * @var ScopeConfigInterface
  39. * @since 100.1.0
  40. */
  41. protected $scopeConfig;
  42. /**
  43. * @var string
  44. */
  45. private $prefix;
  46. /**
  47. * @var ClientResolver
  48. */
  49. private $clientResolver;
  50. /**
  51. * @var EngineResolverInterface
  52. */
  53. private $engineResolver;
  54. /**
  55. * Available Elasticsearch engines.
  56. *
  57. * @var array
  58. */
  59. private $engineList;
  60. /**
  61. * @param ScopeConfigInterface $scopeConfig
  62. * @param ClientResolver|null $clientResolver
  63. * @param EngineResolverInterface|null $engineResolver
  64. * @param string|null $prefix
  65. * @param array $engineList
  66. */
  67. public function __construct(
  68. ScopeConfigInterface $scopeConfig,
  69. ClientResolver $clientResolver = null,
  70. EngineResolverInterface $engineResolver = null,
  71. $prefix = null,
  72. $engineList = []
  73. ) {
  74. $this->scopeConfig = $scopeConfig;
  75. $this->clientResolver = $clientResolver ?: ObjectManager::getInstance()->get(ClientResolver::class);
  76. $this->engineResolver = $engineResolver ?: ObjectManager::getInstance()->get(EngineResolverInterface::class);
  77. $this->prefix = $prefix ?: $this->clientResolver->getCurrentEngine();
  78. $this->engineList = $engineList;
  79. }
  80. /**
  81. * @inheritdoc
  82. *
  83. * @since 100.1.0
  84. */
  85. public function prepareClientOptions($options = [])
  86. {
  87. $defaultOptions = [
  88. 'hostname' => $this->getElasticsearchConfigData('server_hostname'),
  89. 'port' => $this->getElasticsearchConfigData('server_port'),
  90. 'index' => $this->getElasticsearchConfigData('index_prefix'),
  91. 'enableAuth' => $this->getElasticsearchConfigData('enable_auth'),
  92. 'username' => $this->getElasticsearchConfigData('username'),
  93. 'password' => $this->getElasticsearchConfigData('password'),
  94. 'timeout' => $this->getElasticsearchConfigData('server_timeout') ? : self::ELASTICSEARCH_DEFAULT_TIMEOUT,
  95. ];
  96. $options = array_merge($defaultOptions, $options);
  97. return $options;
  98. }
  99. /**
  100. * Retrieve information from Elasticsearch search engine configuration
  101. *
  102. * @param string $field
  103. * @param int $storeId
  104. * @return string|int
  105. * @since 100.1.0
  106. */
  107. public function getElasticsearchConfigData($field, $storeId = null)
  108. {
  109. return $this->getSearchConfigData($this->prefix . '_' . $field, $storeId);
  110. }
  111. /**
  112. * Retrieve information from search engine configuration
  113. *
  114. * @param string $field
  115. * @param int|null $storeId
  116. * @return string|int
  117. * @since 100.1.0
  118. */
  119. public function getSearchConfigData($field, $storeId = null)
  120. {
  121. $path = 'catalog/search/' . $field;
  122. return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
  123. }
  124. /**
  125. * Return true if third party search engine is used
  126. *
  127. * @return bool
  128. * @since 100.1.0
  129. */
  130. public function isElasticsearchEnabled()
  131. {
  132. return in_array($this->engineResolver->getCurrentSearchEngine(), $this->engineList);
  133. }
  134. /**
  135. * Get Elasticsearch index prefix
  136. *
  137. * @return string
  138. * @since 100.1.0
  139. */
  140. public function getIndexPrefix()
  141. {
  142. return $this->getElasticsearchConfigData('index_prefix');
  143. }
  144. /**
  145. * Get Elasticsearch entity type
  146. *
  147. * @return string
  148. * @since 100.1.0
  149. */
  150. public function getEntityType()
  151. {
  152. return self::ELASTICSEARCH_TYPE_DOCUMENT;
  153. }
  154. }