Completed
Push — master ( efafaa...1ce8a3 )
by Avtandil
05:26
created

ElasticsearchServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 45.45%

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 10
cts 22
cp 0.4545
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A provides() 0 4 1
A register() 0 31 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\Elasticsearch;
6
7
use Elasticsearch\Client;
8
use Elasticsearch\ClientBuilder;
9
use Illuminate\Contracts\Support\DeferrableProvider;
10
use Illuminate\Foundation\Application;
11
use Illuminate\Support\ServiceProvider;
12
13
class ElasticsearchServiceProvider extends ServiceProvider implements DeferrableProvider
14
{
15 14
    public function boot(): void
16
    {
17
        //
18 14
    }
19
20 14
    public function register(): void
21
    {
22 14
        $this->app->singleton(Client::class, static function (Application $app) {
23
            // Logger instance
24
            $config = $app['config']->get('services.elastic_search');
25
26
            $params = [
27
                'hosts' => $config['hosts'],
28
            ];
29
30
            if (! empty($config['connectionParams'])) {
31
                $params['connectionParams'] = $config['connectionParams'];
32
            }
33
34
            $logger = ! empty($config['log_channel']) ? $app['log']->stack($config['log_channel']) : null;
35
            if ($logger) {
36
                $params['logger'] = $logger;
37
            }
38
39
            $client = ClientBuilder::fromConfig($params);
40
41
            return $client;
42 14
        });
43
44 14
        $this->app->singleton(ElasticsearchManagerContract::class, static function (Application $app) {
45
            $client = $app->make(Client::class);
46
            $enabled = (bool) $app['config']->get('services.elastic_search.enabled', false);
47
48
            return new ElasticsearchManager($client, $enabled);
49 14
        });
50 14
    }
51
52 1
    public function provides()
53
    {
54 1
        return [Client::class, ElasticsearchManagerContract::class];
55
    }
56
}
57