Completed
Push — master ( 9d421d...7b686b )
by Avtandil
10:28 queued 08:22
created

ElasticSearchServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 8
cts 20
cp 0.4
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A register() 0 31 4
A provides() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\ElasticSearch;
13
14
use Elasticsearch\Client;
15
use Elasticsearch\ClientBuilder;
16
use Illuminate\Foundation\Application;
17
use Illuminate\Support\ServiceProvider;
18
19
class ElasticSearchServiceProvider extends ServiceProvider
20
{
21
    protected $defer = true;
22
23 11
    public function boot(): void
24
    {
25
        //
26 11
    }
27
28 11
    public function register(): void
29
    {
30
        $this->app->singleton(Client::class, function (Application $app) {
31
            // Logger instance
32
            $config = $app['config']->get('services.elastic_search');
33
34
            $params = [
35
                'hosts' => $config['hosts'],
36
            ];
37
38
            if (! empty($config['connectionParams'])) {
39
                $params['connectionParams'] = $config['connectionParams'];
40
            }
41
42
            $logger = ! empty($config['log_channel']) ? $app['log']->stack($config['log_channel']) : null;
43
            if ($logger) {
44
                $params['logger'] = $logger;
45
            }
46
47
            $client = ClientBuilder::fromConfig($params);
48
49
            return $client;
50 11
        });
51
52
        $this->app->singleton(ElasticSearchManagerContract::class, function (Application $app) {
53
            $client = $app->make(Client::class);
54
            $enabled = (bool) $app['config']->get('services.elastic_search.enabled', false);
55
56
            return new ElasticSearchManager($client, $enabled);
57 11
        });
58 11
    }
59
60 1
    public function provides()
61
    {
62 1
        return [Client::class, ElasticSearchManagerContract::class];
63
    }
64
}
65