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