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

AbstractJsonEventCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A executeEvent() 0 11 3
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