Completed
Push — master ( 84a040...ae5fca )
by Julien
11s
created

Manifest::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheAentMachine;
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
    private static function getFilePath(): string
15
    {
16
        $containerProjectDir = Pheromone::getContainerProjectDirectory();
17
        return $containerProjectDir . '/aenthill.json';
18
    }
19
20
    /**
21
     * @return mixed[]
22
     */
23
    private static function parse(): array
24
    {
25
        $filePath = self::getFilePath();
26
        $str = file_get_contents($filePath);
27
        if ($str === false) {
28
            throw new \RuntimeException('Failed to load the aenthill manifest file ' . $filePath);
29
        }
30
        return \GuzzleHttp\json_decode($str, true);
31
    }
32
33
    /**
34
     * @param string[] $events
35
     */
36
    public static function setEvents(array $events): void
37
    {
38
        Aenthill::update(null, $events);
39
    }
40
41
    public static function addMetadata(string $key, string $value): void
42
    {
43
        Aenthill::update([$key => $value]);
44
    }
45
46
    /**
47
     * @param string $key
48
     * @return string
49
     * @throws MissingEnvironmentVariableException
50
     * @throws ManifestException
51
     */
52
    public static function getMetadata(string $key): string
53
    {
54
        $manifest = self::parse();
55
        $aentID = Pheromone::getKey();
56
        if (isset($manifest['aents'])) {
57
            foreach ($manifest['aents'] as $ID => $aent) {
58
                if ($ID === $aentID && array_key_exists('metadata', $aent) && array_key_exists($key, $aent['metadata'])) {
59
                    return $aent['metadata'][$key];
60
                }
61
            }
62
        }
63
        throw ManifestException::missingMetadata($key);
64
    }
65
66
    /**
67
     * @param string $image
68
     * @param string $key
69
     * @param array<string,string>|null $metadata
70
     */
71
    public static function addDependency(string $image, string $key, ?array $metadata): void
72
    {
73
        Aenthill::register($image, $key, $metadata);
74
    }
75
76
    /**
77
     * @param string $key
78
     * @return string
79
     * @throws MissingEnvironmentVariableException
80
     * @throws ManifestException
81
     */
82
    public static function getDependency(string $key): string
83
    {
84
        $manifest = self::parse();
85
        $aentID = Pheromone::getKey();
86
        if (isset($manifest['aents'])) {
87
            foreach ($manifest['aents'] as $ID => $aent) {
88
                if ($ID === $aentID && array_key_exists('dependencies', $aent) && array_key_exists($key, $aent['dependencies'])) {
89
                    return $aent['dependencies'][$key];
90
                }
91
            }
92
        }
93
        throw ManifestException::missingDependency($key);
94
    }
95
}
96