Skip to content

Commit 49e8a06

Browse files
committed
fix(connection): streamline setOptions and add auth_type validation
- Added `validateAuthType` method to verify auth types. - Enhanced `setOptions` to use null coalescing for index prefix. - Simplified `setIndex` method for cleaner logic. - Replaced multiple `str_contains` checks with single line logic. What did the code say to the bug? You're no match for my refactor skills! 🐛🔥
1 parent b61da1f commit 49e8a06

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/Connection.php

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
*/
4545
class Connection extends BaseConnection
4646
{
47+
const VALID_AUTH_TYPES = ['http', 'cloud'];
48+
4749
protected Client $client;
4850

4951
protected string $index = '';
@@ -71,6 +73,7 @@ class Connection extends BaseConnection
7173
*/
7274
protected $postProcessor;
7375

76+
/** {@inheritdoc} */
7477
public function __construct(array $config)
7578
{
7679

@@ -89,11 +92,10 @@ public function __construct(array $config)
8992
$this->useDefaultQueryGrammar();
9093
}
9194

92-
public function setOptions($config)
95+
public function setOptions($config): void
9396
{
94-
if (! empty($config['index_prefix'])) {
95-
$this->indexPrefix = $config['index_prefix'];
96-
}
97+
$this->indexPrefix = $config['index_prefix'] ?? '';
98+
9799
if (isset($config['options']['allow_id_sort'])) {
98100
$this->allowIdSort = $config['options']['allow_id_sort'];
99101
}
@@ -106,26 +108,31 @@ public function setOptions($config)
106108
if (isset($config['options']['meta_header'])) {
107109
$this->elasticMetaHeader = $config['options']['meta_header'];
108110
}
111+
109112
if (! empty($config['error_log_index'])) {
110-
if ($this->indexPrefix) {
111-
$this->errorLoggingIndex = $this->indexPrefix.'_'.$config['error_log_index'];
112-
} else {
113-
$this->errorLoggingIndex = $config['error_log_index'];
114-
}
113+
$this->errorLoggingIndex = $this->indexPrefix
114+
? $this->indexPrefix.'_'.$config['error_log_index']
115+
: $config['error_log_index'];
115116
}
117+
116118
}
117119

118120
protected function buildConnection(): Client
119121
{
120-
$type = config('database.connections.elasticsearch.auth_type') ?? null;
121-
$type = strtolower($type);
122-
if (! in_array($type, ['http', 'cloud'])) {
123-
throw new RuntimeException('Invalid [auth_type] in database config. Must be: http, cloud or api');
124-
}
122+
$type = strtolower(config('database.connections.elasticsearch.auth_type', ''));
123+
$this->validateAuthType($type);
125124

126125
return $this->{'_'.$type.'Connection'}();
127126
}
128127

128+
private function validateAuthType(string $type): void
129+
{
130+
if (! in_array($type, self::VALID_AUTH_TYPES)) {
131+
throw new RuntimeException('Invalid [auth_type] in database config. Must be: http, cloud or api');
132+
}
133+
}
134+
135+
/** {@inheritdoc} */
129136
public function getTablePrefix(): ?string
130137
{
131138
return $this->getIndexPrefix();
@@ -136,6 +143,7 @@ public function getIndexPrefix(): ?string
136143
return $this->indexPrefix;
137144
}
138145

146+
/** {@inheritdoc} */
139147
public function getPostProcessor(): Query\Processor
140148
{
141149
return $this->postProcessor;
@@ -151,6 +159,7 @@ public function getErrorLoggingIndex(): ?string
151159
return $this->errorLoggingIndex;
152160
}
153161

162+
/** {@inheritdoc} */
154163
public function getSchemaGrammar(): Schema\Grammar
155164
{
156165
return new Schema\Grammar;
@@ -161,14 +170,11 @@ public function getIndex(): string
161170
return $this->index;
162171
}
163172

164-
public function setIndex($index): string
173+
public function setIndex(string $index): string
165174
{
166-
$this->index = $index;
167-
if ($this->indexPrefix) {
168-
if (! (str_contains($this->index, $this->indexPrefix.'_'))) {
169-
$this->index = $this->indexPrefix.'_'.$index;
170-
}
171-
}
175+
$this->index = $this->indexPrefix && ! str_contains($index, $this->indexPrefix.'_')
176+
? $this->indexPrefix.'_'.$index
177+
: $index;
172178

173179
return $this->getIndex();
174180
}
@@ -188,17 +194,13 @@ public function getSchemaBuilder(): Schema\Builder
188194
return new Schema\Builder($this);
189195
}
190196

191-
/**
192-
* {@inheritdoc}
193-
*/
197+
/** {@inheritdoc} */
194198
public function disconnect(): void
195199
{
196200
unset($this->connection);
197201
}
198202

199-
/**
200-
* {@inheritdoc}
201-
*/
203+
/** {@inheritdoc} */
202204
public function getDriverName(): string
203205
{
204206
return 'elasticsearch';
@@ -243,9 +245,7 @@ public function __call($method, $parameters)
243245
return $bridge->{'process'.Str::studly($method)}(...$parameters);
244246
}
245247

246-
/**
247-
* {@inheritdoc}
248-
*/
248+
/** {@inheritdoc} */
249249
protected function getDefaultPostProcessor(): Query\Processor
250250
{
251251
return new Query\Processor;
@@ -255,17 +255,13 @@ protected function getDefaultPostProcessor(): Query\Processor
255255
// Connection Builder
256256
//----------------------------------------------------------------------
257257

258-
/**
259-
* {@inheritdoc}
260-
*/
258+
/** {@inheritdoc} */
261259
protected function getDefaultQueryGrammar(): Query\Grammar
262260
{
263261
return new Query\Grammar;
264262
}
265263

266-
/**
267-
* {@inheritdoc}
268-
*/
264+
/** {@inheritdoc} */
269265
protected function getDefaultSchemaGrammar(): Schema\Grammar
270266
{
271267
return new Schema\Grammar;

0 commit comments

Comments
 (0)