|
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 |
|
$this->checkForRequestErrors($error, $result); |
|
56
|
3 |
|
}); |
|
57
|
|
|
|
|
58
|
2 |
|
return $promise; |
|
59
|
|
|
|
|
60
|
2 |
|
} catch (ClientException $e) { |
|
61
|
1 |
|
throw new RequestFailedException('Failed to send GET request to API endpoint', $e->getCode(), $e); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
private function checkForRequestErrors($error, $result) |
|
66
|
|
|
{ |
|
67
|
3 |
|
if ($error) { |
|
68
|
|
|
$this->logger->error('Call to webservice failed', ['error' => $error]); |
|
69
|
|
|
|
|
70
|
|
|
throw new RequestFailedException('Call to webservice failed'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @var Response $result */ |
|
74
|
3 |
|
if ($result->getStatus() !== 200) { |
|
75
|
1 |
|
$message = sprintf( |
|
76
|
1 |
|
'A non-200 response status (%s - %s) was encountered', |
|
77
|
1 |
|
$result->getStatus(), |
|
78
|
1 |
|
$result->getReason() |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
1 |
|
$this->logger->critical($message, ['result' => $result]); |
|
82
|
|
|
|
|
83
|
1 |
|
throw new RequestFailedException($message); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function listen(): Promise |
|
88
|
|
|
{ |
|
89
|
4 |
|
return \Amp\resolve(function() { |
|
90
|
4 |
|
$response = yield $this->request(); |
|
91
|
|
|
|
|
92
|
2 |
|
return new Success($this->resultFactory->build($response)); |
|
93
|
4 |
|
}); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
private function getAuthHeader(): array |
|
97
|
|
|
{ |
|
98
|
4 |
|
return ['Authorization' => sprintf('Basic %s', $this->credentials->getAuthenticationString())]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|