Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
15 
22 {
26  const ENGINE_NAME = 'elasticsearch';
27 
28  private const ENGINE_NAME_5 = 'elasticsearch5';
29 
33  const ELASTICSEARCH_TYPE_DOCUMENT = 'document';
34 
38  const ELASTICSEARCH_TYPE_DEFAULT = 'product';
39 
44 
49  protected $scopeConfig;
50 
54  private $prefix;
55 
59  private $clientResolver;
60 
64  private $engineResolver;
65 
74  public function __construct(
76  ClientResolver $clientResolver = null,
77  EngineResolverInterface $engineResolver = null,
78  $prefix = null
79  ) {
80  $this->scopeConfig = $scopeConfig;
81  $this->clientResolver = $clientResolver ?: ObjectManager::getInstance()->get(ClientResolver::class);
82  $this->engineResolver = $engineResolver ?: ObjectManager::getInstance()->get(EngineResolverInterface::class);
83  $this->prefix = $prefix ?: $this->clientResolver->getCurrentEngine();
84  }
85 
91  public function prepareClientOptions($options = [])
92  {
93  $defaultOptions = [
94  'hostname' => $this->getElasticsearchConfigData('server_hostname'),
95  'port' => $this->getElasticsearchConfigData('server_port'),
96  'index' => $this->getElasticsearchConfigData('index_prefix'),
97  'enableAuth' => $this->getElasticsearchConfigData('enable_auth'),
98  'username' => $this->getElasticsearchConfigData('username'),
99  'password' => $this->getElasticsearchConfigData('password'),
100  'timeout' => $this->getElasticsearchConfigData('server_timeout') ? : self::ELASTICSEARCH_DEFAULT_TIMEOUT,
101  ];
102  $options = array_merge($defaultOptions, $options);
103  return $options;
104  }
105 
114  public function getElasticsearchConfigData($field, $storeId = null)
115  {
116  return $this->getSearchConfigData($this->prefix . '_' . $field, $storeId);
117  }
118 
127  public function getSearchConfigData($field, $storeId = null)
128  {
129  $path = 'catalog/search/' . $field;
130  return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
131  }
132 
139  public function isElasticsearchEnabled()
140  {
141  return in_array($this->engineResolver->getCurrentSearchEngine(), [self::ENGINE_NAME, self::ENGINE_NAME_5]);
142  }
143 
150  public function getIndexPrefix()
151  {
152  return $this->getElasticsearchConfigData('index_prefix');
153  }
154 
161  public function getEntityType()
162  {
164  }
165 }
__construct(ScopeConfigInterface $scopeConfig, ClientResolver $clientResolver=null, EngineResolverInterface $engineResolver=null, $prefix=null)
Definition: Config.php:74
getSearchConfigData($field, $storeId=null)
Definition: Config.php:127
getElasticsearchConfigData($field, $storeId=null)
Definition: Config.php:114