Test Failed
Push — master ( 5a8b85...1177ba )
by Zoilo
01:48
created

Kubernetes::discover()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Common\System;
4
5
use ZoiloMora\ElasticAPM\Events\Common\System\Kubernetes\Node;
6
use ZoiloMora\ElasticAPM\Events\Common\System\Kubernetes\Pod;
7
use ZoiloMora\ElasticAPM\Helper\Encoding;
8
9
/**
10
 * https://www.elastic.co/guide/en/apm/server/master/metadata-api.html#kubernetes-data
11
 */
12
final class Kubernetes implements \JsonSerializable
13
{
14
    /**
15
     * Kubernetes namespace
16
     *
17
     * @var string|null
18
     */
19
    private $namespace;
20
21
    /**
22
     * @var Pod|null
23
     */
24
    private $pod;
25
26
    /**
27
     * @var Node|null
28
     */
29
    private $node;
30
31
    /**
32
     * @param string|null $namespace
33
     * @param Pod|null $pod
34
     * @param Node|null $node
35
     */
36 3
    public function __construct($namespace = null, Pod $pod = null, Node $node = null)
37
    {
38 3
        $this->namespace = $namespace;
39 3
        $this->pod = $pod;
40 3
        $this->node = $node;
41 3
    }
42
43
    /**
44
     * @return Pod|null
45
     */
46 1
    public function pod()
47
    {
48 1
        return $this->pod;
49
    }
50
51
    /**
52
     * @return Node|null
53
     */
54 1
    public function node()
55
    {
56 1
        return $this->node;
57
    }
58
59
    /**
60
     * @return Kubernetes
61
     */
62
    public static function discover()
63
    {
64
        return new self(
65
            getenv('KUBERNETES_NAMESPACE') ?: null,
66
            Pod::discover(),
67
            Node::discover()
68
        );
69
    }
70
71
    /**
72
     * @return array
73
     */
74 1
    public function jsonSerialize()
75
    {
76
        return [
77 1
            'namespace' => Encoding::keywordField($this->namespace),
78 1
            'pod' => $this->pod,
79 1
            'node' => $this->node,
80 1
        ];
81
    }
82
}
83