Completed
Push — master ( 0c8701...84a040 )
by Julien
13s
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;
5
6
use Symfony\Component\Process\Process;
7
use TheAentMachine\Exception\MissingEnvironmentVariableException;
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 addDependency(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
61
    /**
62
     * Starts an aent.
63
     *
64
     * @param string $target the image name or a key from the manifest.
65
     * @param string $event
66
     * @param null|string $payload
67
     */
68
    public static function run(string $target, string $event, ?string $payload = null): void
69
    {
70
        $command = ['aenthill', 'run', $target, $event];
71
        if (!empty($payload)) {
72
            $command[] = $payload;
73
        }
74
        $process = new Process($command);
75
        $process->enableOutput();
76
        $process->setTty(true);
77
        $process->mustRun();
78
    }
79
80
    /**
81
     * @param string $target
82
     * @param string $event
83
     * @param mixed[] $payload
84
     */
85
    public static function runJson(string $target, string $event, array $payload): void
86
    {
87
        self::run($target, $event, \GuzzleHttp\json_encode($payload));
88
    }
89
90
    /**
91
     * Dispatches an event to all aents from the manifest which can handle it.
92
     *
93
     * @param string $event
94
     * @param null|string $payload
95
     * @return string[] the array of replies received from all aents that replied.
96
     */
97
    public static function dispatch(string $event, ?string $payload = null): array
98
    {
99
        $replyAggregator = new ReplyAggregator();
100
        $replyAggregator->clear();
101
        $command = ['aenthill', 'dispatch', $event];
102
        if (!empty($payload)) {
103
            $command[] = $payload;
104
        }
105
        $process = new Process($command);
106
        $process->enableOutput();
107
        $process->setTty(true);
108
        $process->mustRun();
109
        return $replyAggregator->getReplies();
110
    }
111
112
    /**
113
     * @param mixed[]|object $payload
114
     * @return mixed[]
115
     */
116
    public static function dispatchJson(string $event, $payload): array
117
    {
118
        if (\is_object($payload) && !$payload instanceof \JsonSerializable) {
119
            throw new \RuntimeException('Payload object should implement JsonSerializable. Got an instance of ' . \get_class($payload));
120
        }
121
        $replies = self::dispatch($event, \GuzzleHttp\json_encode($payload));
122
        return \array_map(function (string $reply) {
123
            return \GuzzleHttp\json_decode($reply, true);
124
        }, $replies);
125
    }
126
127
    /**
128
     * Replies to the aent which started this aent.
129
     *
130
     * @param string $event
131
     * @param null|string $payload
132
     */
133
    public static function reply(string $event, ?string $payload = null): void
134
    {
135
        $command = ['aenthill', 'reply', $event];
136
        if (!empty($payload)) {
137
            $command[] = $payload;
138
        }
139
        $process = new Process($command);
140
        $process->enableOutput();
141
        $process->setTty(true);
142
        $process->mustRun();
143
    }
144
145
    /**
146
     * @param string $event
147
     * @param mixed[] $payload
148
     */
149
    public static function replyJson(string $event, array $payload): void
150
    {
151
        self::reply($event, \GuzzleHttp\json_encode($payload));
152
    }
153
154
    /**
155
     * Returns the list of aents in the manifest which handles the given event.
156
     *
157
     * @param string $handledEvent
158
     * @return string[]
159
     * @throws MissingEnvironmentVariableException
160
     */
161
    public static function findAentsByHandledEvent(string $handledEvent): array
162
    {
163
        $manifest = Pheromone::getAenthillManifestContent();
164
        $aents = array();
165
        if (isset($manifest['aents'])) {
166
            foreach ($manifest['aents'] as $aent) {
167
                if (array_key_exists('events', $aent) && \in_array($handledEvent, $aent['events'], true)) {
168
                    $aents[] = $aent;
169
                }
170
            }
171
        }
172
        return $aents;
173
    }
174
175
    /**
176
     * Returns true if one of the aents installed can explicitly handle events of type $handledEvent
177
     *
178
     * @param string $handledEvent
179
     * @return bool
180
     * @throws MissingEnvironmentVariableException
181
     */
182
    public static function canHandleEvent(string $handledEvent): bool
183
    {
184
        return count(self::findAentsByHandledEvent($handledEvent)) > 0;
185
    }
186
}
187