1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\Travis; |
5
|
|
|
|
6
|
|
|
use React\EventLoop\Factory as LoopFactory; |
7
|
|
|
use Rx\React\Promise; |
8
|
|
|
use WyriHaximus\Travis\Resource\RepositoryInterface; |
9
|
|
|
use WyriHaximus\Travis\Resource\Sync\Repository; |
10
|
|
|
use WyriHaximus\ApiClient\Transport\Client as Transport; |
11
|
|
|
use WyriHaximus\ApiClient\Transport\Factory; |
12
|
|
|
use function Clue\React\Block\await; |
13
|
|
|
use function React\Promise\resolve; |
14
|
|
|
|
15
|
|
|
class Client |
16
|
|
|
{ |
17
|
|
|
protected $transport; |
18
|
|
|
protected $client; |
19
|
|
|
|
20
|
|
|
public function __construct(string $token = '', Transport $transport = null) |
21
|
|
|
{ |
22
|
|
|
$loop = LoopFactory::create(); |
23
|
|
View Code Duplication |
if (!($transport instanceof Transport)) { |
|
|
|
|
24
|
|
|
$options = [ |
25
|
|
|
'resource_namespace' => 'Async', |
26
|
|
|
] + ApiSettings::TRANSPORT_OPTIONS; |
27
|
|
|
|
28
|
|
|
if ($token !== '') { |
29
|
|
|
$options['headers']['Authorization'] = 'token ' . $token; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$transport = Factory::create($loop, $options); |
33
|
|
|
} |
34
|
|
|
$this->transport = $transport; |
35
|
|
|
$this->client = new AsyncClient($loop, $token, $this->transport); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function repository(string $repository): RepositoryInterface |
39
|
|
|
{ |
40
|
|
|
return await( |
41
|
|
|
$this->client->repository($repository), |
42
|
|
|
$this->transport->getLoop() |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function accounts(): array |
47
|
|
|
{ |
48
|
|
|
return await( |
49
|
|
|
Promise::fromObservable( |
50
|
|
|
$this->client->accounts()->toArray() |
51
|
|
|
), |
52
|
|
|
$this->transport->getLoop() |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function broadcasts(): array |
57
|
|
|
{ |
58
|
|
|
return await( |
59
|
|
|
Promise::fromObservable( |
60
|
|
|
$this->client->broadcasts()->toArray() |
61
|
|
|
), |
62
|
|
|
$this->transport->getLoop() |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.