Passed
Push — master ( 7cc2b5...b56704 )
by David
04:50
created

Pheromone::getOriginContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheAentMachine;
5
6
use TheAentMachine\Exception\LogLevelException;
7
use TheAentMachine\Exception\MissingEnvironmentVariableException;
8
9
/**
10
 * Utility class to access the Aent configuration settings (stored in environment variables)
11
 */
12
class Pheromone
13
{
14
    private static $levels = [
15
        'DEBUG' => true,
16
        'INFO' => true,
17
        'WARN' => true,
18
        'ERROR' => true,
19
    ];
20
21
    /**
22
     * Returns the log level for this Aent.
23
     *
24
     * @return string
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
    private static function getOrFail(string $variableName): string
42
    {
43
        $value = \getenv($variableName);
44
        if ($value === false) {
45
            throw MissingEnvironmentVariableException::missingEnv($variableName);
46
        }
47
        return $value;
48
    }
49
50
    public static function getWhoAmI(): string
51
    {
52
        return self::getOrFail('PHEROMONE_WHOAMI');
53
    }
54
55
    /**
56
     * The project directory path on the host machine
57
     */
58
    public static function getHostProjectDirectory(): string
59
    {
60
        return self::getOrFail('PHEROMONE_HOST_PROJECT_DIR');
61
    }
62
63
    /**
64
     * The project directory path in the container
65
     */
66
    public static function getContainerProjectDirectory(): string
67
    {
68
        return rtrim(self::getOrFail('PHEROMONE_CONTAINER_PROJECT_DIR'), '/');
69
    }
70
71
    public static function getOriginContainer(): ?string
72
    {
73
        $from = getenv('PHEROMONE_FROM_CONTAINER_ID');
74
        return $from === false ? null : $from;
75
    }
76
77
    public static function getOriginImage(): ?string
78
    {
79
        $from = getenv('PHEROMONE_FROM_IMAGE_NAME');
80
        return $from === false ? null : $from;
81
    }
82
}
83