Completed
Pull Request — master (#14)
by Sergey
03:24
created

LaravelDatadogHelperServiceProvider::initDatadog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 30
rs 9.6666
cc 2
nc 2
nop 0
1
<?php
2
3
namespace ChaseConey\LaravelDatadogHelper;
4
5
use ChaseConey\LaravelDatadogHelper\Datadog\BatchedDogStatsd;
6
use ChaseConey\LaravelDatadogHelper\Datadog\DogStatsd;
7
use Illuminate\Support\ServiceProvider;
8
9
class LaravelDatadogHelperServiceProvider extends ServiceProvider
10
{
11
12
    /**
13
     * Perform post-registration booting of services.
14
     *
15
     * @return void
16
     */
17
    public function boot()
18
    {
19
        /** @noinspection PhpUndefinedFunctionInspection */
20
        $this->publishes(
21
            [
22
                __DIR__ . '/../config/datadog-helper.php' => config_path('datadog-helper.php'),
0 ignored issues
show
Bug introduced by
The function config_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
                __DIR__ . '/../config/datadog-helper.php' => /** @scrutinizer ignore-call */ config_path('datadog-helper.php'),
Loading history...
23
            ]
24
        );
25
    }
26
27
    /**
28
     * Register any package services.
29
     *
30
     * @return void
31
     */
32
    public function register()
33
    {
34
35
        $this->app->singleton('datadog', function() {
36
            return $this->initDatadog();
37
        });
38
    }
39
40
    protected function initDatadog()
41
    {
42
        $this->mergeConfigFrom(
43
            __DIR__ . '/../config/datadog-helper.php', 'datadog-helper'
44
        );
45
46
        /** @noinspection PhpUndefinedFunctionInspection */
47
        $laravelConfig = config('datadog-helper');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        $laravelConfig = /** @scrutinizer ignore-call */ config('datadog-helper');
Loading history...
48
49
        $ddConfig = [
50
            'host' => $laravelConfig['statsd_server'],
51
            'port' => $laravelConfig['statsd_port'],
52
            'datadog_host' => $laravelConfig['datadog_host'],
53
            'api_key' => $laravelConfig['api_key'],
54
            'app_key' => $laravelConfig['application_key'],
55
            'global_tags' => $laravelConfig['global_tags'],
56
        ];
57
58
        $maxBuffer = $laravelConfig['max_buffer_length'];
59
        if($maxBuffer > 1) {
60
            $datadog = new BatchedDogStatsd($ddConfig);
61
            $datadog::$maxBufferLength = $maxBuffer;
62
        }
63
        else {
64
            $datadog = new DogStatsd($ddConfig);
65
        }
66
67
        $datadog->setMetricsPrefix($laravelConfig['prefix']);
68
69
        return $datadog;
70
    }
71
}
72