Skip to content

Commit 8a1967e

Browse files
committed
feat(connection): add client info retrieval and improved error handling
- Introduced `getClientInfo` method to retrieve Elasticsearch client information. - Added `AuthenticationException` for better error handling during connection build. - Configured connection name dynamically instead of hardcoding. - Enhanced docstrings for connection validation and builder options. - Implemented new tests for prefix handling and client info retrieval. - Refactored existing tests to use `expect` instead of `$this->assertInstanceOf`. Who needs a crystal ball when you have comprehensive client info? 🔮
1 parent 9c5c0d3 commit 8a1967e

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

src/Connection.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
use Elastic\Elasticsearch\Client;
88
use Elastic\Elasticsearch\ClientBuilder;
9+
use Elastic\Elasticsearch\Exception\AuthenticationException;
910
use Illuminate\Database\Connection as BaseConnection;
1011
use Illuminate\Support\Str;
1112
use PDPhilip\Elasticsearch\DSL\Bridge;
1213
use PDPhilip\Elasticsearch\DSL\Results;
1314
use RuntimeException;
1415

1516
use 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)) {

tests/ConnectionTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
$connection->disconnect();
3636
$client = $connection->getClient();
37-
$this->assertNull($client);
37+
expect($client)->toBeNull();
3838
DB::purge('elasticsearch');
3939

4040
$connection = DB::connection('elasticsearch');
@@ -46,28 +46,37 @@
4646

4747
test('DB', function () {
4848
$connection = DB::connection('elasticsearch');
49-
$this->assertInstanceOf(Client::class, $connection->getClient());
49+
expect($connection->getClient())->toBeInstanceOf(Client::class);
5050
});
5151

5252
test('Connection Without auth_type', function () {
53-
$this->expectException(RuntimeException::class);
54-
$this->expectExceptionMessage('Invalid [auth_type] in database config. Must be: http or cloud');
55-
5653
new Connection(['name' => 'test']);
57-
});
54+
})->throws(RuntimeException::class, 'Invalid [auth_type] in database config. Must be: http or cloud');
5855

5956
test('Cloud Connection Without cloud_id', function () {
6057
$this->expectException(RuntimeException::class);
61-
$this->expectExceptionMessage('auth_type of `cloud` requires `cloud_id` to be set');
6258

6359
new Connection(['name' => 'test', 'auth_type' => 'cloud']);
64-
});
60+
})->throws(RuntimeException::class, 'auth_type of `cloud` requires `cloud_id` to be set');
6561

6662
test('Http Connection Without hosts', function () {
6763
$this->expectException(RuntimeException::class);
6864
$this->expectExceptionMessage('auth_type of `http` requires `hosts` to be set');
6965

7066
new Connection(['name' => 'test', 'auth_type' => 'http']);
67+
})->throws(RuntimeException::class, 'auth_type of `http` requires `hosts` to be set and be an array');
68+
69+
test('Prefix', function () {
70+
$config = [
71+
'name' => 'test',
72+
'auth_type' => 'http',
73+
'hosts' => ['http://localhost:9200'],
74+
'index_prefix' => 'prefix_',
75+
];
76+
77+
$connection = new Connection($config);
78+
79+
expect($connection->getIndexPrefix())->toBe('prefix_');
7180
});
7281

7382
test('Schema Builder', function () {
@@ -79,3 +88,9 @@
7988
$driver = DB::connection('elasticsearch')->getDriverName();
8089
expect($driver === 'elasticsearch')->toBeTrue();
8190
});
91+
92+
test('Info', function () {
93+
$info = DB::connection('elasticsearch')->getClientInfo();
94+
expect($info['cluster_name'])->toBe('elasticsearch')
95+
->and($info['tagline'])->toBe('You Know, for Search');
96+
});

0 commit comments

Comments
 (0)