1 | <?php |
||
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) |
|
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | */ |
||
84 | public function getName() |
||
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | public function getCredentials() |
||
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | public function getTransport() |
||
104 | |||
105 | /* Fluid setters */ |
||
106 | |||
107 | /** |
||
108 | * @param TransportInterface $transport |
||
109 | * @return $this |
||
110 | */ |
||
111 | 77 | public function setTransport($transport) |
|
116 | |||
117 | /** |
||
118 | * @param CredentialsInterface $credentials |
||
119 | * @return $this |
||
120 | */ |
||
121 | 77 | public function setCredentials($credentials) |
|
126 | |||
127 | /** |
||
128 | * @param array $headers |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function setHeaders($headers) |
||
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) |
||
159 | |||
160 | /** |
||
161 | * @param ResponseInterface $response |
||
162 | * @return array |
||
163 | * @throws ErrorResponseException |
||
164 | */ |
||
165 | 1 | protected function handleResponse(ResponseInterface $response) |
|
193 | } |
||
194 |