1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Api\Management; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\RequestOptions; |
6
|
|
|
use Distilleries\Contentful\Api\BaseApi; |
7
|
|
|
use Distilleries\Contentful\Api\ManagementApi; |
8
|
|
|
|
9
|
|
|
class Api extends BaseApi implements ManagementApi |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
protected $baseUrl = 'https://api.contentful.com'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
public function locales(): array |
20
|
|
|
{ |
21
|
|
|
$response = $this->client->request('GET', $this->url('locales'), [ |
22
|
|
|
RequestOptions::HEADERS => $this->headers(), |
23
|
|
|
]); |
24
|
|
|
|
25
|
|
|
return $this->decodeResponse($response); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function contentTypes(): array |
32
|
|
|
{ |
33
|
|
|
$response = $this->client->request('GET', $this->url('content_types'), [ |
34
|
|
|
RequestOptions::HEADERS => $this->headers(), |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
return $this->decodeResponse($response); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function contentTypeEditorInterface(string $contentTypeId): array |
44
|
|
|
{ |
45
|
|
|
$response = $this->client->request('GET', $this->url('content_types/' . $contentTypeId . '/editor_interface'), [ |
46
|
|
|
RequestOptions::HEADERS => $this->headers(), |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
return $this->decodeResponse($response); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// -------------------------------------------------------------------------------- |
53
|
|
|
// -------------------------------------------------------------------------------- |
54
|
|
|
// -------------------------------------------------------------------------------- |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function entries(array $parameters = []): array |
60
|
|
|
{ |
61
|
|
|
$response = $this->client->request('GET', $this->url('entries'), [ |
62
|
|
|
RequestOptions::QUERY => $parameters, |
63
|
|
|
RequestOptions::HEADERS => $this->headers(), |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
return $this->decodeResponse($response); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function entry(string $contentType, array $fields): ?array |
73
|
|
|
{ |
74
|
|
|
$filters = []; |
75
|
|
|
$filters['content_type'] = $contentType; |
76
|
|
|
foreach ($fields as $field => $value) { |
77
|
|
|
$filters['fields.' . $field] = $value; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$response = $this->client->request('GET', $this->url('entries'), [ |
81
|
|
|
RequestOptions::QUERY => $filters, |
82
|
|
|
RequestOptions::HEADERS => $this->headers(), |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$results = $this->decodeResponse($response); |
86
|
|
|
|
87
|
|
|
return (isset($results['items']) && isset($results['items'][0]) && !empty($results['items'][0])) ? $results['items'][0] : null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function createEntry(string $contentType, array $fields, array $sys = []): array |
94
|
|
|
{ |
95
|
|
|
$response = $this->client->request('POST', $this->url('entries'), [ |
96
|
|
|
RequestOptions::BODY => json_encode([ |
97
|
|
|
'sys' => $sys, |
98
|
|
|
'fields' => $fields, |
99
|
|
|
]), |
100
|
|
|
RequestOptions::HEADERS => $this->headers([ |
101
|
|
|
'X-Contentful-Content-Type' => $contentType, |
102
|
|
|
]), |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
return $this->decodeResponse($response); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function publishEntry(string $entryId, int $version = 1): array |
112
|
|
|
{ |
113
|
|
|
$response = $this->client->request('PUT', $this->url('entries/' . $entryId . '/published'), [ |
114
|
|
|
RequestOptions::HEADERS => $this->headers([ |
115
|
|
|
'X-Contentful-Version' => $version, |
116
|
|
|
]), |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
return $this->decodeResponse($response); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function unpublishEntry(string $entryId): array |
126
|
|
|
{ |
127
|
|
|
$response = $this->client->request('DELETE', $this->url('entries/' . $entryId . '/published'), [ |
128
|
|
|
RequestOptions::HEADERS => $this->headers(), |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
return $this->decodeResponse($response); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function deleteEntry(string $entryId): bool |
138
|
|
|
{ |
139
|
|
|
$response = $this->client->request('DELETE', $this->url('entries/' . $entryId), [ |
140
|
|
|
RequestOptions::HEADERS => $this->headers(), |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
return $response->getStatusCode() === 204; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// -------------------------------------------------------------------------------- |
147
|
|
|
// -------------------------------------------------------------------------------- |
148
|
|
|
// -------------------------------------------------------------------------------- |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function assets(array $parameters = []): array |
154
|
|
|
{ |
155
|
|
|
$response = $this->client->request('GET', $this->url('assets'), [ |
156
|
|
|
RequestOptions::QUERY => $parameters, |
157
|
|
|
RequestOptions::HEADERS => $this->headers(), |
158
|
|
|
]); |
159
|
|
|
|
160
|
|
|
return $this->decodeResponse($response); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function createAsset(array $fields, array $sys = []): array |
167
|
|
|
{ |
168
|
|
|
$response = $this->client->request('POST', $this->url('assets'), [ |
169
|
|
|
RequestOptions::BODY => json_encode([ |
170
|
|
|
'fields' => $fields, |
171
|
|
|
'sys' => $sys, |
172
|
|
|
]), |
173
|
|
|
RequestOptions::HEADERS => $this->headers(), |
174
|
|
|
]); |
175
|
|
|
|
176
|
|
|
return $this->decodeResponse($response); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function processAsset(string $assetId, string $locale, int $version = 1) |
183
|
|
|
{ |
184
|
|
|
$this->client->request('PUT', $this->url('environments/master/assets/' . $assetId . '/files/' . $locale . '/process'), [ |
185
|
|
|
RequestOptions::HEADERS => $this->headers([ |
186
|
|
|
'X-Contentful-Version' => $version, |
187
|
|
|
]), |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function publishAsset(string $assetId, int $version = 1): array |
195
|
|
|
{ |
196
|
|
|
$response = $this->client->request('PUT', $this->url('assets/' . $assetId . '/published'), [ |
197
|
|
|
RequestOptions::HEADERS => $this->headers([ |
198
|
|
|
'X-Contentful-Version' => $version, |
199
|
|
|
]), |
200
|
|
|
]); |
201
|
|
|
|
202
|
|
|
return $this->decodeResponse($response); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function unpublishAsset(string $assetId): array |
209
|
|
|
{ |
210
|
|
|
$response = $this->client->request('DELETE', $this->url('assets/' . $assetId . '/published'), [ |
211
|
|
|
RequestOptions::HEADERS => $this->headers(), |
212
|
|
|
]); |
213
|
|
|
|
214
|
|
|
return $this->decodeResponse($response); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function deleteAsset(string $assetId): bool |
221
|
|
|
{ |
222
|
|
|
$response = $this->client->request('DELETE', $this->url('assets/' . $assetId), [ |
223
|
|
|
RequestOptions::HEADERS => $this->headers(), |
224
|
|
|
]); |
225
|
|
|
|
226
|
|
|
return $response->getStatusCode() === 204; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// -------------------------------------------------------------------------------- |
230
|
|
|
// -------------------------------------------------------------------------------- |
231
|
|
|
// -------------------------------------------------------------------------------- |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Return default headers + given headers. |
235
|
|
|
* |
236
|
|
|
* @param array $headers |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
|
private function headers(array $headers = []): array |
240
|
|
|
{ |
241
|
|
|
return array_merge([ |
242
|
|
|
'Content-Type' => 'application/vnd.contentful.management.v1+json', |
243
|
|
|
'Authorization' => 'Bearer ' . $this->config['tokens']['management'], |
244
|
|
|
], $headers); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|