|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command; |
|
6
|
|
|
use ApiClients\Foundation\ClientInterface; |
|
|
|
|
|
|
7
|
|
|
use ApiClients\Foundation\Factory; |
|
8
|
|
|
use React\EventLoop\LoopInterface; |
|
9
|
|
|
use React\Promise\PromiseInterface; |
|
10
|
|
|
use Rx\Observable; |
|
11
|
|
|
use function ApiClients\Tools\Rx\unwrapObservableFromPromise; |
|
12
|
|
|
use function React\Promise\resolve; |
|
13
|
|
|
use Rx\Scheduler; |
|
14
|
|
|
|
|
15
|
|
|
final class AsyncClient implements AsyncClientInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ClientInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param LoopInterface $loop |
|
24
|
|
|
* @param AuthenticationInterface $auth |
|
25
|
|
|
* @param array $options |
|
26
|
|
|
* @return AsyncClient |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function create( |
|
29
|
|
|
LoopInterface $loop, |
|
30
|
|
|
AuthenticationInterface $auth, |
|
31
|
|
|
array $options = [] |
|
32
|
|
|
): self { |
|
33
|
|
|
$options = ApiSettings::getOptions($auth, $options, 'Async'); |
|
34
|
|
|
$client = Factory::create($loop, $options); |
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
|
|
Scheduler::setAsyncFactory(function () use ($loop) { |
|
38
|
|
|
return new Scheduler\EventLoopScheduler($loop); |
|
|
|
|
|
|
39
|
|
|
}); |
|
40
|
|
|
} catch (\Throwable $t) { |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return new self($client); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param ClientInterface $client |
|
48
|
|
|
*/ |
|
49
|
|
|
private function __construct(ClientInterface $client) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->client = $client; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function user(string $user): PromiseInterface |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->client->handle(new Command\UserCommand($user)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function whoami(): PromiseInterface |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->client->handle(new Command\UserCommand()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function myOrganizations(): Observable |
|
65
|
|
|
{ |
|
66
|
|
|
return unwrapObservableFromPromise($this->client->handle(new Command\MyOrganizationsCommand())); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function licenses(): Observable |
|
70
|
|
|
{ |
|
71
|
|
|
return unwrapObservableFromPromise($this->client->handle(new Command\LicensesCommand())); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
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: