Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConnectionManager.php
Go to the documentation of this file.
1 <?php
7 
11 use Psr\Log\LoggerInterface;
12 
18 {
23  protected $client;
24 
29  protected $logger;
30 
35  protected $clientFactory;
36 
41  protected $clientConfig;
42 
48  public function __construct(
51  LoggerInterface $logger
52  ) {
53  $this->logger = $logger;
54  $this->clientFactory = $clientFactory;
55  $this->clientConfig = $clientConfig;
56  }
57 
66  public function getConnection($options = [])
67  {
68  if (!$this->client) {
69  $this->connect($options);
70  }
71 
72  return $this->client;
73  }
74 
82  private function connect($options)
83  {
84  try {
85  $this->client = $this->clientFactory->create($this->clientConfig->prepareClientOptions($options));
86  } catch (\Exception $e) {
87  $this->logger->critical($e);
88  throw new \RuntimeException('Elasticsearch client is not set.');
89  }
90  }
91 }
__construct(ClientFactoryInterface $clientFactory, ClientOptionsInterface $clientConfig, LoggerInterface $logger)