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