Pod::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
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 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Common\System\Kubernetes;
4
5
use ZoiloMora\ElasticAPM\Helper\Encoding;
6
use ZoiloMora\ElasticAPM\Helper\MetadataExtractor\KubernetesAndContainer;
7
8
final class Pod implements \JsonSerializable
9
{
10
    /**
11
     * Kubernetes pod name
12
     *
13
     * @var string|null
14
     */
15
    private $name;
16
17
    /**
18
     * Kubernetes pod uid
19
     *
20
     * @var string|null
21
     */
22
    private $uid;
23
24
    /**
25
     * @param string|null $name
26
     * @param string|null $uid
27
     */
28 17
    public function __construct($name = null, $uid = null)
29
    {
30 17
        $this->name = $name;
31 17
        $this->uid = $uid;
32 17
    }
33
34
    /**
35
     * @return string|null
36
     */
37 1
    public function name()
38
    {
39 1
        return $this->name;
40
    }
41
42
    /**
43
     * @return string|null
44
     */
45 1
    public function uid()
46
    {
47 1
        return $this->uid;
48
    }
49
50
    /**
51
     * @return Pod
52
     */
53 14
    public static function discover()
54
    {
55 14
        return new self(
56 14
            getenv('KUBERNETES_POD_NAME') ?: null,
57 14
            getenv('KUBERNETES_POD_UID') ?: KubernetesAndContainer::instance()->podId()
58 14
        );
59
    }
60
61
    /**
62
     * @return array
63
     */
64 1
    public function jsonSerialize()
65
    {
66
        return [
67 1
            'name' => Encoding::keywordField($this->name),
68 1
            'uid' => Encoding::keywordField($this->uid),
69 1
        ];
70
    }
71
}
72