Completed
Push — master ( 5e7d5e...db75a5 )
by Avtandil
07:05
created

ElasticSearchServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
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\Foundation\Application;
17
use Illuminate\Support\ServiceProvider;
18
19
class ElasticSearchServiceProvider extends ServiceProvider
20
{
21
    protected $defer = true;
22
23
    public function boot()
24
    {
25
        //
26
    }
27
28
    public function register()
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
43
            $logger = ! empty($config['log_channel']) ? $app['log']->stack($config['log_channel']) : null;
44
            if ($logger) {
45
                $params['logger'] = $logger;
46
            }
47
48
            $client = ClientBuilder::fromConfig($params);
49
50
            return $client;
51
        });
52
53
        $this->app->singleton(ElasticSearchManagerContract::class, function (Application $app) {
54
            $client = $app->make(Client::class);
55
            $enabled = (bool) $app['config']->get('services.elastic_search.enabled', false);
56
57
            return new ElasticSearchManager($client, $enabled);
58
        });
59
    }
60
61
    public function provides()
62
    {
63
        return [Client::class, ElasticSearchManagerContract::class];
64
    }
65
}
66