1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\Travis\Resource\Async; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Pusher\AsyncClient; |
6
|
|
|
use ApiClients\Client\Pusher\CommandBus\Command\SharedAppClientCommand; |
7
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand; |
8
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand; |
9
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand; |
10
|
|
|
use ApiClients\Foundation\Transport\JsonStream; |
11
|
|
|
use GuzzleHttp\Psr7\Request; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use React\Promise\PromiseInterface; |
14
|
|
|
use Rx\Observable; |
15
|
|
|
use Rx\ObservableInterface; |
16
|
|
|
use Rx\Observer\CallbackObserver; |
17
|
|
|
use Rx\ObserverInterface; |
18
|
|
|
use Rx\React\Promise; |
19
|
|
|
use Rx\SchedulerInterface; |
20
|
|
|
use WyriHaximus\Travis\ApiSettings; |
21
|
|
|
use WyriHaximus\Travis\Resource\Repository as BaseRepository; |
22
|
|
|
use function React\Promise\reject; |
23
|
|
|
use function React\Promise\resolve; |
24
|
|
|
|
25
|
|
|
class Repository extends BaseRepository |
26
|
|
|
{ |
27
|
|
View Code Duplication |
public function builds(): Observable |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
return Promise::toObservable( |
30
|
|
|
$this->handleCommand(new SimpleRequestCommand('repos/' . $this->slug() . '/builds')) |
31
|
|
|
)->flatMap(function (ResponseInterface $response) { |
32
|
|
|
return Observable::fromArray($response->getBody()->getJson()['builds']); |
|
|
|
|
33
|
|
|
})->flatMap(function (array $build) { |
34
|
|
|
return Promise::toObservable($this->handleCommand(new HydrateCommand('Build', $build))); |
35
|
|
|
}); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function jobs(int $buildId): Observable |
39
|
|
|
{ |
40
|
|
|
return Promise::toObservable($this->build($buildId))->flatMap(function (Build $build) { |
41
|
|
|
return $build->jobs(); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $id |
47
|
|
|
* @return PromiseInterface |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function build(int $id): PromiseInterface |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
return $this->handleCommand( |
52
|
|
|
new SimpleRequestCommand('repos/' . $this->slug() . '/builds/' . $id) |
53
|
|
|
)->then(function (ResponseInterface $response) { |
54
|
|
|
return resolve($this->handleCommand( |
55
|
|
|
new HydrateCommand('Build', $response->getBody()->getJson()['build']) |
|
|
|
|
56
|
|
|
)); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return ObservableInterface |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function commits(): ObservableInterface |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
return Promise::toObservable( |
66
|
|
|
$this->handleCommand(new SimpleRequestCommand('repos/' . $this->slug() . '/builds')) |
67
|
|
|
)->flatMap(function (ResponseInterface $response) { |
68
|
|
|
return Observable::fromArray($response->getBody()->getJson()['commits']); |
|
|
|
|
69
|
|
|
})->flatMap(function (array $commit) { |
70
|
|
|
return Promise::toObservable($this->handleCommand(new HydrateCommand('Commit', $commit))); |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function events(): Observable |
75
|
|
|
{ |
76
|
|
|
return Observable::create(function ( |
77
|
|
|
ObserverInterface $observer, |
78
|
|
|
SchedulerInterface $scheduler |
|
|
|
|
79
|
|
|
) { |
80
|
|
|
$this->handleCommand( |
81
|
|
|
new SharedAppClientCommand(ApiSettings::PUSHER_KEY) |
82
|
|
|
)->then(function ($pusher) use ($observer) { |
83
|
|
|
$pusher->channel('repo-' . $this->id)->filter(function ($message) { |
84
|
|
|
return in_array($message->event, [ |
85
|
|
|
'build:created', |
86
|
|
|
'build:started', |
87
|
|
|
'build:finished', |
88
|
|
|
]); |
89
|
|
|
})->map(function ($message) { |
90
|
|
|
return json_decode($message->data, true); |
91
|
|
|
})->filter(function ($json) { |
92
|
|
|
return isset($json['repository']); |
93
|
|
|
})->flatMap(function ($json) { |
94
|
|
|
return Promise::toObservable( |
95
|
|
|
$this->handleCommand( |
96
|
|
|
new HydrateCommand('Repository', $json['repository']) |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
})->subscribe(new CallbackObserver(function ($repository) use ($observer) { |
100
|
|
|
$observer->onNext($repository); |
101
|
|
|
})); |
102
|
|
|
}); |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return PromiseInterface |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function settings(): PromiseInterface |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
return $this->handleCommand( |
112
|
|
|
new SimpleRequestCommand('repos/' . $this->id() . '/settings') |
113
|
|
|
)->then(function (ResponseInterface $response) { |
114
|
|
|
return resolve($this->handleCommand( |
115
|
|
|
new HydrateCommand('Settings', $response->getBody()->getJson()['settings']) |
|
|
|
|
116
|
|
|
)); |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return PromiseInterface |
122
|
|
|
*/ |
123
|
|
|
public function isActive(): PromiseInterface |
124
|
|
|
{ |
125
|
|
|
return $this->handleCommand(new SimpleRequestCommand('hooks'))->then(function (ResponseInterface $response) { |
126
|
|
|
$active = false; |
127
|
|
|
foreach ($response->getBody()->getJson()['hooks'] as $hook) { |
|
|
|
|
128
|
|
|
if ($hook['id'] == $this->id()) { |
129
|
|
|
$active = (bool)$hook['active']; |
130
|
|
|
break; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($active) { |
135
|
|
|
return resolve($active); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return reject($active); |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return PromiseInterface |
144
|
|
|
*/ |
145
|
|
|
public function enable(): PromiseInterface |
146
|
|
|
{ |
147
|
|
|
return $this->setActiveStatus(true); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return PromiseInterface |
152
|
|
|
*/ |
153
|
|
|
public function disable(): PromiseInterface |
154
|
|
|
{ |
155
|
|
|
return $this->setActiveStatus(false); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param bool $status |
160
|
|
|
* @return PromiseInterface |
161
|
|
|
*/ |
162
|
|
|
protected function setActiveStatus(bool $status) |
163
|
|
|
{ |
164
|
|
|
return $this->handleCommand(new RequestCommand( |
165
|
|
|
new Request( |
166
|
|
|
'PUT', |
167
|
|
|
'hooks/' . $this->id(), |
168
|
|
|
[], |
169
|
|
|
new JsonStream([ |
170
|
|
|
'hook' => [ |
171
|
|
|
'active' => $status, |
172
|
|
|
], |
173
|
|
|
]) |
174
|
|
|
) |
175
|
|
|
))->then(function () { |
176
|
|
|
return $this->refresh(); |
177
|
|
|
}); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return ObservableInterface |
182
|
|
|
*/ |
183
|
|
View Code Duplication |
public function branches(): ObservableInterface |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
return Promise::toObservable( |
186
|
|
|
$this->handleCommand(new SimpleRequestCommand('repos/' . $this->slug() . '/branches')) |
187
|
|
|
)->flatMap(function (ResponseInterface $response) { |
188
|
|
|
return Observable::fromArray($response->getBody()->getJson()['branches']); |
|
|
|
|
189
|
|
|
})->flatMap(function (array $branch) { |
190
|
|
|
return Promise::toObservable($this->handleCommand(new HydrateCommand('Branch', $branch))); |
191
|
|
|
}); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return ObservableInterface |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function vars(): ObservableInterface |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
return Promise::toObservable( |
200
|
|
|
$this->handleCommand(new SimpleRequestCommand('settings/env_vars?repository_id=' . $this->id())) |
201
|
|
|
)->flatMap(function (ResponseInterface $response) { |
202
|
|
|
return Observable::fromArray($response->getBody()->getJson()['env_vars']); |
|
|
|
|
203
|
|
|
})->flatMap(function (array $envVar) { |
204
|
|
|
return Promise::toObservable($this->handleCommand(new HydrateCommand('EnvironmentVariable', $envVar))); |
205
|
|
|
}); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return ObservableInterface |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
public function caches(): ObservableInterface |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
return Promise::toObservable( |
214
|
|
|
$this->handleCommand(new SimpleRequestCommand('repos/' . $this->slug() . '/caches')) |
215
|
|
|
)->flatMap(function (ResponseInterface $response) { |
216
|
|
|
return Observable::fromArray($response->getBody()->getJson()['caches']); |
|
|
|
|
217
|
|
|
})->flatMap(function (array $cache) { |
218
|
|
|
return Promise::toObservable($this->handleCommand(new HydrateCommand('Cache', $cache))); |
219
|
|
|
}); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return PromiseInterface |
224
|
|
|
*/ |
225
|
|
View Code Duplication |
public function key(): PromiseInterface |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
return $this->handleCommand( |
228
|
|
|
new SimpleRequestCommand('repos/' . $this->slug() . '/key') |
229
|
|
|
)->then(function (ResponseInterface $response) { |
230
|
|
|
return resolve($this->handleCommand(new HydrateCommand('RepositoryKey', $response->getBody()->getJson()))); |
|
|
|
|
231
|
|
|
}); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
View Code Duplication |
public function refresh(): PromiseInterface |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
return $this->handleCommand( |
237
|
|
|
new SimpleRequestCommand('repos/' . $this->slug) |
238
|
|
|
)->then(function ($response) { |
239
|
|
|
return resolve($this->handleCommand( |
240
|
|
|
new HydrateCommand('Repository', $response->getBody()->getJson()['repo']) |
241
|
|
|
)); |
242
|
|
|
}); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
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.