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

ElasticsearchServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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