1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ApiClients\Client\Travis; |
5
|
|
|
|
6
|
|
|
use ApiClients\Client\Travis\Resource\RepositoryInterface; |
7
|
|
|
use ApiClients\Client\Travis\Resource\SSHKeyInterface; |
8
|
|
|
use ApiClients\Client\Travis\Resource\UserInterface; |
9
|
|
|
use ApiClients\Foundation\Factory; |
10
|
|
|
use ApiClients\Foundation\Resource\ResourceInterface; |
11
|
|
|
use React\EventLoop\Factory as LoopFactory; |
12
|
|
|
use React\EventLoop\LoopInterface; |
13
|
|
|
use Rx\React\Promise; |
14
|
|
|
use function ApiClients\Tools\Rx\setAsyncScheduler; |
15
|
|
|
use function Clue\React\Block\await; |
16
|
|
|
|
17
|
|
|
final class Client implements ClientInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var LoopInterface |
21
|
|
|
*/ |
22
|
|
|
private $loop; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var AsyncClientInterface |
26
|
|
|
*/ |
27
|
|
|
private $asyncClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param LoopInterface $loop |
31
|
|
|
* @param AsyncClientInterface $asyncClient |
32
|
|
|
*/ |
33
|
10 |
|
private function __construct(LoopInterface $loop, AsyncClientInterface $asyncClient) |
34
|
|
|
{ |
35
|
10 |
|
$this->loop = $loop; |
36
|
10 |
|
$this->asyncClient = $asyncClient; |
37
|
10 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new AsyncClient based on the loop and other options pass. |
41
|
|
|
* |
42
|
|
|
* @param string $token Instructions to fetch the token: https://blog.travis-ci.com/2013-01-28-token-token-token/ |
43
|
|
|
* @param array $options |
44
|
|
|
* @return Client |
45
|
|
|
*/ |
46
|
1 |
|
public static function create( |
47
|
|
|
string $token = '', |
48
|
|
|
array $options = [] |
49
|
|
|
): self { |
50
|
1 |
|
$loop = LoopFactory::create(); |
51
|
1 |
|
$options = ApiSettings::getOptions($token, 'Sync', $options); |
52
|
1 |
|
$client = Factory::create($loop, $options); |
53
|
1 |
|
setAsyncScheduler($loop); |
54
|
1 |
|
$asyncClient = AsyncClient::createFromClient($client); |
55
|
|
|
|
56
|
1 |
|
return self::createFromClient($loop, $asyncClient); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create an Client from a AsyncClientInterface. |
61
|
|
|
* Be sure to pass in a client with the options from ApiSettings and the Sync namespace suffix, |
62
|
|
|
* and pass in the same loop as associated with the AsyncClient you're passing in. |
63
|
|
|
* |
64
|
|
|
* @param LoopInterface $loop |
65
|
|
|
* @param AsyncClientInterface $asyncClient |
66
|
|
|
* @return Client |
67
|
|
|
*/ |
68
|
10 |
|
public static function createFromClient(LoopInterface $loop, AsyncClientInterface $asyncClient): self |
69
|
|
|
{ |
70
|
10 |
|
return new self($loop, $asyncClient); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function hydrate(string $resource): ResourceInterface |
74
|
|
|
{ |
75
|
|
|
return await( |
76
|
|
|
$this->asyncClient->hydrate($resource), |
77
|
|
|
$this->loop |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function extract(ResourceInterface $resource): string |
82
|
|
|
{ |
83
|
|
|
return await( |
84
|
|
|
$this->asyncClient->extract($resource), |
85
|
|
|
$this->loop |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function repository(string $repository): RepositoryInterface |
93
|
|
|
{ |
94
|
1 |
|
return await( |
95
|
1 |
|
$this->asyncClient->repository($repository), |
96
|
1 |
|
$this->loop |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
1 |
|
public function user(): UserInterface |
104
|
|
|
{ |
105
|
1 |
|
return await( |
106
|
1 |
|
$this->asyncClient->user(), |
107
|
1 |
|
$this->loop |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
1 |
|
public function sshKey(int $id): SSHKeyInterface |
115
|
|
|
{ |
116
|
1 |
|
return await( |
117
|
1 |
|
$this->asyncClient->sshKey($id), |
118
|
1 |
|
$this->loop |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
1 |
|
public function hooks(): array |
126
|
|
|
{ |
127
|
1 |
|
return await( |
128
|
1 |
|
Promise::fromObservable( |
129
|
1 |
|
$this->asyncClient->hooks()->toArray() |
130
|
|
|
), |
131
|
1 |
|
$this->loop |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Warning this a intensive operation as it has to make a call for each hook |
137
|
|
|
* to turn it into a Repository!!! |
138
|
|
|
* |
139
|
|
|
* Take a look at examples/repositories-async.php on how to limit the amount of |
140
|
|
|
* concurrent requests. |
141
|
|
|
* |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
2 |
|
public function repositories(callable $filter = null): array |
145
|
|
|
{ |
146
|
2 |
|
return await( |
147
|
2 |
|
Promise::fromObservable( |
148
|
2 |
|
$this->asyncClient->repositories($filter)->toArray() |
149
|
|
|
), |
150
|
2 |
|
$this->loop |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
1 |
|
public function accounts(): array |
158
|
|
|
{ |
159
|
1 |
|
return await( |
160
|
1 |
|
Promise::fromObservable( |
161
|
1 |
|
$this->asyncClient->accounts()->toArray() |
162
|
|
|
), |
163
|
1 |
|
$this->loop |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
1 |
|
public function broadcasts(): array |
171
|
|
|
{ |
172
|
1 |
|
return await( |
173
|
1 |
|
Promise::fromObservable( |
174
|
1 |
|
$this->asyncClient->broadcasts()->toArray() |
175
|
|
|
), |
176
|
1 |
|
$this->loop |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|