Completed
Push — master ( ae5fca...d9758b )
by Julien
13s
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\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::getKey();
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 $image
73
     * @param string $key
74
     * @param array<string,string>|null $metadata
75
     */
76
    public static function addDependency(string $image, string $key, ?array $metadata): void
77
    {
78
        Aenthill::register($image, $key, $metadata);
79
    }
80
81
    /**
82
     * @param string $key
83
     * @return string
84
     * @throws MissingEnvironmentVariableException
85
     * @throws ManifestException
86
     */
87
    public static function getDependency(string $key): string
88
    {
89
        $manifest = self::parse();
90
        $aentID = Pheromone::getKey();
91
        if (isset($manifest['aents'])) {
92
            foreach ($manifest['aents'] as $ID => $aent) {
93
                if ($ID === $aentID && array_key_exists('dependencies', $aent) && array_key_exists($key, $aent['dependencies'])) {
94
                    return $aent['dependencies'][$key];
95
                }
96
            }
97
        }
98
        throw ManifestException::missingDependency($key);
99
    }
100
}
101