GitHub   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 33
c 1
b 0
f 0
dl 0
loc 73
ccs 30
cts 30
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A listen() 0 6 1
A getAuthHeader() 0 5 1
A request() 0 29 3
1
<?php declare(strict_types=1);
2
3
namespace ekinhbayar\GitAmp\Provider;
4
5
use Amp\Http\Client\DelegateHttpClient;
6
use Amp\Http\Client\Request;
7
use Amp\Http\Client\Response;
8
use Amp\NullCancellationToken;
9
use Amp\Promise;
10
use ekinhbayar\GitAmp\Exception\RequestFailed;
11
use ekinhbayar\GitAmp\Github\Credentials;
12
use ekinhbayar\GitAmp\Response\Factory;
13
use Psr\Log\LoggerInterface;
14
use function Amp\call;
15
16
class GitHub implements Listener
17
{
18
    private const EVENT_NAMESPACE = 'ekinhbayar\GitAmp\Event\GitHub';
19
20
    private const API_ENDPOINT = 'https://api.github.com/events';
21
    private const API_VERSION  = 'v3';
22
23
    private DelegateHttpClient $client;
24
25
    private Credentials $credentials;
26
27
    private Factory $resultFactory;
28
29
    private LoggerInterface $logger;
30
31 5
    public function __construct(
32
        DelegateHttpClient $client,
33
        Credentials $credentials,
34
        Factory $resultFactory,
35
        LoggerInterface $logger
36
    ) {
37 5
        $this->client        = $client;
38 5
        $this->credentials   = $credentials;
39 5
        $this->resultFactory = $resultFactory;
40 5
        $this->logger        = $logger;
41 5
    }
42
43 5
    private function request(): Promise
44
    {
45 5
        return call(function () {
46
            try {
47 5
                $request = new Request(self::API_ENDPOINT, 'GET');
48
49 5
                $request->setHeaders($this->getAuthHeader());
50
51 5
                $response = yield $this->client->request($request, new NullCancellationToken());
52 1
            } catch (\Throwable $e) {
53 1
                $this->logger->error('Failed to send GET request to API endpoint', ['exception' => $e]);
54
55 1
                throw new RequestFailed('Failed to send GET request to API endpoint', $e->getCode(), $e);
56
            }
57
58
            /** @var Response $result */
59 4
            if ($response->getStatus() !== 200) {
60 3
                $message = \sprintf(
61
                    'A non-200 response status (%s - %s) was encountered',
62 3
                    $response->getStatus(),
63 3
                    $response->getReason(),
64
                );
65
66 3
                $this->logger->critical($message, ['response' => $response]);
67
68 3
                throw new RequestFailed($message);
69
            }
70
71 1
            return $response;
72 5
        });
73
    }
74
75 5
    public function listen(): Promise
76
    {
77 5
        return call(function () {
78 5
            $response = yield $this->request();
79
80 1
            return yield $this->resultFactory->buildFromResponse(self::EVENT_NAMESPACE, $response);
81 5
        });
82
    }
83
84 5
    private function getAuthHeader(): array
85
    {
86
        return [
87 5
            'Accept'        => \sprintf('application/vnd.github.%s+json', self::API_VERSION),
88 5
            'Authorization' => \sprintf('Bearer %s', $this->credentials->getAuthenticationString()),
89
        ];
90
    }
91
}
92