|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace WyriHaximus\Travis; |
|
5
|
|
|
|
|
6
|
|
|
use ApiClients\Foundation\Client; |
|
|
|
|
|
|
7
|
|
|
use ApiClients\Foundation\Factory; |
|
8
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand; |
|
9
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand; |
|
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 React\Promise\resolve; |
|
17
|
|
|
|
|
18
|
|
|
class AsyncClient |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Client |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $client; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param LoopInterface $loop |
|
27
|
|
|
* @param string $token |
|
28
|
|
|
* @param Client|null $client |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(LoopInterface $loop, string $token = '', Client $client = null) |
|
31
|
|
|
{ |
|
32
|
|
|
if (!($client instanceof Client)) { |
|
33
|
|
|
$this->options = ApiSettings::getOptions($token, 'Async'); |
|
|
|
|
|
|
34
|
|
|
$client = Factory::create($loop, $this->options); |
|
35
|
|
|
} |
|
36
|
|
|
$this->client = $client; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $repository |
|
41
|
|
|
* @return CancellablePromiseInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
public function repository(string $repository): CancellablePromiseInterface |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->client->handle(new SimpleRequestCommand('repos/' . $repository))->then(function ($response) { |
|
46
|
|
|
return $this->client->handle(new HydrateCommand('Repository', $response->getBody()->getJson()['repo'])); |
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return PromiseInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
public function user(): PromiseInterface |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->client->handle(new SimpleRequestCommand('users'))->then(function ($response) { |
|
56
|
|
|
return $this->client->handle(new HydrateCommand('User', $response->getBody()->getJson()['user'])); |
|
57
|
|
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param int $id |
|
62
|
|
|
* @return PromiseInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
public function sshKey(int $id): PromiseInterface |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->client->handle(new SimpleRequestCommand('settings/ssh_key/' . $id))->then(function ($response) { |
|
67
|
|
|
return $this->client->handle(new HydrateCommand('SSHKey', $response->getBody()->getJson()['ssh_key'])); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return ObservableInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
public function hooks(): ObservableInterface |
|
75
|
|
|
{ |
|
76
|
|
|
return Promise::toObservable( |
|
77
|
|
|
$this->client->handle(new SimpleRequestCommand('hooks')) |
|
78
|
|
|
)->flatMap(function ($response) { |
|
79
|
|
|
return Observable::fromArray($response->getBody()->getJson()['hooks']); |
|
80
|
|
|
})->flatMap(function ($hook) { |
|
81
|
|
|
return Promise::toObservable($this->client->handle(new HydrateCommand('Hook', $hook))); |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return ObservableInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public function accounts(): ObservableInterface |
|
89
|
|
|
{ |
|
90
|
|
|
return Promise::toObservable( |
|
91
|
|
|
$this->client->handle(new SimpleRequestCommand('accounts')) |
|
92
|
|
|
)->flatMap(function ($response) { |
|
93
|
|
|
return Observable::fromArray($response->getBody()->getJson()['accounts']); |
|
94
|
|
|
})->flatMap(function ($account) { |
|
95
|
|
|
return Promise::toObservable($this->client->handle(new HydrateCommand('Account', $account))); |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return ObservableInterface |
|
101
|
|
|
*/ |
|
102
|
|
|
public function broadcasts(): ObservableInterface |
|
103
|
|
|
{ |
|
104
|
|
|
return Promise::toObservable( |
|
105
|
|
|
$this->client->handle(new SimpleRequestCommand('broadcasts')) |
|
106
|
|
|
)->flatMap(function ($response) { |
|
107
|
|
|
return Observable::fromArray($response->getBody()->getJson()['broadcasts']); |
|
108
|
|
|
})->flatMap(function ($broadcast) { |
|
109
|
|
|
return Promise::toObservable($this->client->handle(new HydrateCommand('Broadcast', $broadcast))); |
|
110
|
|
|
}); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
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: