1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Dmitry Gladyshev <[email protected]> |
4
|
|
|
* @date 17/08/2016 11:28 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Yandex\Direct; |
8
|
|
|
|
9
|
|
|
use LSS\XML2Array; |
10
|
|
|
use Yandex\Direct\Exception\ErrorResponseException; |
11
|
|
|
use Yandex\Direct\Transport\Request; |
12
|
|
|
use Yandex\Direct\Transport\RequestInterface; |
13
|
|
|
use Yandex\Direct\Transport\ResponseInterface; |
14
|
|
|
use Yandex\Direct\Transport\TransportInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Service |
18
|
|
|
* @package Yandex\Direct |
19
|
|
|
*/ |
20
|
|
|
abstract class Service implements ServiceInterface |
21
|
|
|
{ |
22
|
|
|
use ConfigurableTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TransportInterface |
31
|
|
|
*/ |
32
|
|
|
protected $transport; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CredentialsInterface |
36
|
|
|
*/ |
37
|
|
|
protected $credentials; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $headers = [ |
43
|
|
|
'Accept-Language' => RequestInterface::LANGUAGE_RU, |
44
|
|
|
'Use-Operator-Units' => 'true' |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
* @throws ErrorResponseException |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
1 |
|
public function request(array $params, array $headers = []) |
54
|
|
|
{ |
55
|
|
|
/* Request API */ |
56
|
|
|
|
57
|
1 |
|
$response = $this->getTransport()->request(new Request([ |
58
|
1 |
|
'service' => $this->getName(), |
59
|
1 |
|
'credentials' => $this->getCredentials(), |
60
|
1 |
|
'params' => filter_params($params), |
61
|
1 |
|
'headers' => array_merge($this->headers, $headers), |
62
|
1 |
|
])); |
63
|
|
|
|
64
|
1 |
|
switch ($response->getService()) { |
65
|
1 |
|
case TransportInterface::SERVICE_REPORTS: |
66
|
|
|
return $this->handleReportsResponse($response); |
|
|
|
|
67
|
|
|
|
68
|
1 |
|
default: |
69
|
1 |
|
return $this->handleResponse($response); |
70
|
1 |
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $name |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
73 |
|
public function setName($name) |
78
|
|
|
{ |
79
|
73 |
|
$this->name = $name; |
80
|
73 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function getName() |
87
|
|
|
{ |
88
|
|
|
return $this->name; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function getCredentials() |
95
|
|
|
{ |
96
|
|
|
return $this->credentials; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
|
|
public function getTransport() |
103
|
|
|
{ |
104
|
|
|
return $this->transport; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* Fluid setters */ |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param TransportInterface $transport |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
74 |
|
public function setTransport($transport) |
114
|
|
|
{ |
115
|
74 |
|
$this->transport = $transport; |
116
|
74 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param CredentialsInterface $credentials |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
74 |
|
public function setCredentials($credentials) |
124
|
|
|
{ |
125
|
74 |
|
$this->credentials = $credentials; |
126
|
74 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $headers |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function setHeaders($headers) |
134
|
|
|
{ |
135
|
|
|
$this->headers = $headers; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $useOperatorUnits |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setUseOperatorUnits($useOperatorUnits) |
144
|
|
|
{ |
145
|
|
|
if (is_numeric($useOperatorUnits) || is_bool($useOperatorUnits)) { |
146
|
|
|
$useOperatorUnits = $useOperatorUnits ? 'true' : 'false'; |
147
|
|
|
} |
148
|
|
|
$this->headers['Use-Operator-Units'] = $useOperatorUnits; |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $language |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function setLanguage($language) |
157
|
|
|
{ |
158
|
|
|
$this->headers['Accept-Language'] = $language; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param ResponseInterface $response |
164
|
|
|
* @return array |
165
|
|
|
* @throws ErrorResponseException |
166
|
|
|
*/ |
167
|
1 |
|
protected function handleResponse(ResponseInterface $response) |
168
|
|
|
{ |
169
|
1 |
|
$result = json_decode($response->getBody(), true); |
170
|
|
|
|
171
|
|
|
/* Handle error response */ |
172
|
|
|
|
173
|
1 |
|
if (isset($result['error']) |
174
|
1 |
|
&& $result['error'] |
175
|
1 |
|
) { |
176
|
|
|
throw new ErrorResponseException( |
177
|
|
|
$result['error']['error_string'], |
178
|
|
|
$result['error']['error_detail'], |
179
|
|
|
$result['error']['error_code'], |
180
|
|
|
$result['error']['request_id'] |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/* Prepare results */ |
185
|
|
|
|
186
|
1 |
|
$result['units'] = [ |
187
|
1 |
|
'debit' => $response->getUnitsDebit(), |
188
|
1 |
|
'limit' => $response->getUnitsLimit(), |
189
|
1 |
|
'rest' => $response->getUnitsRest() |
190
|
1 |
|
]; |
191
|
1 |
|
$result['request_id'] = $response->getRequestId(); |
192
|
|
|
|
193
|
1 |
|
return $result; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param ResponseInterface $response |
198
|
|
|
* @return array|\DOMDocument |
199
|
|
|
* @throws ErrorResponseException |
200
|
|
|
* @throws \Exception |
201
|
|
|
*/ |
202
|
|
|
protected function handleReportsResponse(ResponseInterface $response) |
203
|
|
|
{ |
204
|
|
|
if ($response->getCode() >= 500) { |
205
|
|
|
$result = XML2Array::createArray($response->getBody()); |
206
|
|
|
$result = $result['reports:reportDownloadError']['reports:ApiError']; |
207
|
|
|
throw new ErrorResponseException( |
208
|
|
|
$result['reports:errorMessage'], |
209
|
|
|
$result['reports:errorDetail'], |
210
|
|
|
$result['reports:errorCode'], |
211
|
|
|
$result['reports:requestId'] |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$result = [ |
216
|
|
|
'request_id' => $response->getRequestId() |
217
|
|
|
]; |
218
|
|
|
|
219
|
|
|
if ($response->getCode() == 201 |
220
|
|
|
|| $response->getCode() == 202 |
221
|
|
|
) { |
222
|
|
|
$result['retryIn'] = $response->getHeaders()['retryIn']; |
223
|
|
|
return $result; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$result['report'] = $response->getBody(); |
227
|
|
|
|
228
|
|
|
return $result; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|