Test Setup Failed
Pull Request — master (#46)
by Ekin
06:26
created

Results::appendResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
2
3
namespace ekinhbayar\GitAmp\Response;
4
5
use Amp\Http\Client\Response;
6
use ekinhbayar\GitAmp\Event\Factory as EventFactory;
7
use ekinhbayar\GitAmp\Event\UnknownEventException;
8
use ExceptionalJSON\DecodeErrorException;
9
use Psr\Log\LoggerInterface;
10
11
class Results
12
{
13
    private EventFactory $eventFactory;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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
    }
66
}
67