DruidServiceProvider::setupConfig()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid;
5
6
use Illuminate\Support\ServiceProvider;
7
use Laravel\Lumen\Application as LumenApplication;
8
use Illuminate\Foundation\Application as LaravelApplication;
9
10
/**
11
 * Class DruidServiceProvider
12
 *
13
 * @package Level23\Druid
14
 * @codeCoverageIgnore
15
 */
16
class DruidServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Register the application services.
20
     */
21
    public function register(): void
22
    {
23
        $this->registerDruidClient();
24
    }
25
26
    /**
27
     * Publish and merge the config
28
     */
29
    protected function setupConfig(): void
30
    {
31
        $source = (string)realpath(__DIR__ . '/../config/config.php');
32
33
        if ($this->app instanceof LaravelApplication) {
34
            $this->publishes([$source => config_path('druid.php')]);
35
        } elseif ($this->app instanceof LumenApplication) {
36
            $this->app->configure('druid');
37
        }
38
39
        $this->mergeConfigFrom($source, 'druid');
40
    }
41
42
    /**
43
     * Register the application bindings.
44
     *
45
     * @return void
46
     */
47
    protected function registerDruidClient(): void
48
    {
49
        $this->setupConfig();
50
51
        $this->app->singleton(DruidClient::class, function () {
52
            $client = new DruidClient($this->app['config']['druid']); // @phpstan-ignore-line
53
            $client->setLogger($this->app['log']); // @phpstan-ignore-line
54
55
            return $client;
56
        });
57
58
        $this->app->bind('druid', DruidClient::class);
59
    }
60
}