Completed
Push — master ( a49287...23805c )
by Avtandil
14:39 queued 15s
created

ElasticsearchServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 44
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 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\Contracts\Support\DeferrableProvider;
17
use Illuminate\Foundation\Application;
18
use Illuminate\Support\ServiceProvider;
19
20
class ElasticsearchServiceProvider extends ServiceProvider implements DeferrableProvider
21
{
22 11
    public function boot(): void
23
    {
24
        //
25 11
    }
26
27 11
    public function register(): void
28
    {
29
        $this->app->singleton(Client::class, function (Application $app) {
30
            // Logger instance
31
            $config = $app['config']->get('services.elastic_search');
32
33
            $params = [
34
                'hosts' => $config['hosts'],
35
            ];
36
37
            if (! empty($config['connectionParams'])) {
38
                $params['connectionParams'] = $config['connectionParams'];
39
            }
40
41
            $logger = ! empty($config['log_channel']) ? $app['log']->stack($config['log_channel']) : null;
42
            if ($logger) {
43
                $params['logger'] = $logger;
44
            }
45
46
            $client = ClientBuilder::fromConfig($params);
47
48
            return $client;
49 11
        });
50
51
        $this->app->singleton(ElasticsearchManagerContract::class, function (Application $app) {
52
            $client = $app->make(Client::class);
53
            $enabled = (bool) $app['config']->get('services.elastic_search.enabled', false);
54
55
            return new ElasticsearchManager($client, $enabled);
56 11
        });
57 11
    }
58
59 1
    public function provides()
60
    {
61 1
        return [Client::class, ElasticsearchManagerContract::class];
62
    }
63
}
64