Skip to content

Commit

Permalink
Add logger option to client builder
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubMerkandi committed Jul 26, 2023
1 parent 49f512d commit 7f4f7b3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"elasticsearch/elasticsearch": "^7.16",
"illuminate/support": "^9.0||^10.0",
"laravel/scout": "^9.0||^10.0",
"webmozart/assert": "^1.10"
"webmozart/assert": "^1.10",
"psr/log": "^1|^2|^3"
},
"require-dev": {
"phpunit/phpunit": "~9.0",
Expand Down
2 changes: 2 additions & 0 deletions config/explorer.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
* A model is only using index aliases if it implements the Aliased interface.
*/
'prune_old_aliases' => true,

'logger' => false,
];
5 changes: 4 additions & 1 deletion src/ExplorerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public function boot(): void

$this->app->when(ElasticClientFactory::class)
->needs(Client::class)
->give(static fn () => ElasticClientBuilder::fromConfig(config())->build());
->give(static fn () => ElasticClientBuilder::fromConfig(
config(),
logger()
)->build());

$this->app->when(ElasticDocumentAdapter::class)
->needs(Client::class)
Expand Down
7 changes: 6 additions & 1 deletion src/Infrastructure/Elastic/ElasticClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Contracts\Config\Repository;
use Psr\Log\LoggerInterface;

final class ElasticClientBuilder
{
private const HOST_KEYS = ['host', 'port', 'scheme'];

public static function fromConfig(Repository $config): ClientBuilder
public static function fromConfig(Repository $config, LoggerInterface $logger): ClientBuilder
{
$builder = ClientBuilder::create();

Expand Down Expand Up @@ -65,6 +66,10 @@ public static function fromConfig(Repository $config): ClientBuilder
$builder->setSSLCert($path, $password);
}

if($config->get('explorer.logger', false)) {
$builder->setLogger($logger);
}

return $builder;
}

Expand Down
56 changes: 56 additions & 0 deletions tests/Support/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace JeroenG\Explorer\Tests\Support;

use Psr\Log\LoggerInterface;

final class Logger implements LoggerInterface
{

public function emergency(\Stringable|string $message, array $context = []): void
{
// TODO: Implement emergency() method.
}

public function alert(\Stringable|string $message, array $context = []): void
{
// TODO: Implement alert() method.
}

public function critical(\Stringable|string $message, array $context = []): void
{
// TODO: Implement critical() method.
}

public function error(\Stringable|string $message, array $context = []): void
{
// TODO: Implement error() method.
}

public function warning(\Stringable|string $message, array $context = []): void
{
// TODO: Implement warning() method.
}

public function notice(\Stringable|string $message, array $context = []): void
{
// TODO: Implement notice() method.
}

public function info(\Stringable|string $message, array $context = []): void
{
// TODO: Implement info() method.
}

public function debug(\Stringable|string $message, array $context = []): void
{
// TODO: Implement debug() method.
}

public function log($level, \Stringable|string $message, array $context = []): void
{
// TODO: Implement log() method.
}
}
5 changes: 4 additions & 1 deletion tests/Unit/ElasticClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Container\Container;
use JeroenG\Explorer\Infrastructure\Elastic\ElasticClientBuilder;
use JeroenG\Explorer\Tests\Support\ConfigRepository;
use JeroenG\Explorer\Tests\Support\Logger;
use Mockery\Adapter\Phpunit\MockeryTestCase;

final class ElasticClientBuilderTest extends MockeryTestCase
Expand All @@ -20,9 +21,11 @@ final class ElasticClientBuilderTest extends MockeryTestCase
public function test_it_creates_client_with_config(array $config, ClientBuilder $expectedBuilder): void
{
$configRepository = new ConfigRepository([ 'explorer' => $config ]);
$logger = new Logger();

Container::getInstance()->instance('config', $configRepository);

$resultBuilder = ElasticClientBuilder::fromConfig($configRepository);
$resultBuilder = ElasticClientBuilder::fromConfig($configRepository, $logger);

self::assertEquals($expectedBuilder, $resultBuilder);
}
Expand Down

0 comments on commit 7f4f7b3

Please sign in to comment.