Passed
Pull Request — master (#4)
by David
01:59
created

Hercule::findAentsByHandledEvent()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 2
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace TheAentMachine;
3
4
use Symfony\Component\Process\Process;
5
6
class Hercule
7
{
8
    /**
9
     * @param string[] $events
10
     * @throws \Symfony\Component\Process\Exception\ProcessFailedException
11
     */
12
    public static function setHandledEvents(array $events): void
13
    {
14
        $command = ['hercule', 'set:handled-events'];
15
        foreach ($events as $event) {
16
            $command[] = $event;
17
        }
18
19
        $process = new Process($command);
20
        $process->enableOutput();
21
        $process->setTty(true);
22
23
        $process->mustRun();
24
    }
25
26
    /**
27
     * @param string $handledEvent
28
     * @return string[]
29
     */
30
    public static function findAentsByHandledEvent(string $handledEvent): array
31
    {
32
        $containerProjectDir = Pheromone::getContainerProjectDirectory();
33
34
        $aenthillJSONstr = file_get_contents($containerProjectDir . '/aenthill.json');
35
        $aenthillJSON = \GuzzleHttp\json_decode($aenthillJSONstr, true);
36
37
        $aents = array();
38
        if (isset($aenthillJSON['aents'])) {
39
            foreach ($aenthillJSON['aents'] as $aent) {
40
                if (array_key_exists('handled_events', $aent) && \in_array($handledEvent, $aent['handled_events'], true)) {
41
                    $aents[] = $aent;
42
                }
43
            }
44
        }
45
        return $aents;
46
    }
47
48
    /**
49
     * Returns true if one of the aents installed can explicitly handle events of type $handledEvent
50
     *
51
     * @param string $handledEvent
52
     * @return bool
53
     */
54
    public static function canHandleEvent(string $handledEvent): bool
55
    {
56
        return count(self::findAentsByHandledEvent($handledEvent)) > 0;
57
    }
58
}
59