1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\Travis\Resource\Async; |
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
use React\Promise\PromiseInterface; |
8
|
|
|
use Rx\Observable; |
9
|
|
|
use Rx\ObservableInterface; |
10
|
|
|
use Rx\React\Promise; |
11
|
|
|
use WyriHaximus\Travis\Resource\Repository as BaseRepository; |
12
|
|
|
use function React\Promise\reject; |
13
|
|
|
use function React\Promise\resolve; |
14
|
|
|
|
15
|
|
|
class Repository extends BaseRepository |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return ObservableInterface |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function builds(): ObservableInterface |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
return Promise::toObservable( |
23
|
|
|
$this->getTransport()->request('repos/' . $this->slug() . '/builds') |
24
|
|
|
)->flatMap(function ($response) { |
25
|
|
|
return Observable::fromArray($response['builds']); |
26
|
|
|
})->map(function ($build) { |
27
|
|
|
return $this->getTransport()->getHydrator()->hydrate('Build', $build); |
28
|
|
|
}); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param int $id |
33
|
|
|
* @return PromiseInterface |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function build(int $id): PromiseInterface |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
return $this->getTransport()->request( |
38
|
|
|
'repos/' . $this->slug() . '/builds/' . $id |
39
|
|
|
)->then(function ($response) { |
40
|
|
|
return resolve($this->getTransport()->getHydrator()->hydrate('Build', $response['build'])); |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return ObservableInterface |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function commits(): ObservableInterface |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return Promise::toObservable( |
50
|
|
|
$this->getTransport()->request('repos/' . $this->slug() . '/builds') |
51
|
|
|
)->flatMap(function ($response) { |
52
|
|
|
return Observable::fromArray($response['commits']); |
53
|
|
|
})->map(function ($build) { |
54
|
|
|
return $this->getTransport()->getHydrator()->hydrate('Commit', $build); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return PromiseInterface |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function settings(): PromiseInterface |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return $this->getTransport()->request( |
64
|
|
|
'repos/' . $this->id() . '/settings' |
65
|
|
|
)->then(function ($response) { |
66
|
|
|
return resolve($this->getTransport()->getHydrator()->hydrate('Settings', $response['settings'])); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return PromiseInterface |
72
|
|
|
*/ |
73
|
|
|
public function isActive(): PromiseInterface |
74
|
|
|
{ |
75
|
|
|
return $this->getTransport()->request( |
76
|
|
|
'hooks' |
77
|
|
|
)->then(function ($response) { |
78
|
|
|
$active = false; |
79
|
|
|
foreach ($response['hooks'] as $hook) { |
80
|
|
|
if ($hook['id'] == $this->id()) { |
81
|
|
|
$active = (bool)$hook['active']; |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($active) { |
87
|
|
|
return resolve($active); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return reject($active); |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return PromiseInterface |
96
|
|
|
*/ |
97
|
|
|
public function enable(): PromiseInterface |
98
|
|
|
{ |
99
|
|
|
return $this->setActiveStatus(true); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return PromiseInterface |
104
|
|
|
*/ |
105
|
|
|
public function disable(): PromiseInterface |
106
|
|
|
{ |
107
|
|
|
return $this->setActiveStatus(false); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param bool $status |
112
|
|
|
* @return PromiseInterface |
113
|
|
|
*/ |
114
|
|
|
protected function setActiveStatus(bool $status) |
115
|
|
|
{ |
116
|
|
|
return $this->getTransport()->requestPsr7( |
117
|
|
|
new Request( |
118
|
|
|
'PUT', |
119
|
|
|
$this->getTransport()->getBaseURL() . 'hooks/' . $this->id(), |
120
|
|
|
$this->getTransport()->getHeaders(), |
121
|
|
|
json_encode([ |
122
|
|
|
'hook' => [ |
123
|
|
|
'active' => $status, |
124
|
|
|
], |
125
|
|
|
]) |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return ObservableInterface |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function branches(): ObservableInterface |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return Promise::toObservable( |
136
|
|
|
$this->getTransport()->request('repos/' . $this->slug() . '/branches') |
137
|
|
|
)->flatMap(function ($response) { |
138
|
|
|
return Observable::fromArray($response['branches']); |
139
|
|
|
})->map(function ($branch) { |
140
|
|
|
return $this->getTransport()->getHydrator()->hydrate('Branch', $branch); |
141
|
|
|
}); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return ObservableInterface |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function vars(): ObservableInterface |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
return Promise::toObservable( |
150
|
|
|
$this->getTransport()->request('/settings/env_vars?repository_id=' . $this->id()) |
151
|
|
|
)->flatMap(function ($response) { |
152
|
|
|
return Observable::fromArray($response['env_vars']); |
153
|
|
|
})->map(function ($var) { |
154
|
|
|
return $this->getTransport()->getHydrator()->hydrate('EnvironmentVariable', $var); |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return ObservableInterface |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function caches(): ObservableInterface |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
return Promise::toObservable( |
164
|
|
|
$this->getTransport()->request('repos/' . $this->slug() . '/caches') |
165
|
|
|
)->flatMap(function ($response) { |
166
|
|
|
return Observable::fromArray($response['caches']); |
167
|
|
|
})->map(function ($cache) { |
168
|
|
|
return $this->getTransport()->getHydrator()->hydrate('Cache', $cache); |
169
|
|
|
}); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return PromiseInterface |
174
|
|
|
*/ |
175
|
|
|
public function key(): PromiseInterface |
176
|
|
|
{ |
177
|
|
|
return $this->getTransport()->request('repos/' . $this->slug() . '/key')->then(function ($key) { |
178
|
|
|
return resolve($this->getTransport()->getHydrator()->hydrate('RepositoryKey', $key)); |
179
|
|
|
}); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function refresh(): PromiseInterface |
183
|
|
|
{ |
184
|
|
|
return $this->getTransport()->request('repos/' . $this->slug)->then(function ($json) { |
185
|
|
|
return resolve($this->getTransport()->getHydrator()->hydrate('Repository', $json['repo'])); |
186
|
|
|
}); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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.