Completed
Pull Request — master (#31)
by Jindun
02:18
created

Hermes::aentExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace TheAentMachine;
3
4
use Symfony\Component\Process\Process;
5
6
class Hermes
7
{
8
    /**
9
     * @param string $event
10
     * @param null|string $payload
11
     * @return string[] The array of replies received from all Aents that replied.
12
     */
13
    public static function dispatch(string $event, ?string $payload = null): array
14
    {
15
        $replyAggregator = new ReplyAggregator();
16
        $replyAggregator->clear();
17
18
        $command = ['hermes', 'dispatch', $event];
19
        if (!empty($payload)) {
20
            $command[] = $payload;
21
        }
22
23
        $process = new Process($command);
24
        $process->enableOutput();
25
        $process->setTty(true);
26
27
        $process->mustRun();
28
29
        return $replyAggregator->getReplies();
30
    }
31
32
    /**
33
     * @param mixed[]|object $payload
34
     * @return mixed[]
35
     */
36
    public static function dispatchJson(string $event, $payload): array
37
    {
38
        if (\is_object($payload) && !$payload instanceof \JsonSerializable) {
39
            throw new \RuntimeException('Payload object should implement JsonSerializable. Got an instance of '.\get_class($payload));
40
        }
41
        $replies = self::dispatch($event, \GuzzleHttp\json_encode($payload));
42
43
        return \array_map(function (string $reply) {
44
            return \GuzzleHttp\json_decode($reply, true);
45
        }, $replies);
46
    }
47
48
    public static function reply(string $event, ?string $payload = null): void
49
    {
50
        $command = ['hermes', 'reply', $event];
51
        if (!empty($payload)) {
52
            $command[] = $payload;
53
        }
54
55
        $process = new Process($command);
56
        $process->enableOutput();
57
        $process->setTty(true);
58
59
        $process->mustRun();
60
    }
61
62
    /**
63
     * @param mixed[] $payload
64
     */
65
    public static function replyJson(string $event, array $payload): void
66
    {
67
        self::reply($event, \GuzzleHttp\json_encode($payload));
68
    }
69
70
    /**
71
     * @param string[] $images
72
     * @throws \Symfony\Component\Process\Exception\ProcessFailedException
73
     */
74
    public static function setDependencies(array $images): void
75
    {
76
        $command = ['hermes', 'set:dependencies'];
77
        foreach ($images as $image) {
78
            $command[] = $image;
79
        }
80
81
        $process = new Process($command);
82
        $process->enableOutput();
83
        $process->setTty(true);
84
85
        $process->mustRun();
86
    }
87
88
    /**
89
     * @param string[] $events
90
     * @throws \Symfony\Component\Process\Exception\ProcessFailedException
91
     */
92
    public static function setHandledEvents(array $events): void
93
    {
94
        if (self::aentExists(getenv(Pheromone::getWhoAmI()))){
95
            $command = ['hermes', 'set:handled-events'];
96
            foreach ($events as $event) {
97
                $command[] = $event;
98
            }
99
100
            $process = new Process($command);
101
            $process->enableOutput();
102
            $process->setTty(true);
103
104
            $process->mustRun();
105
        }
106
    }
107
    
108
    /**
109
     * @param string $handledEvent
110
     * @return string[]
111
     */
112
    public static function findAentsByHandledEvent(string $handledEvent): array
113
    {
114
        $manifest = Pheromone::getAenthillManifestContent();
115
116
        $aents = array();
117
        if (isset($manifest['aents'])) {
118
            foreach ($manifest['aents'] as $aent) {
119
                if (array_key_exists('handled_events', $aent) && \in_array($handledEvent, $aent['handled_events'], true)) {
120
                    $aents[] = $aent;
121
                }
122
            }
123
        }
124
        return $aents;
125
    }
126
127
    /**
128
     * @param string $imageName
129
     * @return bool
130
     */
131
    public static function aentExists(string $imageName): bool
132
    {
133
        $manifest = Pheromone::getAenthillManifestContent();
134
135
        return isset($manifest['aents']) && \in_array($imageName, $manifest['aents'], true);
136
    }
137
138
    /**
139
     * Returns true if one of the aents installed can explicitly handle events of type $handledEvent
140
     *
141
     * @param string $handledEvent
142
     * @return bool
143
     */
144
    public static function canHandleEvent(string $handledEvent): bool
145
    {
146
        return count(self::findAentsByHandledEvent($handledEvent)) > 0;
147
    }
148
}
149