Completed
Pull Request — master (#11)
by David
03:19
created

Hermes   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 18

8 Methods

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