1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\Resource\UserInterface; |
6
|
|
|
use ApiClients\Foundation\Factory as FoundationClientFactory; |
7
|
|
|
use ApiClients\Foundation\Options; |
8
|
|
|
use React\EventLoop\Factory; |
9
|
|
|
use React\EventLoop\LoopInterface; |
10
|
|
|
use function Clue\React\Block\await; |
11
|
|
|
use Rx\Scheduler; |
12
|
|
|
|
13
|
|
|
final class Client implements ClientInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var LoopInterface |
17
|
|
|
*/ |
18
|
|
|
private $loop; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AsyncClient |
22
|
|
|
*/ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param AuthenticationInterface $auth |
27
|
|
|
* @param array $options |
28
|
|
|
* @return Client |
29
|
|
|
*/ |
30
|
1 |
|
public static function create( |
31
|
|
|
AuthenticationInterface $auth, |
32
|
|
|
array $options = [] |
33
|
|
|
): self { |
34
|
1 |
|
$loop = Factory::create(); |
35
|
1 |
|
$options = ApiSettings::getOptions($auth, $options, 'Sync'); |
36
|
1 |
|
$rateLimitState = new RateLimitState(); |
37
|
1 |
|
$options[Options::CONTAINER_DEFINITIONS][RateLimitState::class] = $rateLimitState; |
38
|
1 |
|
$client = FoundationClientFactory::create($loop, $options); |
39
|
|
|
|
40
|
|
|
try { |
41
|
1 |
|
Scheduler::setAsyncFactory(function () use ($loop) { |
42
|
|
|
return new Scheduler\EventLoopScheduler($loop); |
43
|
1 |
|
}); |
44
|
|
|
} catch (\Throwable $t) { |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$asyncClient = AsyncClient::createFromClient($client, $rateLimitState); |
48
|
|
|
|
49
|
1 |
|
return new self($loop, $asyncClient); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param LoopInterface $loop |
54
|
|
|
* @param AsyncClient $client |
55
|
|
|
*/ |
56
|
1 |
|
private function __construct(LoopInterface $loop, AsyncClient $client) |
57
|
|
|
{ |
58
|
1 |
|
$this->loop = $loop; |
59
|
1 |
|
$this->client = $client; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $user |
64
|
|
|
* @return UserInterface |
65
|
|
|
*/ |
66
|
|
|
public function user(string $user): UserInterface |
67
|
|
|
{ |
68
|
|
|
return await( |
69
|
|
|
$this->client->user($user), |
70
|
|
|
$this->loop |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return RateLimitState |
76
|
|
|
*/ |
77
|
1 |
|
public function getRateLimitState(): RateLimitState |
78
|
|
|
{ |
79
|
1 |
|
return $this->client->getRateLimitState(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|