1 | <?php declare(strict_types=1); |
||
11 | class Results |
||
12 | { |
||
13 | private EventFactory $eventFactory; |
||
|
|||
14 | |||
15 | private LoggerInterface $logger; |
||
16 | |||
17 | private array $events = []; |
||
18 | |||
19 | 8 | public function __construct(EventFactory $eventFactory, LoggerInterface $logger) |
|
20 | { |
||
21 | 8 | $this->eventFactory = $eventFactory; |
|
22 | 8 | $this->logger = $logger; |
|
23 | } |
||
24 | |||
25 | 6 | public function appendResponse(string $eventNamespace, Response $response): \Generator |
|
26 | { |
||
27 | try { |
||
28 | 6 | $bufferedResponse = yield $response->getBody()->buffer(); |
|
29 | |||
30 | 6 | $events = \json_try_decode($bufferedResponse, true); |
|
31 | 1 | } catch (DecodeErrorException $e) { |
|
32 | 1 | $this->logger->emergency('Failed to decode response body as JSON', ['exception' => $e]); |
|
33 | |||
34 | 1 | throw new DecodingFailedException('Failed to decode response body as JSON', $e->getCode(), $e); |
|
35 | } |
||
36 | |||
37 | 5 | foreach ($events as $event) { |
|
38 | 5 | $this->appendEvent($eventNamespace, $event); |
|
39 | } |
||
40 | } |
||
41 | |||
42 | 5 | private function appendEvent(string $eventNamespace, array $event): void |
|
43 | { |
||
44 | try { |
||
45 | 5 | $this->events[] = $this->eventFactory->build($eventNamespace, $event); |
|
46 | 2 | } catch (UnknownEventException $e) { |
|
47 | //$this->logger->debug('Unknown event encountered', ['exception' => $e]); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | 2 | public function hasEvents(): bool |
|
52 | { |
||
53 | 2 | return (bool) \count($this->events); |
|
54 | } |
||
55 | |||
56 | 2 | public function jsonEncode(): string |
|
57 | { |
||
58 | 2 | $events = []; |
|
59 | |||
60 | 2 | foreach ($this->events as $event) { |
|
61 | 1 | $events[] = $event->getAsArray(); |
|
62 | } |
||
63 | |||
64 | 2 | return \json_encode($events); |
|
65 | } |
||
67 |