Passed
Pull Request — master (#5)
by David
03:40 queued 13s
created

Hercule::addAent()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 1
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
1
<?php
2
namespace TheAentMachine;
3
4
use Symfony\Component\Filesystem\Filesystem;
5
use Symfony\Component\Process\Process;
6
7
class Hercule
8
{
9
    /**
10
     * @param string[] $events
11
     * @throws \Symfony\Component\Process\Exception\ProcessFailedException
12
     */
13
    public static function setHandledEvents(array $events): void
14
    {
15
        $command = ['hercule', 'set:handled-events'];
16
        foreach ($events as $event) {
17
            $command[] = $event;
18
        }
19
20
        $process = new Process($command);
21
        $process->enableOutput();
22
        $process->setTty(true);
23
24
        $process->mustRun();
25
    }
26
27
    /**
28
     * @param string $handledEvent
29
     * @return string[]
30
     */
31
    public static function findAentsByHandledEvent(string $handledEvent): array
32
    {
33
        $containerProjectDir = Pheromone::getContainerProjectDirectory();
34
35
        $aenthillJSONstr = file_get_contents($containerProjectDir . '/aenthill.json');
36
        $aenthillJSON = \GuzzleHttp\json_decode($aenthillJSONstr, true);
37
38
        $aents = array();
39
        if (isset($aenthillJSON['aents'])) {
40
            foreach ($aenthillJSON['aents'] as $aent) {
41
                if (array_key_exists('handled_events', $aent) && \in_array($handledEvent, $aent['handled_events'], true)) {
42
                    $aents[] = $aent;
43
                }
44
            }
45
        }
46
        return $aents;
47
    }
48
49
    /**
50
     * Returns true if one of the aents installed can explicitly handle events of type $handledEvent
51
     *
52
     * @param string $handledEvent
53
     * @return bool
54
     */
55
    public static function canHandleEvent(string $handledEvent): bool
56
    {
57
        return count(self::findAentsByHandledEvent($handledEvent)) > 0;
58
    }
59
60
    /**
61
     * Registers a new Aent in Aenthill
62
     */
63
    public static function addAent(string $aent): void
64
    {
65
        $containerProjectDir = Pheromone::getContainerProjectDirectory();
66
67
        $aenthillJSONstr = file_get_contents($containerProjectDir . '/aenthill.json');
68
        $aenthillJSON = \GuzzleHttp\json_decode($aenthillJSONstr, true);
69
70
        $aents = array();
71
        if (isset($aenthillJSON['aents'])) {
72
            foreach ($aenthillJSON['aents'] as $aentDescriptor) {
73
                if ($aentDescriptor['image'] === $aent) {
74
                    // Aent already there. Let's return.
75
                    return;
76
                }
77
            }
78
        }
79
80
        $aents['aents'][]['image'] = $aent;
81
82
        $filesystem = new Filesystem();
83
        $filesystem->dumpFile($containerProjectDir . '/aenthill.json', \json_encode($aents, \JSON_PRETTY_PRINT));
84
    }
85
}
86