Completed
Pull Request — master (#41)
by Julien
03:07
created

Manifest::getDependency()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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