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
|
|
|
*/ |
51
|
1 |
|
public function request(array $params, array $headers = []) |
52
|
|
|
{ |
53
|
|
|
/* Request API */ |
54
|
|
|
|
55
|
1 |
|
$response = $this->getTransport()->request(new Request([ |
56
|
1 |
|
'service' => $this->getName(), |
57
|
1 |
|
'credentials' => $this->getCredentials(), |
58
|
1 |
|
'params' => $params, |
59
|
1 |
|
'headers' => array_merge($this->headers, $headers), |
60
|
1 |
|
])); |
61
|
|
|
|
62
|
1 |
|
switch ($response->getService()) { |
63
|
1 |
|
case TransportInterface::SERVICE_REPORTS: |
64
|
|
|
return $this->handleReportsResponse($response); |
65
|
|
|
|
66
|
1 |
|
default: |
67
|
1 |
|
return $this->handleResponse($response); |
68
|
1 |
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
49 |
|
public function setName($name) |
76
|
|
|
{ |
77
|
49 |
|
$this->name = $name; |
78
|
49 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
|
|
public function getName() |
85
|
|
|
{ |
86
|
|
|
return $this->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function getCredentials() |
93
|
|
|
{ |
94
|
|
|
return $this->credentials; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function getTransport() |
101
|
|
|
{ |
102
|
|
|
return $this->transport; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/* Fluid setters */ |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param TransportInterface $transport |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
50 |
|
public function setTransport($transport) |
112
|
|
|
{ |
113
|
50 |
|
$this->transport = $transport; |
114
|
50 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param CredentialsInterface $credentials |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
50 |
|
public function setCredentials($credentials) |
122
|
|
|
{ |
123
|
50 |
|
$this->credentials = $credentials; |
124
|
50 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $headers |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function setHeaders($headers) |
132
|
|
|
{ |
133
|
|
|
$this->headers = $headers; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $useOperatorUnits |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function setUseOperatorUnits($useOperatorUnits) |
142
|
|
|
{ |
143
|
|
|
if (is_numeric($useOperatorUnits) || is_bool($useOperatorUnits)) { |
144
|
|
|
$useOperatorUnits = $useOperatorUnits ? 'true' : 'false'; |
145
|
|
|
} |
146
|
|
|
$this->headers['Use-Operator-Units'] = $useOperatorUnits; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $language |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function setLanguage($language) |
155
|
|
|
{ |
156
|
|
|
$this->headers['Accept-Language'] = $language; |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
protected function handleResponse(ResponseInterface $response) |
161
|
|
|
{ |
162
|
1 |
|
$result = json_decode($response->getBody(), true); |
163
|
|
|
|
164
|
|
|
/* Handle error response */ |
165
|
|
|
|
166
|
1 |
|
if (isset($result['error']) |
167
|
1 |
|
&& $result['error'] |
168
|
1 |
|
) { |
169
|
|
|
throw new ErrorResponseException( |
170
|
|
|
$result['error']['error_string'], |
171
|
|
|
$result['error']['error_detail'], |
172
|
|
|
$result['error']['error_code'], |
173
|
|
|
$result['error']['request_id'] |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/* Prepare results */ |
178
|
|
|
|
179
|
1 |
|
$result['units'] = [ |
180
|
1 |
|
'debit' => $response->getUnitsDebit(), |
181
|
1 |
|
'limit' => $response->getUnitsLimit(), |
182
|
1 |
|
'rest' => $response->getUnitsRest() |
183
|
1 |
|
]; |
184
|
1 |
|
$result['request_id'] = $response->getRequestId(); |
185
|
|
|
|
186
|
1 |
|
return $result; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function handleReportsResponse(ResponseInterface $response) |
190
|
|
|
{ |
191
|
|
|
if ($response->getCode() >= 500) { |
192
|
|
|
$result = XML2Array::createArray($response->getBody()); |
193
|
|
|
$result = $result['reports:reportDownloadError']['reports:ApiError']; |
194
|
|
|
throw new ErrorResponseException( |
195
|
|
|
$result['reports:errorMessage'], |
196
|
|
|
$result['reports:errorDetail'], |
197
|
|
|
$result['reports:errorCode'], |
198
|
|
|
$result['reports:requestId'] |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$result = [ |
203
|
|
|
'request_id' => $response->getRequestId() |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
if ($response->getCode() == 201) { |
207
|
|
|
$result['retryIn'] = $response->getHeaders()['retryIn']; |
208
|
|
|
return $result; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$result['report'] = $response->getBody(); |
212
|
|
|
|
213
|
|
|
return $result; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|