Completed
Pull Request — master (#24)
by Julien
02:36 queued 15s
created

Pheromone::getWhoAmI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * @throws LogLevelException
25
     */
26
    public static function getLogLevel(): string
27
    {
28
        $logLevel = getenv('PHEROMONE_LOG_LEVEL');
29
30
        if ($logLevel === false) {
31
            throw LogLevelException::emptyLogLevel();
32
        }
33
34
        if (!array_key_exists($logLevel, self::$levels)) {
35
            throw LogLevelException::invalidLogLevel($logLevel);
36
        }
37
38
        return $logLevel;
39
    }
40
41
    /**
42
     * Tries to returns the value of an environment variable or throws an exception.
43
     *
44
     * @param string $variableName the environment variable key
45
     * @return string
46
     * @throws MissingEnvironmentVariableException
47
     */
48
    private static function getOrFail(string $variableName): string
49
    {
50
        $value = getenv($variableName);
51
        if ($value === false) {
52
            throw MissingEnvironmentVariableException::missingEnv($variableName);
53
        }
54
        return $value;
55
    }
56
57
    /**
58
     * Tries to returns the value of an environment variable or null.
59
     *
60
     * @param string $variableName the environment variable key
61
     * @return null|string
62
     */
63
    private static function getOrNull(string $variableName): ?string
64
    {
65
        $value = getenv($variableName);
66
        return $value === false ? null : $value;
67
    }
68
69
    /**
70
     * The project directory path on the host machine.
71
     *
72
     * @return string
73
     * @throws MissingEnvironmentVariableException
74
     */
75
    public static function getHostProjectDirectory(): string
76
    {
77
        return self::getOrFail('PHEROMONE_HOST_PROJECT_DIR');
78
    }
79
80
    /**
81
     * The project directory path in the container.
82
     *
83
     * @return string
84
     * @throws MissingEnvironmentVariableException
85
     */
86
    public static function getContainerProjectDirectory(): string
87
    {
88
        return rtrim(self::getOrFail('PHEROMONE_CONTAINER_PROJECT_DIR'), '/');
89
    }
90
91
    /**
92
     * The current image of this aent.
93
     *
94
     * @return string
95
     * @throws MissingEnvironmentVariableException
96
     */
97
    public static function getImage(): string
98
    {
99
        return self::getOrFail('PHEROMONE_IMAGE_NAME');
100
    }
101
102
    /**
103
     * The container ID which has started this aent or null.
104
     *
105
     * @return null|string
106
     */
107
    public static function getOriginContainer(): ?string
108
    {
109
        return self::getOrNull('PHEROMONE_FROM_CONTAINER_ID');
110
    }
111
112
    /**
113
     * The key from the manifest if this aent has been installed or null.
114
     *
115
     * @return null|string
116
     */
117
    public static function getKey(): ?string
118
    {
119
        return self::getOrNull('PHEROMONE_KEY');
120
    }
121
122
    /**
123
     * The metadata or null.
124
     *
125
     * @param string $key the key on which this aent stored the metadata.
126
     * @return null|string
127
     */
128
    public static function getMetadata(string $key): ?string
129
    {
130
        return self::getOrNull('PHEROMONE_METADATA_' . strtoupper($key));
131
    }
132
133
    /**
134
     * The key from the manifest of the dependency or null.
135
     *
136
     * @param string $key the key on which this aent stored the dependency.
137
     * @return null|string
138
     */
139
    public static function getDependency(string $key): ?string
140
    {
141
        return self::getOrNull('PHEROMONE_DEPENDENCY_' . strtoupper($key));
142
    }
143
144
    /**
145
     * Returns the content of the manifest.
146
     *
147
     * @return mixed[]
148
     * @throws MissingEnvironmentVariableException
149
     */
150
    public static function getAenthillManifestContent(): array
151
    {
152
        $containerProjectDir = self::getContainerProjectDirectory();
153
        $aenthillJSONstr = file_get_contents($containerProjectDir . '/aenthill.json');
154
        if ($aenthillJSONstr === false) {
155
            throw new \RuntimeException('Failed to load the aenthill manifest file ' . $containerProjectDir . '/aenthill.json');
156
        }
157
        return \GuzzleHttp\json_decode($aenthillJSONstr, true);
158
    }
159
}
160