Completed
Pull Request — master (#59)
by Julien
02:49
created

Manifest::mustGetMetadata()   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
8
/**
9
 * Utility class to access the manifest data.
10
 */
11
final class Manifest
12
{
13
    /**
14
     * @param string[] $events
15
     */
16
    public static function setEvents(array $events): void
17
    {
18
        Aenthill::update(null, $events);
19
    }
20
21
    public static function addMetadata(string $key, string $value): void
22
    {
23
        Aenthill::update([$key => $value]);
24
    }
25
26
    /**
27
     * @param string $key
28
     * @return string
29
     * @throws ManifestException
30
     */
31
    public static function mustGetMetadata(string $key): string
32
    {
33
        try {
34
            return Aenthill::metadata($key);
35
        } catch (\Exception $e) {
36
            throw ManifestException::missingMetadata($key, $e);
37
        }
38
    }
39
40
    /**
41
     * @param string $key
42
     * @return null|string
43
     */
44
    public static function getMetadata(string $key): ?string
45
    {
46
        try {
47
            return self::mustGetMetadata($key);
48
        } catch (ManifestException $e) {
49
            return null;
50
        }
51
    }
52
53
    /**
54
     * @param string $image
55
     * @param string $key
56
     * @param array<string,string>|null $metadata
57
     */
58
    public static function addDependency(string $image, string $key, ?array $metadata): void
59
    {
60
        Aenthill::register($image, $key, $metadata);
61
    }
62
63
    /**
64
     * @param string $key
65
     * @return string
66
     * @throws ManifestException
67
     */
68
    public static function mustGetDependency(string $key): string
69
    {
70
        try {
71
            return Aenthill::dependency($key);
72
        } catch (\Exception $e) {
73
            throw ManifestException::missingDependency($key, $e);
74
        }
75
    }
76
77
    /**
78
     * @param string $key
79
     * @return null|string
80
     */
81
    public static function getDependency(string $key): ?string
82
    {
83
        try {
84
            return self::mustGetDependency($key);
85
        } catch (ManifestException $e) {
86
            return null;
87
        }
88
    }
89
}
90