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 Yandex\Direct\Exception\ErrorResponseException; |
10
|
|
|
use Yandex\Direct\Transport\Request; |
11
|
|
|
use Yandex\Direct\Transport\RequestInterface; |
12
|
|
|
use Yandex\Direct\Transport\ResponseInterface; |
13
|
|
|
use Yandex\Direct\Transport\TransportInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Service |
17
|
|
|
* @package Yandex\Direct |
18
|
|
|
*/ |
19
|
|
|
abstract class Service implements ServiceInterface |
20
|
|
|
{ |
21
|
|
|
use ConfigurableTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TransportInterface |
30
|
|
|
*/ |
31
|
|
|
protected $transport; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CredentialsInterface |
35
|
|
|
*/ |
36
|
|
|
protected $credentials; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $headers = [ |
42
|
|
|
'Accept-Language' => RequestInterface::LANGUAGE_RU |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
* @throws ErrorResponseException |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
1 |
|
public function request(array $params, array $headers = []) |
52
|
|
|
{ |
53
|
1 |
|
if (!empty($params['params'])) { |
54
|
|
|
$params['params'] = filter_params($params['params']); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
if (empty($params['params'])) { |
58
|
1 |
|
$params['params'] = new \StdClass; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
$response = $this->getTransport()->request(new Request([ |
62
|
1 |
|
'service' => $this->getName(), |
63
|
1 |
|
'credentials' => $this->getCredentials(), |
64
|
1 |
|
'params' => $params, |
65
|
1 |
|
'headers' => array_merge($this->headers, $headers), |
66
|
1 |
|
])); |
67
|
|
|
|
68
|
1 |
|
return $this->handleResponse($response); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
76 |
|
public function setName($name) |
76
|
|
|
{ |
77
|
76 |
|
$this->name = $name; |
78
|
76 |
|
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
|
77 |
|
public function setTransport($transport) |
112
|
|
|
{ |
113
|
77 |
|
$this->transport = $transport; |
114
|
77 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param CredentialsInterface $credentials |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
77 |
|
public function setCredentials($credentials) |
122
|
|
|
{ |
123
|
77 |
|
$this->credentials = $credentials; |
124
|
77 |
|
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_bool($useOperatorUnits)) { |
144
|
|
|
$this->headers['Use-Operator-Units'] = $useOperatorUnits ? 'true' : 'false'; |
145
|
|
|
} |
146
|
|
|
|
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
|
|
|
/** |
161
|
|
|
* @param ResponseInterface $response |
162
|
|
|
* @return array |
163
|
|
|
* @throws ErrorResponseException |
164
|
|
|
*/ |
165
|
1 |
|
protected function handleResponse(ResponseInterface $response) |
166
|
|
|
{ |
167
|
1 |
|
$result = json_decode($response->getBody(), true); |
168
|
|
|
|
169
|
|
|
/* Handle error response */ |
170
|
|
|
|
171
|
1 |
|
if (isset($result['error']) |
172
|
1 |
|
&& $result['error'] |
173
|
1 |
|
) { |
174
|
|
|
throw new ErrorResponseException( |
175
|
|
|
$result['error']['error_string'], |
176
|
|
|
$result['error']['error_detail'], |
177
|
|
|
$result['error']['error_code'], |
178
|
|
|
$result['error']['request_id'] |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/* Prepare results */ |
183
|
|
|
|
184
|
1 |
|
$result['units'] = [ |
185
|
1 |
|
'debit' => $response->getUnitsDebit(), |
186
|
1 |
|
'limit' => $response->getUnitsLimit(), |
187
|
1 |
|
'rest' => $response->getUnitsRest() |
188
|
1 |
|
]; |
189
|
1 |
|
$result['request_id'] = $response->getRequestId(); |
190
|
|
|
|
191
|
1 |
|
return $result; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|