Completed
Pull Request — master (#24)
by Julien
02:38
created

Aenthill::runJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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