|
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\ObservableInterface; |
|
13
|
|
|
use Rx\React\Promise; |
|
14
|
|
|
use Rx\Scheduler; |
|
15
|
|
|
use function ApiClients\Tools\Rx\unwrapObservableFromPromise; |
|
16
|
|
|
|
|
17
|
|
|
final class AsyncClient implements AsyncClientInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ClientInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $client; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a new AsyncClient based on the loop and other options pass |
|
26
|
|
|
* |
|
27
|
|
|
* @param LoopInterface $loop |
|
28
|
|
|
* @param string $token Instructions to fetch the token: https://blog.travis-ci.com/2013-01-28-token-token-token/ |
|
29
|
|
|
* @param array $options |
|
30
|
|
|
* @return AsyncClient |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public static function create( |
|
33
|
|
|
LoopInterface $loop, |
|
34
|
|
|
string $token = '', |
|
35
|
|
|
array $options = [] |
|
36
|
|
|
): self { |
|
37
|
|
|
try { |
|
38
|
|
|
Scheduler::setAsyncFactory(function () use ($loop) { |
|
39
|
|
|
return new Scheduler\EventLoopScheduler($loop); |
|
|
|
|
|
|
40
|
1 |
|
}); |
|
41
|
|
|
} catch (\Throwable $t) { |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
$options = ApiSettings::getOptions($token, 'Async', $options); |
|
45
|
1 |
|
$client = Factory::create($loop, $options); |
|
46
|
1 |
|
return new self($client); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create an AsyncClient from a ApiClients\Foundation\ClientInterface. |
|
51
|
|
|
* Be sure to pass in a client with the options from ApiSettings and the Async namespace suffix. |
|
52
|
|
|
* |
|
53
|
|
|
* @param ClientInterface $client |
|
54
|
|
|
* @return AsyncClient |
|
55
|
|
|
*/ |
|
56
|
8 |
|
public static function createFromClient(ClientInterface $client): self |
|
57
|
|
|
{ |
|
58
|
8 |
|
return new self($client); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param ClientInterface $client |
|
63
|
|
|
*/ |
|
64
|
9 |
|
private function __construct(ClientInterface $client) |
|
65
|
|
|
{ |
|
66
|
9 |
|
$this->client = $client; |
|
67
|
9 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function repository(string $repository): CancellablePromiseInterface |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->client->handle(new Command\RepositoryCommand($repository)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Warning this a intensive operation as it has to make a call for each hook |
|
79
|
|
|
* to turn it into a Repository!!! |
|
80
|
|
|
* |
|
81
|
|
|
* Take a look at examples/repositories-async.php on how to limit the amount of |
|
82
|
|
|
* concurrent requests. |
|
83
|
|
|
* |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function repositories(callable $filter = null): ObservableInterface |
|
87
|
|
|
{ |
|
88
|
|
|
if ($filter === null) { |
|
89
|
|
|
$filter = function () { |
|
90
|
|
|
return true; |
|
91
|
|
|
}; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->hooks()->filter(function ($hook) { |
|
95
|
|
|
return $hook->active(); |
|
96
|
|
|
})->filter($filter)->flatMap(function (HookInterface $hook) { |
|
97
|
|
|
return Promise::toObservable($this->client->handle( |
|
98
|
|
|
new Command\RepositoryIdCommand($hook->id()) |
|
99
|
|
|
)); |
|
100
|
|
|
}); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function user(): PromiseInterface |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->client->handle(new Command\UserCommand()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function sshKey(int $id): PromiseInterface |
|
115
|
|
|
{ |
|
116
|
1 |
|
return $this->client->handle(new Command\SSHKeyCommand($id)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return ObservableInterface |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function hooks(): ObservableInterface |
|
123
|
|
|
{ |
|
124
|
1 |
|
return unwrapObservableFromPromise($this->client->handle( |
|
125
|
1 |
|
new Command\HooksCommand() |
|
126
|
|
|
)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function accounts(): ObservableInterface |
|
133
|
|
|
{ |
|
134
|
1 |
|
return unwrapObservableFromPromise($this->client->handle( |
|
135
|
1 |
|
new Command\AccountsCommand() |
|
136
|
|
|
)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function broadcasts(): ObservableInterface |
|
143
|
|
|
{ |
|
144
|
1 |
|
return unwrapObservableFromPromise($this->client->handle( |
|
145
|
1 |
|
new Command\BroadcastsCommand() |
|
146
|
|
|
)); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: