1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Travis\Resource\Async; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Pusher\CommandBus\Command\SharedAppClientCommand; |
6
|
|
|
use ApiClients\Client\Travis\ApiSettings; |
7
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\BranchesCommand; |
8
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\BuildsCommand; |
9
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\CachesCommand; |
10
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\CommitsCommand; |
11
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\RepositoryCommand; |
12
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\RepositoryKeyCommand; |
13
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\SettingsCommand; |
14
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\VarsCommand; |
15
|
|
|
use ApiClients\Client\Travis\Resource\Repository as BaseRepository; |
16
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand; |
17
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand; |
18
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand; |
19
|
|
|
use ApiClients\Foundation\Transport\JsonStream; |
20
|
|
|
use GuzzleHttp\Psr7\Request; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
use React\Promise\PromiseInterface; |
23
|
|
|
use Rx\Observable; |
24
|
|
|
use Rx\ObservableInterface; |
25
|
|
|
use Rx\Observer\CallbackObserver; |
26
|
|
|
use Rx\ObserverInterface; |
27
|
|
|
use Rx\React\Promise; |
28
|
|
|
use Rx\SchedulerInterface; |
29
|
|
|
use function ApiClients\Tools\Rx\unwrapObservableFromPromise; |
30
|
|
|
use function React\Promise\reject; |
31
|
|
|
use function React\Promise\resolve; |
32
|
|
|
|
33
|
|
|
class Repository extends BaseRepository |
34
|
|
|
{ |
35
|
|
|
public function builds(): Observable |
36
|
|
|
{ |
37
|
|
|
return unwrapObservableFromPromise($this->handleCommand( |
38
|
|
|
new BuildsCommand($this->slug()) |
39
|
|
|
)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function jobs(int $buildId): Observable |
43
|
|
|
{ |
44
|
|
|
return Promise::toObservable($this->build($buildId))->flatMap(function (Build $build) { |
|
|
|
|
45
|
|
|
return $build->jobs(); |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param int $id |
51
|
|
|
* @return PromiseInterface |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function build(int $id): PromiseInterface |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
return $this->handleCommand( |
56
|
|
|
new SimpleRequestCommand('repos/' . $this->slug() . '/builds/' . $id) |
57
|
|
|
)->then(function (ResponseInterface $response) { |
58
|
|
|
return resolve($this->handleCommand( |
59
|
|
|
new HydrateCommand('Build', $response->getBody()->getJson()['build']) |
|
|
|
|
60
|
|
|
)); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return ObservableInterface |
66
|
|
|
*/ |
67
|
|
|
public function commits(): ObservableInterface |
68
|
|
|
{ |
69
|
|
|
return unwrapObservableFromPromise($this->handleCommand( |
70
|
|
|
new CommitsCommand($this->slug()) |
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
|
|
|
public function settings(): PromiseInterface |
110
|
|
|
{ |
111
|
|
|
return $this->handleCommand( |
112
|
|
|
new SettingsCommand($this->id()) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return PromiseInterface |
118
|
|
|
*/ |
119
|
|
|
public function isActive(): PromiseInterface |
120
|
|
|
{ |
121
|
|
|
return $this->handleCommand(new SimpleRequestCommand('hooks'))->then(function (ResponseInterface $response) { |
122
|
|
|
$active = false; |
123
|
|
|
foreach ($response->getBody()->getJson()['hooks'] as $hook) { |
|
|
|
|
124
|
|
|
if ($hook['id'] == $this->id()) { |
125
|
|
|
$active = (bool)$hook['active']; |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($active) { |
131
|
|
|
return resolve($active); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return reject($active); |
135
|
|
|
}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return PromiseInterface |
140
|
|
|
*/ |
141
|
|
|
public function enable(): PromiseInterface |
142
|
|
|
{ |
143
|
|
|
return $this->setActiveStatus(true); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return PromiseInterface |
148
|
|
|
*/ |
149
|
|
|
public function disable(): PromiseInterface |
150
|
|
|
{ |
151
|
|
|
return $this->setActiveStatus(false); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param bool $status |
156
|
|
|
* @return PromiseInterface |
157
|
|
|
*/ |
158
|
|
|
protected function setActiveStatus(bool $status) |
159
|
|
|
{ |
160
|
|
|
return $this->handleCommand(new RequestCommand( |
161
|
|
|
new Request( |
162
|
|
|
'PUT', |
163
|
|
|
'hooks/' . $this->id(), |
164
|
|
|
[], |
165
|
|
|
new JsonStream([ |
166
|
|
|
'hook' => [ |
167
|
|
|
'active' => $status, |
168
|
|
|
], |
169
|
|
|
]) |
170
|
|
|
) |
171
|
|
|
))->then(function () { |
172
|
|
|
return $this->refresh(); |
173
|
|
|
}); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return ObservableInterface |
178
|
|
|
*/ |
179
|
|
|
public function branches(): ObservableInterface |
180
|
|
|
{ |
181
|
|
|
return unwrapObservableFromPromise($this->handleCommand( |
182
|
|
|
new BranchesCommand($this->id()) |
183
|
|
|
)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return ObservableInterface |
188
|
|
|
*/ |
189
|
|
|
public function vars(): ObservableInterface |
190
|
|
|
{ |
191
|
|
|
return unwrapObservableFromPromise($this->handleCommand( |
192
|
|
|
new VarsCommand($this->id()) |
193
|
|
|
)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return ObservableInterface |
198
|
|
|
*/ |
199
|
|
|
public function caches(): ObservableInterface |
200
|
|
|
{ |
201
|
|
|
return unwrapObservableFromPromise($this->handleCommand( |
202
|
|
|
new CachesCommand($this->id()) |
203
|
|
|
)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return PromiseInterface |
208
|
|
|
*/ |
209
|
|
|
public function key(): PromiseInterface |
210
|
|
|
{ |
211
|
|
|
return $this->handleCommand( |
212
|
|
|
new RepositoryKeyCommand($this->slug()) |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function refresh(): PromiseInterface |
217
|
|
|
{ |
218
|
|
|
return $this->handleCommand( |
219
|
|
|
new RepositoryCommand($this->slug) |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.