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