Completed
Pull Request — master (#54)
by Jindun
02:35
created

Aenthill::update()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 4
nop 2
dl 0
loc 19
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheAentMachine\Aenthill;
5
6
use Symfony\Component\Process\Process;
7
use TheAentMachine\ReplyAggregator;
8
9
class Aenthill
10
{
11
    /**
12
     * Updates current aent in the manifest.
13
     *
14
     * @param null|array<string,string> $metadata
15
     * @param null|string[] $events
16
     */
17
    public static function update(?array $metadata = null, ?array $events = null): void
18
    {
19
        $command = ['aenthill', 'update'];
20
        if (!empty($metadata)) {
21
            foreach ($metadata as $key => $value) {
22
                $command[] = '-m';
23
                $command[] = $key . '=' . $value;
24
            }
25
        }
26
        if (!empty($events)) {
27
            foreach ($events as $event) {
28
                $command[] = '-e';
29
                $command[] = $event;
30
            }
31
        }
32
        $process = new Process($command);
33
        $process->enableOutput();
34
        $process->setTty(true);
35
        $process->mustRun();
36
    }
37
38
    /**
39
     * Adds a dependency in manifest to current aent.
40
     *
41
     * @param string $image
42
     * @param string $key
43
     * @param null|array<string,string> $metadata
44
     */
45
    public static function register(string $image, string $key, ?array $metadata = null): void
46
    {
47
        $command = ['aenthill', 'register', $image, $key];
48
        if (!empty($metadata)) {
49
            foreach ($metadata as $k => $value) {
50
                $command[] = '-m';
51
                $command[] = $k . '=' . $value;
52
            }
53
        }
54
        $process = new Process($command);
55
        $process->enableOutput();
56
        $process->setTty(true);
57
        $process->mustRun();
58
    }
59
60
    public static function metadata(string $key): string
61
    {
62
        $command = ['aenthill', 'metadata', $key];
63
        $process = new Process($command);
64
        $process->mustRun();
65
        $output = $process->getOutput();
66
        return substr($output, 0, -1); // remove \n
67
    }
68
69
    public static function dependency(string $key): string
70
    {
71
        $command = ['aenthill', 'dependency', $key];
72
        $process = new Process($command);
73
        $process->mustRun();
74
        $output = $process->getOutput();
75
        return substr($output, 0, -1); // remove \n
76
    }
77
78
    /**
79
     * Starts an aent.
80
     *
81
     * @param string $target the image name or a key from the manifest.
82
     * @param string $event
83
     * @param null|string $payload
84
     * @return string[]
85
     */
86
    public static function run(string $target, string $event, ?string $payload = null): array
87
    {
88
        $replyAggregator = new ReplyAggregator();
89
        $replyAggregator->clear();
90
        $command = ['aenthill', 'run', $target, $event];
91
        if (null !== $payload) {
92
            $command[] = $payload;
93
        }
94
        $process = new Process($command);
95
        $process->enableOutput();
96
        $process->setTty(true);
97
        $process->mustRun();
98
        return $replyAggregator->getReplies();
99
    }
100
101
    /**
102
     * @param string $target
103
     * @param string $event
104
     * @param mixed[] $payload
105
     * @return string[]
106
     */
107
    public static function runJson(string $target, string $event, array $payload): array
108
    {
109
        return self::run($target, $event, \GuzzleHttp\json_encode($payload));
110
    }
111
112
    /**
113
     * Dispatches an event to all aents from the manifest which can handle it.
114
     *
115
     * @param string $event
116
     * @param null|string $payload
117
     * @return string[] the array of replies received from all aents that replied.
118
     */
119
    public static function dispatch(string $event, ?string $payload = null): array
120
    {
121
        $replyAggregator = new ReplyAggregator();
122
        $replyAggregator->clear();
123
        $command = ['aenthill', 'dispatch', $event];
124
        if (null !== $payload) {
125
            $command[] = $payload;
126
        }
127
        $process = new Process($command);
128
        $process->enableOutput();
129
        $process->setTty(true);
130
        $process->mustRun();
131
        return $replyAggregator->getReplies();
132
    }
133
134
    /**
135
     * @param mixed[]|object $payload
136
     * @return mixed[]
137
     */
138
    public static function dispatchJson(string $event, $payload): array
139
    {
140
        if (\is_object($payload) && !$payload instanceof \JsonSerializable) {
141
            throw new \RuntimeException('Payload object should implement JsonSerializable. Got an instance of ' . \get_class($payload));
142
        }
143
        $replies = self::dispatch($event, \GuzzleHttp\json_encode($payload));
144
        return \array_map(function (string $reply) {
145
            return \GuzzleHttp\json_decode($reply, true);
146
        }, $replies);
147
    }
148
149
    /**
150
     * Replies to the aent which started this aent.
151
     *
152
     * @param string $event
153
     * @param null|string $payload
154
     */
155
    public static function reply(string $event, ?string $payload = null): void
156
    {
157
        $command = ['aenthill', 'reply', $event];
158
        if (null !== $payload) {
159
            $command[] = $payload;
160
        }
161
        $process = new Process($command);
162
        $process->enableOutput();
163
        $process->setTty(true);
164
        $process->mustRun();
165
    }
166
167
    /**
168
     * @param string $event
169
     * @param mixed[] $payload
170
     */
171
    public static function replyJson(string $event, array $payload): void
172
    {
173
        self::reply($event, \GuzzleHttp\json_encode($payload));
174
    }
175
}
176