1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace BabDev\Transifex\Connector; |
4
|
|
|
|
5
|
|
|
use BabDev\Transifex\ApiConnector; |
6
|
|
|
use BabDev\Transifex\Exception\InvalidConfigurationException; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Transifex API Projects class. |
11
|
|
|
* |
12
|
|
|
* @link http://docs.transifex.com/api/projects/ |
13
|
|
|
*/ |
14
|
|
|
final class Projects extends ApiConnector |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Build the data array to send with create and update requests. |
18
|
|
|
* |
19
|
|
|
* @param array $options Optional additional params to send with the request |
20
|
|
|
* |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
8 |
|
private function buildProjectRequest(array $options): array |
24
|
|
|
{ |
25
|
8 |
|
$data = []; |
26
|
|
|
|
27
|
|
|
// Valid options to check |
28
|
|
|
$validOptions = [ |
29
|
8 |
|
'long_description', |
30
|
|
|
'private', |
31
|
|
|
'homepage', |
32
|
|
|
'trans_instructions', |
33
|
|
|
'tags', |
34
|
|
|
'maintainers', |
35
|
|
|
'team', |
36
|
|
|
'auto_join', |
37
|
|
|
'license', |
38
|
|
|
'fill_up_resources', |
39
|
|
|
'repository_url', |
40
|
|
|
'organization', |
41
|
|
|
'archived', |
42
|
|
|
'type', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
// Loop through the valid options and if we have them, add them to the request data |
46
|
8 |
|
foreach ($validOptions as $option) { |
47
|
8 |
|
if (isset($options[$option])) { |
48
|
6 |
|
$data[$option] = $options[$option]; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Set the license if present |
53
|
8 |
|
if (isset($options['license'])) { |
54
|
4 |
|
$this->checkLicense($options['license']); |
55
|
2 |
|
$data['license'] = $options['license']; |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
return $data; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Checks that a license is an accepted value. |
63
|
|
|
* |
64
|
|
|
* @param string $license The license to check |
65
|
|
|
* |
66
|
|
|
* @throws InvalidConfigurationException |
67
|
|
|
*/ |
68
|
4 |
|
private function checkLicense(string $license): void |
69
|
|
|
{ |
70
|
4 |
|
$accepted = ['proprietary', 'permissive_open_source', 'other_open_source']; |
71
|
|
|
|
72
|
|
|
// Ensure the license option is an allowed value |
73
|
4 |
|
if (!\in_array($license, $accepted)) { |
74
|
2 |
|
throw new InvalidConfigurationException( |
75
|
2 |
|
\sprintf( |
76
|
2 |
|
'The license %s is not valid, accepted license values are %s', |
77
|
2 |
|
$license, |
78
|
2 |
|
\implode(', ', $accepted) |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
2 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a project. |
86
|
|
|
* |
87
|
|
|
* @param string $name The name of the project |
88
|
|
|
* @param string $slug The slug for the project |
89
|
|
|
* @param string $description A description of the project |
90
|
|
|
* @param string $sourceLanguage The source language code for the project |
91
|
|
|
* @param array $options Optional additional params to send with the request |
92
|
|
|
* |
93
|
|
|
* @return ResponseInterface |
94
|
|
|
* |
95
|
|
|
* @throws InvalidConfigurationException |
96
|
|
|
*/ |
97
|
4 |
|
public function createProject( |
98
|
|
|
string $name, |
99
|
|
|
string $slug, |
100
|
|
|
string $description, |
101
|
|
|
string $sourceLanguage, |
102
|
|
|
array $options = [] |
103
|
|
|
): ResponseInterface { |
104
|
|
|
// Build the request data. |
105
|
4 |
|
$data = \array_merge( |
106
|
|
|
[ |
107
|
4 |
|
'name' => $name, |
108
|
4 |
|
'slug' => $slug, |
109
|
4 |
|
'description' => $description, |
110
|
4 |
|
'source_language_code' => $sourceLanguage, |
111
|
|
|
], |
112
|
4 |
|
$this->buildProjectRequest($options) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
// Check mandatory fields. |
116
|
3 |
|
if (!isset($data['license']) || \in_array($data['license'], ['permissive_open_source', 'other_open_source'])) { |
117
|
3 |
|
if (!isset($data['repository_url'])) { |
118
|
1 |
|
throw new InvalidConfigurationException( |
119
|
1 |
|
'If a project is denoted either as permissive_open_source or other_open_source, the field repository_url is mandatory and should contain a link to the public repository of the project to be created.' |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
$request = $this->createRequest('POST', $this->createUri('/api/2/projects/')); |
125
|
2 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($data))); |
126
|
2 |
|
$request = $request->withHeader('Content-Type', 'application/json'); |
127
|
|
|
|
128
|
2 |
|
return $this->client->sendRequest($request); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Delete a project. |
133
|
|
|
* |
134
|
|
|
* @param string $slug The slug for the resource |
135
|
|
|
* |
136
|
|
|
* @return ResponseInterface |
137
|
|
|
*/ |
138
|
2 |
|
public function deleteProject(string $slug): ResponseInterface |
139
|
|
|
{ |
140
|
2 |
|
return $this->client->sendRequest($this->createRequest('DELETE', $this->createUri("/api/2/project/$slug"))); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get a list of projects belonging to an organization. |
145
|
|
|
* |
146
|
|
|
* @return ResponseInterface |
147
|
|
|
*/ |
148
|
4 |
|
public function getOrganizationProjects(string $organization): ResponseInterface |
149
|
|
|
{ |
150
|
|
|
// This API endpoint uses the newer `api.transifex.com` subdomain, only change if the default www was given |
151
|
4 |
|
$currentBaseUri = $this->getOption('base_uri'); |
152
|
|
|
|
153
|
4 |
|
if (!$currentBaseUri || $currentBaseUri === 'https://www.transifex.com') { |
154
|
4 |
|
$this->setOption('base_uri', 'https://api.transifex.com'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
try { |
158
|
4 |
|
return $this->client->sendRequest($this->createRequest('GET', $this->createUri("/organizations/$organization/projects/"))); |
159
|
|
|
} finally { |
160
|
4 |
|
$this->setOption('base_uri', $currentBaseUri); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get information about a project. |
166
|
|
|
* |
167
|
|
|
* @param string $project The project to retrieve details for |
168
|
|
|
* @param bool $details True to retrieve additional project details |
169
|
|
|
* |
170
|
|
|
* @return ResponseInterface |
171
|
|
|
*/ |
172
|
2 |
|
public function getProject(string $project, bool $details = false): ResponseInterface |
173
|
|
|
{ |
174
|
2 |
|
$uri = $this->createUri("/api/2/project/$project/"); |
175
|
|
|
|
176
|
2 |
|
if ($details) { |
177
|
2 |
|
$uri = $uri->withQuery('details'); |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
return $this->client->sendRequest($this->createRequest('GET', $uri)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get a list of projects the user is part of. |
185
|
|
|
* |
186
|
|
|
* @return ResponseInterface |
187
|
|
|
*/ |
188
|
3 |
|
public function getProjects(): ResponseInterface |
189
|
|
|
{ |
190
|
3 |
|
return $this->client->sendRequest($this->createRequest('GET', $this->createUri('/api/2/projects/'))); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Update a project. |
195
|
|
|
* |
196
|
|
|
* @param string $slug The slug for the project |
197
|
|
|
* @param array $options Additional params to send with the request |
198
|
|
|
* |
199
|
|
|
* @return ResponseInterface |
200
|
|
|
* |
201
|
|
|
* @throws \RuntimeException |
202
|
|
|
*/ |
203
|
4 |
|
public function updateProject(string $slug, array $options): ResponseInterface |
204
|
|
|
{ |
205
|
4 |
|
$data = $this->buildProjectRequest($options); |
206
|
|
|
|
207
|
|
|
// Make sure we actually have data to send |
208
|
3 |
|
if (empty($data)) { |
209
|
1 |
|
throw new \RuntimeException('There is no data to send to Transifex.'); |
210
|
|
|
} |
211
|
|
|
|
212
|
2 |
|
$request = $this->createRequest('PUT', $this->createUri("/api/2/project/$slug/")); |
213
|
2 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($data))); |
214
|
2 |
|
$request = $request->withHeader('Content-Type', 'application/json'); |
215
|
|
|
|
216
|
2 |
|
return $this->client->sendRequest($request); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|