Completed
Pull Request — master (#46)
by Ekin
06:38
created

src/Response/Results.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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