Passed
Push — master ( d9b17e...f981b4 )
by Zoilo
01:42
created

KubernetesAndContainer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 177
ccs 70
cts 70
cp 1
rs 10
c 0
b 0
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getBasename() 0 10 2
A __construct() 0 5 1
A extractPodId() 0 23 3
A instance() 0 9 2
A podId() 0 3 1
A containerId() 0 3 1
A extractContainerId() 0 23 3
A getDirname() 0 5 1
A extractInfo() 0 21 4
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Helper\MetadataExtractor;
4
5
use ZoiloMora\ElasticAPM\Utils\ControlGroup\ControlGroupRepository;
6
use ZoiloMora\ElasticAPM\Utils\ControlGroup\FileControlGroupRepository;
7
8
final class KubernetesAndContainer
9
{
10
    /**
11
     * @var self
12
     */
13
    private static $instance;
14
15
    /**
16
     * @var ControlGroupRepository
17
     */
18
    private $controlGroupRepository;
19
20
    /**
21
     * @var string|null
22
     */
23
    private $podId = null;
24
25
    /**
26
     * @var string|null
27
     */
28
    private $containerId = null;
29
30
    /**
31
     * @return self
32
     */
33 13
    public static function instance()
34
    {
35 13
        if (null === self::$instance) {
36 1
            self::$instance = new self(
37 1
                new FileControlGroupRepository()
38 1
            );
39 1
        }
40
41 13
        return self::$instance;
42
    }
43
44
    /**
45
     * @param ControlGroupRepository $controlGroupRepository
46
     */
47 9
    public function __construct(ControlGroupRepository $controlGroupRepository)
48
    {
49 9
        $this->controlGroupRepository = $controlGroupRepository;
50
51 9
        $this->extractInfo();
52 9
    }
53
54
    /**
55
     * @return string|null
56
     */
57 19
    public function podId()
58
    {
59 19
        return $this->podId;
60
    }
61
62
    /**
63
     * @return string|null
64
     */
65 18
    public function containerId()
66
    {
67 18
        return $this->containerId;
68
    }
69
70
    /**
71
     * @return void
72
     */
73 9
    private function extractInfo()
74
    {
75 9
        $cGroups = $this->controlGroupRepository->findAll();
76
77 9
        foreach ($cGroups as $cGroup) {
78 7
            $path = $cGroup->path();
79
80 7
            $dirname = $this->getDirname($path);
81 7
            $basename = $this->getBasename($path);
82
83 7
            $podId = $this->extractPodId($dirname);
84 7
            if (null !== $podId) {
85 3
                $this->podId = $podId;
86 3
                $this->containerId = $basename;
87
88 3
                break;
89
            }
90
91 4
            $containerId = $this->extractContainerId($basename);
92 4
            if (null !== $containerId) {
93 2
                $this->containerId = $containerId;
94 2
            }
95 9
        }
96 9
    }
97
98
    /**
99
     * @param string $path
100
     *
101
     * @return string
102
     */
103 7
    private function getDirname($path)
104
    {
105 7
        $position = 1 + strrpos($path, '/');
106
107 7
        return substr($path, 0, $position);
108
    }
109
110
    /**
111
     * @param string $path
112
     *
113
     * @return string
114
     */
115 7
    private function getBasename($path)
116
    {
117 7
        $position = 1 + strrpos($path, '/');
118 7
        $basename = substr($path, $position);
119
120 7
        if ('.scope' === substr($basename, -6)) {
121 1
            $basename = substr($basename, 0, -6);
122 1
        }
123
124 7
        return $basename;
125
    }
126
127
    /**
128
     * @param string $dirname
129
     *
130
     * @return string|null
131
     */
132 7
    private function extractPodId($dirname)
133
    {
134 7
        preg_match(
135 7
            '/(?:^\/kubepods\/[^\/]+\/pod([^\/]+)\/$)/',
136 7
            $dirname,
137
            $output
138 7
        );
139
140 7
        if (0 !== count($output)) {
141 2
            return $output[1];
142
        }
143
144 5
        preg_match(
145 5
            '/(?:^\/kubepods\.slice\/kubepods-[^\/]+\.slice\/kubepods-[^\/]+-pod([^\/]+)\.slice\/$)/',
146 5
            $dirname,
147
            $output
148 5
        );
149
150 5
        if (0 !== count($output)) {
151 1
            return $output[1];
152
        }
153
154 4
        return null;
155
    }
156
157
    /**
158
     * @param string $basename
159
     *
160
     * @return string|null
161
     */
162 4
    private function extractContainerId($basename)
163
    {
164 4
        preg_match(
165 4
            '/^[[:xdigit:]]{64}$/',
166 4
            $basename,
167
            $output
168 4
        );
169
170 4
        if (0 !== count($output)) {
171 1
            return $output[0];
172
        }
173
174 3
        preg_match(
175 3
            '/^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4,}$/',
176 3
            $basename,
177
            $output
178 3
        );
179
180 3
        if (0 !== count($output)) {
181 1
            return $output[0];
182
        }
183
184 2
        return null;
185
    }
186
}
187