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