Passed
Push — master ( 657f5c...767802 )
by Ekin
03:11
created

GitHub   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 70
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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