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