Passed
Pull Request — master (#11)
by Julien
02:11
created

Hermes::setDependencies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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