Completed
Push — master ( 5ec713...1c1906 )
by Colin
11s
created

ServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 58
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 5 1
A register() 0 18 1
A setUpConfig() 0 12 3
A setUpConsoleCommands() 0 8 2
1
<?php namespace Cviebrock\LaravelElasticsearch;
2
3
use Cviebrock\LaravelElasticsearch\Console\Command\IndexExistsCommand;
4
use Elasticsearch\Client;
5
use Illuminate\Container\Container;
6
use Illuminate\Foundation\Application as LaravelApplication;
7
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
8
use Laravel\Lumen\Application as LumenApplication;
9
10
11
/**
12
 * Class ServiceProvider
13
 *
14
 * @package Cviebrock\LaravelElasticsearch
15
 */
16
class ServiceProvider extends BaseServiceProvider
17
{
18
19
    /**
20
     * Bootstrap the application services.
21
     */
22
    public function boot()
23
    {
24
        $this->setUpConfig();
25
        $this->setUpConsoleCommands();
26
    }
27
28
    /**
29
     * Register the application services.
30
     *
31
     * @return void
32
     */
33
    public function register()
34
    {
35
        $app = $this->app;
36
37
        $app->singleton('elasticsearch.factory', function($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
            return new Factory();
39
        });
40
41
        $app->singleton('elasticsearch', function($app) {
42
            return new Manager($app, $app['elasticsearch.factory']);
43
        });
44
45
        $app->alias('elasticsearch', Manager::class);
46
47
        $app->singleton(Client::class, function(Container $app) {
48
            return $app->make('elasticsearch')->connection();
49
        });
50
    }
51
52
    protected function setUpConfig(): void
53
    {
54
        $source = dirname(__DIR__) . '/config/elasticsearch.php';
55
56
        if ($this->app instanceof LaravelApplication) {
57
            $this->publishes([$source => config_path('elasticsearch.php')], 'config');
58
        } elseif ($this->app instanceof LumenApplication) {
0 ignored issues
show
Bug introduced by
The class Laravel\Lumen\Application does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
59
            $this->app->configure('elasticsearch');
60
        }
61
62
        $this->mergeConfigFrom($source, 'elasticsearch');
63
    }
64
65
    private function setUpConsoleCommands(): void
66
    {
67
        if ($this->app->runningInConsole()) {
68
            $this->commands([
69
                IndexExistsCommand::class,
70
            ]);
71
        }
72
    }
73
}
74