Passed
Push — master ( 1177ba...21cebb )
by Zoilo
01:39
created

Container::discover()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Common\System;
4
5
use ZoiloMora\ElasticAPM\Utils\ControlGroups;
6
use ZoiloMora\ElasticAPM\Helper\Encoding;
7
8
final class Container implements \JsonSerializable
9
{
10
    /**
11
     * Container ID
12
     *
13
     * @var string
14
     */
15
    private $id;
16
17
    /**
18
     * @param string $id
19
     */
20 15
    public function __construct($id)
21
    {
22 15
        $this->assertId($id);
23
24 14
        $this->id = $id;
25 14
    }
26
27
    /**
28
     * @param mixed $id
29
     *
30
     * @return void
31
     *
32
     * @throws \InvalidArgumentException
33
     */
34 15
    private function assertId($id)
35
    {
36 15
        if (null !== $id && false === is_string($id)) {
37 1
            throw new \InvalidArgumentException('[id] must be one of these types: string or null.');
38
        }
39 14
    }
40
41
    /**
42
     * @return string
43
     */
44 1
    public function id()
45
    {
46 1
        return $this->id;
47
    }
48
49
    /**
50
     * @return Container
51
     */
52 11
    public static function discover()
53
    {
54 11
        return new self(
55 11
            ControlGroups::instance()->containerId()
56 11
        );
57
    }
58
59
    /**
60
     * @return array|null
61
     */
62 2
    public function jsonSerialize()
63
    {
64 2
        if (null === $this->id) {
65 1
            return null;
66
        }
67
68
        return [
69 1
            'id' => Encoding::keywordField($this->id),
70 1
        ];
71
    }
72
}
73