1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\Travis; |
5
|
|
|
|
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use React\Promise\PromiseInterface; |
8
|
|
|
use Rx\Observable; |
9
|
|
|
use Rx\ObservableInterface; |
10
|
|
|
use Rx\React\Promise; |
11
|
|
|
use WyriHaximus\ApiClient\Transport\Client as Transport; |
12
|
|
|
use WyriHaximus\ApiClient\Transport\Factory; |
13
|
|
|
use function React\Promise\resolve; |
14
|
|
|
|
15
|
|
|
class AsyncClient |
16
|
|
|
{ |
17
|
|
|
protected $transport; |
18
|
|
|
|
19
|
|
|
public function __construct(LoopInterface $loop, string $token = '', Transport $transport = null) |
20
|
|
|
{ |
21
|
|
View Code Duplication |
if (!($transport instanceof Transport)) { |
|
|
|
|
22
|
|
|
$options = [ |
23
|
|
|
'resource_namespace' => 'Async', |
24
|
|
|
] + ApiSettings::TRANSPORT_OPTIONS; |
25
|
|
|
|
26
|
|
|
if ($token !== '') { |
27
|
|
|
$options['headers']['Authorization'] = 'token ' . $token; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$transport = Factory::create($loop, $options); |
31
|
|
|
} |
32
|
|
|
$this->transport = $transport; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function repository(string $repository): PromiseInterface |
36
|
|
|
{ |
37
|
|
|
return $this->transport->request('repos/' . $repository)->then(function ($json) { |
38
|
|
|
return resolve($this->transport->getHydrator()->hydrate('Repository', $json['repo'])); |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function hooks(): ObservableInterface |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
return Promise::toObservable( |
45
|
|
|
$this->transport->request('hooks') |
46
|
|
|
)->flatMap(function ($response) { |
47
|
|
|
return Observable::fromArray($response['hooks']); |
48
|
|
|
})->map(function ($hook) { |
49
|
|
|
return $this->transport->getHydrator()->hydrate('Hook', $hook); |
50
|
|
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function accounts(): ObservableInterface |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
return Promise::toObservable( |
56
|
|
|
$this->transport->request('accounts') |
57
|
|
|
)->flatMap(function ($response) { |
58
|
|
|
return Observable::fromArray($response['accounts']); |
59
|
|
|
})->map(function ($account) { |
60
|
|
|
return $this->transport->getHydrator()->hydrate('Account', $account); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function broadcasts(): ObservableInterface |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
return Promise::toObservable( |
67
|
|
|
$this->transport->request('broadcasts') |
68
|
|
|
)->flatMap(function ($response) { |
69
|
|
|
return Observable::fromArray($response['broadcasts']); |
70
|
|
|
})->map(function ($broadcast) { |
71
|
|
|
return $this->getTransport()->getHydrator()->hydrate('Broadcast', $broadcast); |
|
|
|
|
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.