1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Travis; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Travis\CommandBus\Command; |
6
|
|
|
use ApiClients\Client\Travis\Resource\HookInterface; |
7
|
|
|
use ApiClients\Foundation\ClientInterface; |
8
|
|
|
use ApiClients\Foundation\Factory; |
9
|
|
|
use React\EventLoop\LoopInterface; |
10
|
|
|
use React\Promise\CancellablePromiseInterface; |
11
|
|
|
use React\Promise\PromiseInterface; |
12
|
|
|
use Rx\Observable; |
13
|
|
|
use Rx\ObservableInterface; |
14
|
|
|
use Rx\React\Promise; |
15
|
|
|
use function ApiClients\Tools\Rx\unwrapObservableFromPromise; |
16
|
|
|
use function React\Promise\resolve; |
17
|
|
|
|
18
|
|
|
final class AsyncClient |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ClientInterface |
22
|
|
|
*/ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param LoopInterface $loop |
27
|
|
|
* @param string $token |
28
|
|
|
* @param array $options |
29
|
|
|
* @return AsyncClient |
30
|
|
|
*/ |
31
|
|
|
public static function create( |
32
|
|
|
LoopInterface $loop, |
33
|
|
|
string $token = '', |
34
|
|
|
array $options = [] |
35
|
|
|
): self { |
36
|
|
|
$options = ApiSettings::getOptions($token, 'Async', $options); |
37
|
|
|
$client = Factory::create($loop, $options); |
38
|
|
|
return new self($client); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ClientInterface $client |
43
|
|
|
* @return AsyncClient |
44
|
|
|
*/ |
45
|
6 |
|
public static function createFromClient(ClientInterface $client): self |
46
|
|
|
{ |
47
|
6 |
|
return new self($client); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ClientInterface $client |
52
|
|
|
*/ |
53
|
6 |
|
private function __construct(ClientInterface $client) |
54
|
|
|
{ |
55
|
6 |
|
$this->client = $client; |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $repository |
60
|
|
|
* @return CancellablePromiseInterface |
61
|
|
|
*/ |
62
|
1 |
|
public function repository(string $repository): CancellablePromiseInterface |
63
|
|
|
{ |
64
|
1 |
|
return $this->client->handle(new Command\RepositoryCommand($repository)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return ObservableInterface |
69
|
|
|
*/ |
70
|
|
|
public function repositories(): ObservableInterface |
71
|
|
|
{ |
72
|
|
|
return $this->hooks()->filter(function ($hook) { |
73
|
|
|
return $hook->active(); |
74
|
|
|
})->flatMap(function (HookInterface $hook) { |
75
|
|
|
return Promise::toObservable($this->client->handle( |
76
|
|
|
new Command\RepositoryIdCommand($hook->id()) |
77
|
|
|
)); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return PromiseInterface |
83
|
|
|
*/ |
84
|
|
|
public function user(): PromiseInterface |
85
|
|
|
{ |
86
|
1 |
|
return $this->client->handle(new Command\UserCommand()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $id |
91
|
|
|
* @return PromiseInterface |
92
|
|
|
*/ |
93
|
1 |
|
public function sshKey(int $id): PromiseInterface |
94
|
|
|
{ |
95
|
1 |
|
return $this->client->handle(new Command\SSHKeyCommand($id)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return ObservableInterface |
100
|
|
|
*/ |
101
|
|
|
public function hooks(): ObservableInterface |
102
|
|
|
{ |
103
|
|
|
return unwrapObservableFromPromise($this->client->handle( |
104
|
|
|
new Command\HooksCommand() |
105
|
|
|
)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return ObservableInterface |
110
|
|
|
*/ |
111
|
|
|
public function accounts(): ObservableInterface |
112
|
|
|
{ |
113
|
|
|
return unwrapObservableFromPromise($this->client->handle( |
114
|
|
|
new Command\AccountsCommand() |
115
|
|
|
)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return ObservableInterface |
120
|
|
|
*/ |
121
|
|
|
public function broadcasts(): ObservableInterface |
122
|
|
|
{ |
123
|
|
|
return unwrapObservableFromPromise($this->client->handle( |
124
|
|
|
new Command\BroadcastsCommand() |
125
|
|
|
)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|