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 |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var LoopInterface |
20
|
|
|
*/ |
21
|
|
|
private $loop; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var AsyncClient |
25
|
|
|
*/ |
26
|
|
|
private $asyncClient; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $token |
30
|
|
|
* @param array $options |
31
|
|
|
* @return Client |
32
|
|
|
*/ |
33
|
|
|
public static function create( |
34
|
|
|
string $token = '', |
35
|
|
|
array $options = [] |
36
|
|
|
): self { |
37
|
|
|
$loop = LoopFactory::create(); |
38
|
|
|
$options = ApiSettings::getOptions($token, 'Sync', $options); |
39
|
|
|
$client = Factory::create($loop, $options); |
40
|
|
|
$asyncClient = AsyncClient::createFromClient($client); |
41
|
|
|
return self::createFromClient($loop, $asyncClient); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param LoopInterface $loop |
46
|
|
|
* @param AsyncClient $asyncClient |
47
|
|
|
* @return Client |
48
|
|
|
*/ |
49
|
|
|
public static function createFromClient(LoopInterface $loop, AsyncClient $asyncClient): self |
50
|
|
|
{ |
51
|
|
|
return new self($loop, $asyncClient); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Client constructor. |
56
|
|
|
* @param LoopInterface $loop |
57
|
|
|
* @param AsyncClient $asyncClient |
58
|
|
|
*/ |
59
|
|
|
private function __construct(LoopInterface $loop, AsyncClient $asyncClient) |
60
|
|
|
{ |
61
|
|
|
$this->loop = $loop; |
62
|
|
|
$this->asyncClient = $asyncClient; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $repository |
67
|
|
|
* @return RepositoryInterface |
68
|
|
|
*/ |
69
|
|
|
public function repository(string $repository): RepositoryInterface |
70
|
|
|
{ |
71
|
|
|
return await( |
72
|
|
|
$this->asyncClient->repository($repository), |
73
|
|
|
$this->loop |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return UserInterface |
79
|
|
|
*/ |
80
|
|
|
public function user(): UserInterface |
81
|
|
|
{ |
82
|
|
|
return await( |
83
|
|
|
$this->asyncClient->user(), |
84
|
|
|
$this->loop |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param int $id |
90
|
|
|
* @return SSHKeyInterface |
91
|
|
|
*/ |
92
|
|
|
public function sshKey(int $id): SSHKeyInterface |
93
|
|
|
{ |
94
|
|
|
return await( |
95
|
|
|
$this->asyncClient->sshKey($id), |
96
|
|
|
$this->loop |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function hooks(): array |
104
|
|
|
{ |
105
|
|
|
return await( |
106
|
|
|
Promise::fromObservable( |
107
|
|
|
$this->asyncClient->hooks()->toArray() |
108
|
|
|
), |
109
|
|
|
$this->loop |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function repositories(): array |
117
|
|
|
{ |
118
|
|
|
return await( |
119
|
|
|
Promise::fromObservable( |
120
|
|
|
$this->asyncClient->repositories()->toArray() |
121
|
|
|
), |
122
|
|
|
$this->loop |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function accounts(): array |
130
|
|
|
{ |
131
|
|
|
return await( |
132
|
|
|
Promise::fromObservable( |
133
|
|
|
$this->asyncClient->accounts()->toArray() |
134
|
|
|
), |
135
|
|
|
$this->loop |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function broadcasts(): array |
143
|
|
|
{ |
144
|
|
|
return await( |
145
|
|
|
Promise::fromObservable( |
146
|
|
|
$this->asyncClient->broadcasts()->toArray() |
147
|
|
|
), |
148
|
|
|
$this->loop |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|