Completed
Push — master ( a1068d...c410a5 )
by David
10s
created

Hermes::aentExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0

1 Method

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