Completed
Push — master ( a4b22f...a83e33 )
by Julien
11s
created

Manifest::getDependencyOrNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheAentMachine\Aenthill;
5
6
use TheAentMachine\Exception\ManifestException;
7
use TheAentMachine\Exception\MissingEnvironmentVariableException;
8
9
/**
10
 * Utility class to access the manifest data.
11
 */
12
class Manifest
13
{
14
    /**
15
     * @return string
16
     * @throws MissingEnvironmentVariableException
17
     */
18
    private static function getFilePath(): string
19
    {
20
        $containerProjectDir = Pheromone::getContainerProjectDirectory();
21
        return $containerProjectDir . '/aenthill.json';
22
    }
23
24
    /**
25
     * @return mixed[]
26
     * @throws MissingEnvironmentVariableException
27
     */
28
    private static function parse(): array
29
    {
30
        $filePath = self::getFilePath();
31
        $str = file_get_contents($filePath);
32
        if ($str === false) {
33
            throw new \RuntimeException('Failed to load the aenthill manifest file ' . $filePath);
34
        }
35
        return \GuzzleHttp\json_decode($str, true);
36
    }
37
38
    /**
39
     * @param string[] $events
40
     */
41
    public static function setEvents(array $events): void
42
    {
43
        Aenthill::update(null, $events);
44
    }
45
46
    public static function addMetadata(string $key, string $value): void
47
    {
48
        Aenthill::update([$key => $value]);
49
    }
50
51
    /**
52
     * @param string $key
53
     * @return string
54
     * @throws MissingEnvironmentVariableException
55
     * @throws ManifestException
56
     */
57
    public static function getMetadata(string $key): string
58
    {
59
        $manifest = self::parse();
60
        $aentID = Pheromone::getID();
61
        if (isset($manifest['aents'])) {
62
            foreach ($manifest['aents'] as $ID => $aent) {
63
                if ($ID === $aentID && array_key_exists('metadata', $aent) && array_key_exists($key, $aent['metadata'])) {
64
                    return $aent['metadata'][$key];
65
                }
66
            }
67
        }
68
        throw ManifestException::missingMetadata($key);
69
    }
70
71
    /**
72
     * @param string $key
73
     * @return null|string
74
     * @throws MissingEnvironmentVariableException
75
     */
76
    public static function getMetadataOrNull(string $key): ?string
77
    {
78
        try {
79
            return self::getMetadata($key);
80
        } catch (ManifestException $e) {
81
            return null;
82
        }
83
    }
84
85
    /**
86
     * @param string $image
87
     * @param string $key
88
     * @param array<string,string>|null $metadata
89
     */
90
    public static function addDependency(string $image, string $key, ?array $metadata): void
91
    {
92
        Aenthill::register($image, $key, $metadata);
93
    }
94
95
    /**
96
     * @param string $key
97
     * @return string
98
     * @throws MissingEnvironmentVariableException
99
     * @throws ManifestException
100
     */
101
    public static function getDependency(string $key): string
102
    {
103
        $manifest = self::parse();
104
        $aentID = Pheromone::getID();
105
        if (isset($manifest['aents'])) {
106
            foreach ($manifest['aents'] as $ID => $aent) {
107
                if ($ID === $aentID && array_key_exists('dependencies', $aent) && array_key_exists($key, $aent['dependencies'])) {
108
                    return $aent['dependencies'][$key];
109
                }
110
            }
111
        }
112
        throw ManifestException::missingDependency($key);
113
    }
114
115
116
    /**
117
     * @param string $key
118
     * @return null|string
119
     * @throws MissingEnvironmentVariableException
120
     */
121
    public static function getDependencyOrNull(string $key): ?string
122
    {
123
        try {
124
            return self::getDependency($key);
125
        } catch (ManifestException $e) {
126
            return null;
127
        }
128
    }
129
}
130