Passed
Pull Request — master (#41)
by Julien
03:30
created

Manifest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 20

8 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 7 2
A getDependency() 0 11 6
A getMetadata() 0 11 6
A __construct() 0 4 1
A setEvents() 0 3 1
A addDependency() 0 3 1
A addMetadata() 0 3 1
A __call() 0 6 2
1
<?php
2
3
4
namespace TheAentMachine;
5
6
/**
7
 * Utility class to access the manifest data.
8
 */
9
class Manifest
10
{
11
    /** @var string */
12
    private $filePath;
13
14
    /** @var array|null */
15
    private $content;
16
17
    /**
18
     * Manifest constructor.
19
     */
20
    public function __construct()
21
    {
22
        $containerProjectDir = Pheromone::getContainerProjectDirectory();
23
        $this->filePath = $containerProjectDir . '/aenthill.json';
24
    }
25
26
    public function __call($method, $arguments)
27
    {
28
        if (method_exists($this, $method)) {
29
            $this->parse();
30
            call_user_func([$this, $method], $arguments);
31
            $this->parse();
32
        }
33
    }
34
35
    private function parse(): void
36
    {
37
        $str = file_get_contents($this->filePath);
38
        if ($str === false) {
39
            throw new \RuntimeException('Failed to load the aenthill manifest file ' . $this->filePath);
40
        }
41
        $this->content = \GuzzleHttp\json_decode($str, true);
42
    }
43
44
    public function setEvents(array $events): void
45
    {
46
        Aenthill::update(null, $events);
47
    }
48
49
    public function addMetadata(string $key, string $value): void
50
    {
51
        Aenthill::update([$key => $value]);
52
    }
53
54
    /**
55
     * @param string $key
56
     * @return null|string
57
     * @throws Exception\MissingEnvironmentVariableException
58
     */
59
    public function getMetadata(string $key): ?string
60
    {
61
        $aentID = Pheromone::getKey();
62
        if (isset($this->content['aents'])) {
63
            foreach ($this->content['aents'] as $ID => $aent) {
64
                if ($ID === $aentID && array_key_exists('metadata', $aent) && array_key_exists($key, $aent['metadata'])) {
65
                    return $aent['metadata'][$key];
66
                }
67
            }
68
        }
69
        return null;
70
    }
71
72
    /**
73
     * @param string $image
74
     * @param string $key
75
     * @param array<string,string>|null $metadata
76
     */
77
    public function addDependency(string $image, string $key, ?array $metadata): void
78
    {
79
        Aenthill::register($image, $key, $metadata);
80
    }
81
82
    /**
83
     * @param string $key
84
     * @return null|string
85
     * @throws Exception\MissingEnvironmentVariableException
86
     */
87
    public function getDependency(string $key): ?string
88
    {
89
        $aentID = Pheromone::getKey();
90
        if (isset($this->content['aents'])) {
91
            foreach ($this->content['aents'] as $ID => $aent) {
92
                if ($ID === $aentID && array_key_exists('dependencies', $aent) && array_key_exists($key, $aent['dependencies'])) {
93
                    return $aent['dependencies'][$key];
94
                }
95
            }
96
        }
97
        return null;
98
    }
99
}
100