Sample   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 50
ccs 14
cts 14
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A assertName() 0 8 3
A jsonSerialize() 0 5 1
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\MetricSet;
4
5
/**
6
 * A single metric sample.
7
 */
8
final class Sample implements \JsonSerializable
9
{
10
    /**
11
     * @var string
12
     */
13
    private $name;
14
15
    /**
16
     * @var double
17
     */
18
    private $value;
19
20
    /**
21
     * @param string $name
22
     * @param double $value
23
     */
24 5
    public function __construct($name, $value)
25
    {
26 5
        $this->assertName($name);
27
28 3
        $this->name = $name;
29 3
        $this->value = (double) $value;
30 3
    }
31
32
    /**
33
     * @param string|null $name
34
     *
35
     * @return void
36
     *
37
     * @throws \InvalidArgumentException
38
     */
39 5
    private function assertName($name)
40
    {
41 5
        if (false === is_string($name)) {
42 1
            throw new \InvalidArgumentException('The [name] must be of type string.');
43
        }
44
45 4
        if (0 === preg_match('/^[^*"]*$/', $name)) {
46 1
            throw new \InvalidArgumentException('The [name] must match the regular expression /^[^*"]*$/');
47
        }
48 3
    }
49
50
    /**
51
     * @return array
52
     */
53 2
    public function jsonSerialize()
54
    {
55
        return [
56 2
            $this->name => [
57 2
                'value' => $this->value,
58
            ]
59 2
        ];
60
    }
61
}
62