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