Sample::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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