DockerNamesStringFunction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B getValue() 0 31 7
1
<?php
2
3
namespace Startwind\Forrest\Enrichment\EnrichFunction\Explode;
4
5
use Startwind\Forrest\Enrichment\EnrichFunction\CacheableFunction;
6
use Startwind\Forrest\Logger\ForrestLogger;
7
use Startwind\Forrest\Runner\CommandRunner;
8
use Startwind\Forrest\Runner\Exception\ToolNotFoundException;
9
use Symfony\Component\Console\Command\Command;
10
11
class DockerNamesStringFunction extends BasicExplodeFunction implements CacheableFunction
12
{
13
    protected string $functionName = 'docker-names';
14
15
    protected function getValue(string $value): array
16
    {
17
        if (!CommandRunner::isToolInstalled('docker', $command)) {
18
            throw new ToolNotFoundException('The cli tool "docker" has to be installed to use the "docker-name" enrichment function.');
19
        }
20
21
        exec("docker ps --no-trunc --format='{{json .}}' 2>&1", $output, $statusCode);
22
23
        if ($statusCode !== Command::SUCCESS) {
24
            if (str_contains($output[0], 'Is the docker daemon running?')) {
25
                ForrestLogger::warn('Docker daemon not running. Please start it to use the "docker-names" function.');
26
            } else {
27
                ForrestLogger::warn($output[0]);
28
            }
29
            return [];
30
        }
31
32
        $names = [];
33
34
        foreach ($output as $containerJson) {
35
            $container = json_decode($containerJson, true);
36
            if ($container) {
37
                $names[] = $container['Names'];
38
            }
39
        }
40
41
        if (count($names) == 0) {
42
            ForrestLogger::warn('Currently there are no docker containers running. Please start one to use the "docker-names" function.');
43
        }
44
45
        return $names;
46
    }
47
}
48