1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\AppVeyor; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\AppVeyor\CommandBus\Command\AddProjectCommand; |
6
|
|
|
use ApiClients\Client\AppVeyor\CommandBus\Command\ProjectCommand; |
7
|
|
|
use ApiClients\Client\AppVeyor\CommandBus\Command\ProjectsCommand; |
8
|
|
|
use ApiClients\Client\AppVeyor\Resource\ProjectInterface; |
9
|
|
|
use ApiClients\Foundation\ClientInterface; |
|
|
|
|
10
|
|
|
use ApiClients\Foundation\Factory; |
11
|
|
|
use React\EventLoop\LoopInterface; |
12
|
|
|
use React\Promise\Promise; |
13
|
|
|
use React\Promise\PromiseInterface; |
14
|
|
|
use Rx\ObservableInterface; |
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
|
|
|
*/ |
28
|
|
|
private function __construct(ClientInterface $client) |
29
|
|
|
{ |
30
|
|
|
$this->client = $client; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param LoopInterface $loop |
35
|
|
|
* @param string $token |
36
|
|
|
* @param array $options |
37
|
|
|
* @return AsyncClient |
38
|
|
|
*/ |
39
|
|
|
public static function create( |
40
|
|
|
LoopInterface $loop, |
41
|
|
|
string $token, |
42
|
|
|
array $options = [] |
43
|
|
|
): self { |
44
|
|
|
$options = ApiSettings::getOptions($token, $options, 'Async'); |
45
|
|
|
$client = Factory::create($loop, $options); |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
Scheduler::setAsyncFactory(function () use ($loop) { |
49
|
|
|
return new Scheduler\EventLoopScheduler($loop); |
|
|
|
|
50
|
|
|
}); |
51
|
|
|
} catch (\Throwable $t) { |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return new self($client); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @internal |
59
|
|
|
* @param ClientInterface $client |
60
|
|
|
* @return AsyncClient |
61
|
|
|
*/ |
62
|
|
|
public static function createFromClient(ClientInterface $client): self |
63
|
|
|
{ |
64
|
|
|
return new self($client); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function projects(): ObservableInterface |
68
|
|
|
{ |
69
|
|
|
return unwrapObservableFromPromise($this->client->handle(new ProjectsCommand())); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function project(string $repository): PromiseInterface |
73
|
|
|
{ |
74
|
|
|
return $this->client->handle(new ProjectCommand($repository)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function hasProject(string $provider, string $repository): PromiseInterface |
78
|
|
|
{ |
79
|
|
|
return new Promise(function ($resolve, $reject) use ($provider, $repository) { |
80
|
|
|
$hasProject = false; |
81
|
|
|
$this->projects()->filter(function (ProjectInterface $project) use ($provider, $repository) { |
82
|
|
|
return $project->repositoryType() === $provider && $project->repositoryName() === $repository; |
83
|
|
|
})->take(1)->subscribe(function () use (&$hasProject) { |
84
|
|
|
$hasProject = true; |
85
|
|
|
}, function ($error) use ($reject) { |
86
|
|
|
$reject($error); |
87
|
|
|
}, function () use (&$hasProject, $resolve) { |
88
|
|
|
$resolve($hasProject); |
89
|
|
|
}); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function addProject(string $provider, string $repository): PromiseInterface |
94
|
|
|
{ |
95
|
|
|
return $this->client->handle(new AddProjectCommand($provider, $repository)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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: