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
|
|
|
*/ |
32
|
10 |
|
private function __construct(LoopInterface $loop, AsyncClientInterface $asyncClient) |
33
|
|
|
{ |
34
|
10 |
|
$this->loop = $loop; |
35
|
10 |
|
$this->asyncClient = $asyncClient; |
36
|
10 |
|
} |
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
|
|
|
*/ |
45
|
1 |
|
public static function create( |
46
|
|
|
string $token = '', |
47
|
|
|
array $options = [] |
48
|
|
|
): self { |
49
|
1 |
|
$loop = LoopFactory::create(); |
50
|
1 |
|
$options = ApiSettings::getOptions($token, 'Sync', $options); |
51
|
1 |
|
$client = Factory::create($loop, $options); |
52
|
1 |
|
$asyncClient = AsyncClient::createFromClient($client); |
53
|
|
|
|
54
|
1 |
|
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
|
|
|
*/ |
66
|
10 |
|
public static function createFromClient(LoopInterface $loop, AsyncClientInterface $asyncClient): self |
67
|
|
|
{ |
68
|
10 |
|
return new self($loop, $asyncClient); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function hydrate(string $resource): ResourceInterface |
72
|
|
|
{ |
73
|
|
|
return await( |
74
|
|
|
$this->asyncClient->hydrate($resource), |
75
|
|
|
$this->loop |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function extract(ResourceInterface $resource): string |
80
|
|
|
{ |
81
|
|
|
return await( |
82
|
|
|
$this->asyncClient->extract($resource), |
83
|
|
|
$this->loop |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function repository(string $repository): RepositoryInterface |
91
|
|
|
{ |
92
|
1 |
|
return await( |
93
|
1 |
|
$this->asyncClient->repository($repository), |
94
|
1 |
|
$this->loop |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
1 |
|
public function user(): UserInterface |
102
|
|
|
{ |
103
|
1 |
|
return await( |
104
|
1 |
|
$this->asyncClient->user(), |
105
|
1 |
|
$this->loop |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function sshKey(int $id): SSHKeyInterface |
113
|
|
|
{ |
114
|
1 |
|
return await( |
115
|
1 |
|
$this->asyncClient->sshKey($id), |
116
|
1 |
|
$this->loop |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
1 |
|
public function hooks(): array |
124
|
|
|
{ |
125
|
1 |
|
return await( |
126
|
1 |
|
Promise::fromObservable( |
127
|
1 |
|
$this->asyncClient->hooks()->toArray() |
128
|
|
|
), |
129
|
1 |
|
$this->loop |
130
|
|
|
); |
131
|
|
|
} |
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
|
|
|
* concurrent requests. |
139
|
|
|
* |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
2 |
|
public function repositories(callable $filter = null): array |
143
|
|
|
{ |
144
|
2 |
|
return await( |
145
|
2 |
|
Promise::fromObservable( |
146
|
2 |
|
$this->asyncClient->repositories($filter)->toArray() |
147
|
|
|
), |
148
|
2 |
|
$this->loop |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
1 |
|
public function accounts(): array |
156
|
|
|
{ |
157
|
1 |
|
return await( |
158
|
1 |
|
Promise::fromObservable( |
159
|
1 |
|
$this->asyncClient->accounts()->toArray() |
160
|
|
|
), |
161
|
1 |
|
$this->loop |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
1 |
|
public function broadcasts(): array |
169
|
|
|
{ |
170
|
1 |
|
return await( |
171
|
1 |
|
Promise::fromObservable( |
172
|
1 |
|
$this->asyncClient->broadcasts()->toArray() |
173
|
|
|
), |
174
|
1 |
|
$this->loop |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|