Completed
Push — master ( fdda86...a50ad6 )
by David
10s
created

Pheromone   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 82
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getOrFail() 0 7 2
A getContainerProjectDirectory() 0 3 1
A getWhoAmI() 0 3 1
A getLogLevel() 0 13 3
A getOriginContainer() 0 4 2
A getAenthillManifestContent() 0 8 2
A getHostProjectDirectory() 0 3 1
A getOriginImage() 0 4 2
1
<?php
2
3
namespace TheAentMachine;
4
5
use TheAentMachine\Exception\LogLevelException;
6
use TheAentMachine\Exception\MissingEnvironmentVariableException;
7
8
/**
9
 * Utility class to access the Aent configuration settings (stored in environment variables)
10
 */
11
class Pheromone
12
{
13
    private static $levels = [
14
        'DEBUG' => true,
15
        'INFO' => true,
16
        'WARN' => true,
17
        'ERROR' => true,
18
    ];
19
20
    /**
21
     * Returns the log level for this Aent.
22
     *
23
     * @return string
24
     */
25
    public static function getLogLevel(): string
26
    {
27
        $logLevel = getenv('PHEROMONE_LOG_LEVEL');
28
29
        if ($logLevel === false) {
30
            throw LogLevelException::emptyLogLevel();
31
        }
32
33
        if (!array_key_exists($logLevel, self::$levels)) {
34
            throw LogLevelException::invalidLogLevel($logLevel);
35
        }
36
37
        return $logLevel;
38
    }
39
40
    private static function getOrFail(string $variableName): string
41
    {
42
        $value = \getenv($variableName);
43
        if ($value === false) {
44
            throw MissingEnvironmentVariableException::missingEnv($variableName);
45
        }
46
        return $value;
47
    }
48
49
    public static function getWhoAmI(): string
50
    {
51
        return self::getOrFail('PHEROMONE_WHOAMI');
52
    }
53
54
    /**
55
     * The project directory path on the host machine
56
     */
57
    public static function getHostProjectDirectory(): string
58
    {
59
        return self::getOrFail('PHEROMONE_HOST_PROJECT_DIR');
60
    }
61
62
    /**
63
     * The project directory path in the container
64
     */
65
    public static function getContainerProjectDirectory(): string
66
    {
67
        return rtrim(self::getOrFail('PHEROMONE_CONTAINER_PROJECT_DIR'), '/');
68
    }
69
70
    public static function getOriginContainer(): ?string
71
    {
72
        $from = getenv('PHEROMONE_FROM_CONTAINER_ID');
73
        return $from === false ? null : $from;
74
    }
75
76
    public static function getOriginImage(): ?string
77
    {
78
        $from = getenv('PHEROMONE_FROM_IMAGE_NAME');
79
        return $from === false ? null : $from;
80
    }
81
82
    /**
83
     * @return mixed[]
84
     */
85
    public static function getAenthillManifestContent(): array
86
    {
87
        $containerProjectDir = self::getContainerProjectDirectory();
88
        $aenthillJSONstr = file_get_contents($containerProjectDir . '/aenthill.json');
89
        if ($aenthillJSONstr === false) {
90
            throw new \RuntimeException('Failed to load the aenthill manifest file ' . $containerProjectDir . '/aenthill.json');
91
        }
92
        return \GuzzleHttp\json_decode($aenthillJSONstr, true);
93
    }
94
}
95