Completed
Push — master ( ae5fca...d9758b )
by Julien
13s
created

JsonEventCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A executeEvent() 0 16 4
1
<?php
2
3
4
namespace TheAentMachine\Command;
5
6
/**
7
 * Events that have JSON payloads should extend this class.
8
 */
9
abstract class JsonEventCommand extends EventCommand
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 = \json_decode($payload, true);
23
        if (JSON_ERROR_NONE !== json_last_error()) {
24
            throw new \InvalidArgumentException(
25
                'json_decode error: ' . json_last_error_msg()
26
            );
27
        }
28
        $result = $this->executeJsonEvent($data);
29
        if ($result === null) {
30
            return null;
31
        }
32
        return \GuzzleHttp\json_encode($result);
33
    }
34
}
35