66
77use Elastic \Elasticsearch \Client ;
88use Elastic \Elasticsearch \ClientBuilder ;
9+ use Elastic \Elasticsearch \Exception \AuthenticationException ;
910use Illuminate \Database \Connection as BaseConnection ;
1011use Illuminate \Support \Str ;
1112use PDPhilip \Elasticsearch \DSL \Bridge ;
1213use PDPhilip \Elasticsearch \DSL \Results ;
1314use RuntimeException ;
1415
1516use function array_replace_recursive ;
17+ use function in_array ;
18+ use function is_array ;
19+ use function strtolower ;
1620
1721/**
1822 * @method bool indexModify(array $settings)
@@ -71,7 +75,7 @@ class Connection extends BaseConnection
7175
7276 protected bool $ rebuild = false ;
7377
74- protected string $ connectionName = ' elasticsearch ' ;
78+ protected string $ connectionName ;
7579
7680 /**
7781 * @var Query\Processor
@@ -81,7 +85,6 @@ class Connection extends BaseConnection
8185 /** {@inheritdoc} */
8286 public function __construct (array $ config )
8387 {
84-
8588 $ this ->connectionName = $ config ['name ' ];
8689
8790 $ this ->config = $ config ;
@@ -136,6 +139,16 @@ public function getIndexPrefix(): ?string
136139 return $ this ->indexPrefix ;
137140 }
138141
142+ /**
143+ * Retrieves information about the client.
144+ *
145+ * @return array An associative array containing the client's information.
146+ */
147+ public function getClientInfo (): array
148+ {
149+ return $ this ->client ->info ()->asArray ();
150+ }
151+
139152 /** {@inheritdoc} */
140153 public function getPostProcessor (): Query \Processor
141154 {
@@ -172,6 +185,7 @@ public function setIndex(string $index): string
172185 return $ this ->getIndex ();
173186 }
174187
188+ /** {@inheritdoc} */
175189 public function table ($ table , $ as = null )
176190 {
177191 $ query = new Query \Builder ($ this , new Query \Processor );
@@ -296,10 +310,14 @@ private function _sanitizeConfig(): void
296310
297311 }
298312
299- //----------------------------------------------------------------------
300- // Connection Builder
301- //----------------------------------------------------------------------
302-
313+ /**
314+ * Builds and configures a connection to the ElasticSearch client based on
315+ * the provided configuration settings.
316+ *
317+ * @return Client The configured ElasticSearch client.
318+ *
319+ * @throws AuthenticationException
320+ */
303321 protected function buildConnection (): Client
304322 {
305323
@@ -329,6 +347,11 @@ protected function buildConnection(): Client
329347 return $ cb ->build ();
330348 }
331349
350+ /**
351+ * Validates the connection configuration based on the specified authentication type.
352+ *
353+ * @throws RuntimeException if the configuration is invalid for the specified authentication type.
354+ */
332355 private function _validateConnection (): void
333356 {
334357 if (! in_array ($ this ->config ['auth_type ' ], self ::VALID_AUTH_TYPES )) {
@@ -339,13 +362,18 @@ private function _validateConnection(): void
339362 throw new RuntimeException ('auth_type of `cloud` requires `cloud_id` to be set ' );
340363 }
341364
342- if ($ this ->config ['auth_type ' ] === 'http ' && ! $ this ->config ['hosts ' ]) {
343- throw new RuntimeException ('auth_type of `http` requires `hosts` to be set ' );
365+ if ($ this ->config ['auth_type ' ] === 'http ' && ( ! $ this ->config ['hosts ' ] || ! is_array ( $ this -> config [ ' hosts ' ])) ) {
366+ throw new RuntimeException ('auth_type of `http` requires `hosts` to be set and be an array ' );
344367 }
345368
346369 }
347370
348- protected function _builderOptions ($ cb )
371+ /**
372+ * Configures and returns the client builder with the provided SSL and retry settings.
373+ *
374+ * @param ClientBuilder $cb The callback builder instance.
375+ */
376+ protected function _builderOptions (ClientBuilder $ cb ): ClientBuilder
349377 {
350378 $ cb ->setSSLVerification ($ this ->sslVerification );
351379 if (isset ($ this ->elasticMetaHeader )) {
0 commit comments