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