Completed
Pull Request — master (#59)
by Julien
02:49
created

AbstractJsonEventCommand::executeEvent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheAentMachine\Command;
5
6
/**
7
 * Events that have JSON payloads should extend this class.
8
 */
9
abstract class AbstractJsonEventCommand extends AbstractEventCommand
10
{
11
    /**
12
     * @param mixed[] $payload
13
     * @return mixed[]|null
14
     */
15
    abstract protected function executeJsonEvent(array $payload): ?array;
16
17
    protected function executeEvent(?string $payload): ?string
18
    {
19
        if ($payload === null) {
20
            throw new \InvalidArgumentException('Empty payload. JSON message expected.');
21
        }
22
        $data = \GuzzleHttp\json_decode($payload, true);
23
        $result = $this->executeJsonEvent($data);
24
        if ($result === null) {
25
            return null;
26
        }
27
        return \GuzzleHttp\json_encode($result);
28
    }
29
}
30