Completed
Push — master ( a35239...addb26 )
by Chase
02:09
created

LaravelDatadogHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prefixData() 0 8 2
A prefix() 0 9 2
A send() 0 4 1
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
    {
13
        $prefixedData = self::prefixData($data);
14
        parent::send($prefixedData, $sampleRate, $tags);
15
    }
16
17
    /**
18
     * Takes normal data array from Datadogstatsd library and adds the Laravel prefix functionality
19
     *
20
     * @param $data
21
     * @return array
22
     */
23
    protected static function prefixData($data)
24
    {
25
        $prefixedData = array();
26
        foreach ($data as $stat => $value) {
27
            $prefixedKey = self::prefix($stat);
28
            $prefixedData[$prefixedKey] = $value;
29
        }
30
        return $prefixedData;
31
    }
32
33
    /**
34
     * Prefixes a metric name if one is available via config
35
     *
36
     * @return string
37
     */
38
    protected static function prefix($metric)
39
    {
40
        $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

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