1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ApiClients\Client\Travis; |
5
|
|
|
|
6
|
|
|
use ApiClients\Foundation\Factory; |
7
|
|
|
use React\EventLoop\Factory as LoopFactory; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use Rx\React\Promise; |
10
|
|
|
use ApiClients\Client\Travis\Resource\RepositoryInterface; |
11
|
|
|
use ApiClients\Client\Travis\Resource\SSHKeyInterface; |
12
|
|
|
use ApiClients\Client\Travis\Resource\UserInterface; |
13
|
|
|
use function Clue\React\Block\await; |
14
|
|
|
use function React\Promise\resolve; |
15
|
|
|
|
16
|
|
|
class Client |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var LoopInterface |
20
|
|
|
*/ |
21
|
|
|
protected $loop; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var AsyncClient |
25
|
|
|
*/ |
26
|
|
|
protected $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $token |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $token = '') |
32
|
|
|
{ |
33
|
|
|
$this->loop = LoopFactory::create(); |
34
|
|
|
$this->options = ApiSettings::getOptions($token, 'Sync'); |
|
|
|
|
35
|
|
|
$this->client = new AsyncClient($this->loop, $token, Factory::create($this->loop, $this->options)); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $repository |
40
|
|
|
* @return RepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
public function repository(string $repository): RepositoryInterface |
43
|
|
|
{ |
44
|
|
|
return await( |
45
|
|
|
$this->client->repository($repository), |
46
|
|
|
$this->loop |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return UserInterface |
52
|
|
|
*/ |
53
|
|
|
public function user(): UserInterface |
54
|
|
|
{ |
55
|
|
|
return await( |
56
|
|
|
$this->client->user(), |
57
|
|
|
$this->loop |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $id |
63
|
|
|
* @return SSHKeyInterface |
64
|
|
|
*/ |
65
|
|
|
public function sshKey(int $id): SSHKeyInterface |
66
|
|
|
{ |
67
|
|
|
return await( |
68
|
|
|
$this->client->sshKey($id), |
69
|
|
|
$this->loop |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function hooks(): array |
77
|
|
|
{ |
78
|
|
|
return await( |
79
|
|
|
Promise::fromObservable( |
80
|
|
|
$this->client->hooks()->toArray() |
81
|
|
|
), |
82
|
|
|
$this->loop |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function repositories(): array |
90
|
|
|
{ |
91
|
|
|
return await( |
92
|
|
|
Promise::fromObservable( |
93
|
|
|
$this->client->repositories()->toArray() |
94
|
|
|
), |
95
|
|
|
$this->loop |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function accounts(): array |
103
|
|
|
{ |
104
|
|
|
return await( |
105
|
|
|
Promise::fromObservable( |
106
|
|
|
$this->client->accounts()->toArray() |
107
|
|
|
), |
108
|
|
|
$this->loop |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function broadcasts(): array |
116
|
|
|
{ |
117
|
|
|
return await( |
118
|
|
|
Promise::fromObservable( |
119
|
|
|
$this->client->broadcasts()->toArray() |
120
|
|
|
), |
121
|
|
|
$this->loop |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: