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