1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stp\SndApi\News; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
7
|
|
|
use Stp\SndApi\Common\Client as CommonClient; |
8
|
|
|
use Stp\SndApi\Common\Exception\ItemDoesNotExistsException; |
9
|
|
|
use Stp\SndApi\Common\Exception\UnsatisfactoryResponseCodeException; |
10
|
|
|
use Stp\SndApi\News\Converter\ArticlesListParametersConverter; |
11
|
|
|
use Stp\SndApi\News\Exception\InvalidContentTypeException; |
12
|
|
|
use Stp\SndApi\News\Exception\InvalidMethodException; |
13
|
|
|
use Stp\SndApi\News\Exception\InvalidMethodParametersException; |
14
|
|
|
use Stp\SndApi\News\Validator\ArticleListMethodValidator; |
15
|
|
|
use Stp\SndApi\News\Validator\ArticlesListParametersValidator; |
16
|
|
|
use Stp\SndApi\News\Validator\ContentTypeValidator; |
17
|
|
|
|
18
|
|
|
class Client extends CommonClient |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
|
|
public function __construct($apiKey, $apiSecret, $publicationId) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($apiKey, $apiSecret, $publicationId, self::BASE_URL . '/news/v2'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $url |
30
|
|
|
* @param bool $acceptJsonResponse |
31
|
|
|
* |
32
|
|
|
* @return ResponseInterface |
33
|
|
|
* @throws ItemDoesNotExistsException |
34
|
|
|
* @throws UnsatisfactoryResponseCodeException |
35
|
|
|
* @throws RequestException |
36
|
|
|
*/ |
37
|
|
|
protected function apiGet($url, $acceptJsonResponse = true) |
38
|
|
|
{ |
39
|
|
|
$response = parent::apiGet($url, $acceptJsonResponse); |
40
|
|
|
|
41
|
|
|
if ($response->getStatusCode() === 404) { |
42
|
|
|
throw new ItemDoesNotExistsException; |
43
|
|
|
} elseif ($response->getStatusCode() !== 200) { |
44
|
|
|
throw new UnsatisfactoryResponseCodeException(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $response; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array|bool|float|int|string |
52
|
|
|
* @throws ItemDoesNotExistsException |
53
|
|
|
* @throws UnsatisfactoryResponseCodeException |
54
|
|
|
*/ |
55
|
|
|
public function getServiceDocument() |
56
|
|
|
{ |
57
|
|
|
$response = $this->apiGet('/'); |
58
|
|
|
|
59
|
|
|
return $response->json(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array|bool|float|int|string |
64
|
|
|
* @throws ItemDoesNotExistsException |
65
|
|
|
* @throws UnsatisfactoryResponseCodeException |
66
|
|
|
*/ |
67
|
|
|
public function getSectionsList() |
68
|
|
|
{ |
69
|
|
|
$response = $this->apiGet('/publication/{publicationId}/sections'); |
70
|
|
|
|
71
|
|
|
return $response->json(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $sectionId |
76
|
|
|
* @return array|bool|float|int|string |
77
|
|
|
* @throws ItemDoesNotExistsException |
78
|
|
|
* @throws UnsatisfactoryResponseCodeException |
79
|
|
|
*/ |
80
|
|
|
public function getSubsectionsList($sectionId) |
81
|
|
|
{ |
82
|
|
|
$response = $this->apiGet(sprintf('/publication/{publicationId}/sections/%d/subsections', $sectionId)); |
83
|
|
|
|
84
|
|
|
return $response->json(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $sectionName |
89
|
|
|
* @return array|bool|float|int|string |
90
|
|
|
*/ |
91
|
|
|
public function getSectionByUniqueName($sectionName) |
92
|
|
|
{ |
93
|
|
|
return $this->getSection(sprintf('/publication/{publicationId}/sections/instance?uniqueName=%s', $sectionName)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $sectionId |
98
|
|
|
* @return array|bool|float|int|string |
99
|
|
|
*/ |
100
|
|
|
public function getSectionById($sectionId) |
101
|
|
|
{ |
102
|
|
|
return $this->getSection(sprintf('/publication/{publicationId}/sections/%d', $sectionId)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $url |
107
|
|
|
* @return array|bool|float|int|string |
108
|
|
|
* @throws ItemDoesNotExistsException |
109
|
|
|
* @throws UnsatisfactoryResponseCodeException |
110
|
|
|
*/ |
111
|
|
|
private function getSection($url) |
112
|
|
|
{ |
113
|
|
|
$response = $this->apiGet($url); |
114
|
|
|
|
115
|
|
|
return $response->json(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param int|string $sectionId Section id or uniqueName |
120
|
|
|
* @param string $method |
121
|
|
|
* @param array $parameters |
122
|
|
|
* @return array|bool|float|int|string |
123
|
|
|
* @throws InvalidMethodException |
124
|
|
|
* @throws InvalidMethodParametersException |
125
|
|
|
* @throws ItemDoesNotExistsException |
126
|
|
|
* @throws UnsatisfactoryResponseCodeException |
127
|
|
|
*/ |
128
|
|
|
public function getArticlesBySectionId($sectionId, $method, $parameters = []) |
129
|
|
|
{ |
130
|
|
|
$methodValidator = new ArticleListMethodValidator($method); |
131
|
|
|
if (!$methodValidator->isValid()) { |
132
|
|
|
throw new InvalidMethodException(sprintf('Method "%s" is not allowed', $method)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$parametersConverter = new ArticlesListParametersConverter(); |
136
|
|
|
$parameters = $parametersConverter->convert($parameters); |
137
|
|
|
|
138
|
|
|
$parametersValidator = new ArticlesListParametersValidator($method, $parameters); |
139
|
|
|
if (!$parametersValidator->isValid()) { |
140
|
|
|
throw new InvalidMethodParametersException( |
141
|
|
|
sprintf('Invalid parameter name or value used for "%s" method', $method) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$url = sprintf( |
146
|
|
|
'/publication/{publicationId}/sections/%s/%s?%s', |
147
|
|
|
$sectionId, |
148
|
|
|
$method, |
149
|
|
|
http_build_query($parameters) |
150
|
|
|
); |
151
|
|
|
$url = rtrim($url, '?&'); |
152
|
|
|
|
153
|
|
|
$response = $this->apiGet($url); |
154
|
|
|
|
155
|
|
|
return $response->json(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $contentId |
160
|
|
|
* @return array|bool|float|int|string |
161
|
|
|
* @throws InvalidContentTypeException |
162
|
|
|
*/ |
163
|
|
|
public function getArticle($contentId) |
164
|
|
|
{ |
165
|
|
|
return $this->searchByInstance($contentId, 'article'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return array |
170
|
|
|
* @throws ItemDoesNotExistsException |
171
|
|
|
* @throws UnsatisfactoryResponseCodeException |
172
|
|
|
*/ |
173
|
|
|
public function getImageVersions() |
174
|
|
|
{ |
175
|
|
|
$response = $this->apiGet( |
176
|
|
|
sprintf( |
177
|
|
|
'/publication/%s/imageversions', |
178
|
|
|
$this->getPublicationId() |
179
|
|
|
), |
180
|
|
|
false |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$imageVersions = []; |
184
|
|
|
|
185
|
|
|
if ($responseBody = $response->getBody()) { |
186
|
|
|
$imageVersions = array_filter(explode("\n", $responseBody)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $imageVersions; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param int $contentId |
194
|
|
|
* @param string $contentType |
195
|
|
|
* @return array|bool|float|int|string |
196
|
|
|
* @throws InvalidContentTypeException |
197
|
|
|
* @throws ItemDoesNotExistsException |
198
|
|
|
* @throws UnsatisfactoryResponseCodeException |
199
|
|
|
*/ |
200
|
|
|
public function searchByInstance($contentId, $contentType) |
201
|
|
|
{ |
202
|
|
|
$contentTypeValidator = new ContentTypeValidator($contentType); |
203
|
|
|
|
204
|
|
|
if (!$contentTypeValidator->isValid()) { |
205
|
|
|
throw new InvalidContentTypeException(sprintf('Content Type "%s" is not allowed', $contentType)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$response = $this->apiGet( |
209
|
|
|
sprintf( |
210
|
|
|
'/publication/{publicationId}/searchContents/instance?contentId=%d&contentType=%s', |
211
|
|
|
$contentId, |
212
|
|
|
$contentType |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
return $response->json(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param array $contentIds |
221
|
|
|
* @return array|bool|float|int|string |
222
|
|
|
* @throws ItemDoesNotExistsException |
223
|
|
|
* @throws UnsatisfactoryResponseCodeException |
224
|
|
|
*/ |
225
|
|
|
public function searchByCollection($contentIds) |
226
|
|
|
{ |
227
|
|
|
$response = $this->apiGet( |
228
|
|
|
sprintf('/publication/{publicationId}/searchContents/collection?contentIds=%s', join(',', $contentIds)) |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
return $response->json(); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|