Completed
Pull Request — master (#7)
by Chase
02:04
created

LaravelDatadogHelper::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ChaseConey\LaravelDatadogHelper;
4
5
class LaravelDatadogHelper extends \Datadogstatsd
6
{
7
8
    /**
9
     * {@inheritdoc}
10
     */
11
    public static function send($data, $sampleRate = 1.0, array $tags = null) {
12
        $prefixedData = self::prefixData($data);
13
        parent::send($prefixedData, $sampleRate, $tags);
14
    }
15
16
    /**
17
     * Takes normal data array from Datadogstatsd library and adds the Laravel prefix functionality
18
     *
19
     * @param $data
20
     * @return array
21
     */
22
    protected static function prefixData($data)
23
    {
24
        $prefixedData = array();
25
        foreach ($data as $stat => $value) {
26
            $prefixedKey = self::prefix($stat);
27
            $prefixedData[$prefixedKey] = $value;
28
        }
29
        return $prefixedData;
30
    }
31
32
    /**
33
     * Prefixes a metric name if one is available via config
34
     *
35
     * @return string
36
     */
37
    protected static function prefix($metric)
38
    {
39
        $prefix = config('datadog-helper.prefix');
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

39
        $prefix = /** @scrutinizer ignore-call */ config('datadog-helper.prefix');
Loading history...
40
41
        if (!$prefix) {
42
            return $metric;
43
        }
44
45
        return "{$prefix}.{$metric}";
46
    }
47
48
}
49