Completed
Push — master ( 4e305a...a4de8a )
by Pieter
02:19
created

GitAmp   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 81
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

4 Methods

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