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

Container   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
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 62
ccs 18
cts 18
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 3 1
A __construct() 0 5 1
A jsonSerialize() 0 8 2
A assertId() 0 4 3
A discover() 0 4 1
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