1 | <?php |
||
29 | class Transport implements TransportInterface, LoggerAwareInterface |
||
30 | { |
||
31 | use ConfigurableTrait; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $baseUrl = 'https://api.direct.yandex.com'; |
||
37 | |||
38 | /** |
||
39 | * @var ClientInterface |
||
40 | */ |
||
41 | private $httpClient; |
||
42 | |||
43 | /** |
||
44 | * Custom Service urls |
||
45 | * @var array |
||
46 | */ |
||
47 | private $serviceUrls = []; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $headers = [ |
||
53 | 'Content-Type' => 'application/json; charset=utf-8' |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * @var LoggerInterface |
||
58 | */ |
||
59 | private $logger; |
||
60 | |||
61 | /** |
||
62 | * @var MessageFormatter |
||
63 | */ |
||
64 | private $logMessageFormatter; |
||
65 | |||
66 | /** |
||
67 | * JsonTransport constructor. |
||
68 | * |
||
69 | * @param array $options |
||
70 | */ |
||
71 | 38 | public function __construct(array $options = []) |
|
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | 2 | public function getServiceUrl($serviceName) |
|
91 | |||
92 | /** |
||
93 | * @param array $headers |
||
94 | */ |
||
95 | 1 | public function setHeaders(array $headers) |
|
96 | { |
||
97 | 1 | $this->headers = array_merge($this->headers, $headers); |
|
98 | 1 | } |
|
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | */ |
||
103 | 1 | public function setLogger(LoggerInterface $logger) |
|
104 | { |
||
105 | 1 | $this->logger = $logger; |
|
106 | 1 | } |
|
107 | |||
108 | /** |
||
109 | * @param RequestInterface $request |
||
110 | * @return Response |
||
111 | * @throws RuntimeException |
||
112 | * @throws TransportRequestException |
||
113 | */ |
||
114 | 1 | public function request(RequestInterface $request) |
|
148 | |||
149 | /** |
||
150 | * @return ClientInterface |
||
151 | */ |
||
152 | 1 | private function getHttpClient() |
|
162 | |||
163 | /** |
||
164 | * @return LoggerInterface |
||
165 | */ |
||
166 | 1 | private function getLogger() |
|
167 | { |
||
168 | // Use stub if logger is not initialized |
||
169 | 1 | if ($this->logger === null) { |
|
170 | $this->logger = new NullLogger; |
||
171 | } |
||
172 | 1 | return $this->logger; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * @return MessageFormatter |
||
177 | */ |
||
178 | 1 | private function getMessageFormatter() |
|
179 | { |
||
180 | 1 | if ($this->logMessageFormatter === null) { |
|
181 | 1 | $this->logMessageFormatter = new MessageFormatter(MessageFormatter::DEBUG); |
|
182 | 1 | } |
|
183 | 1 | return $this->logMessageFormatter; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return HandlerStack |
||
188 | */ |
||
189 | 1 | private function getHttpHandlers() |
|
190 | { |
||
191 | 1 | $stack = HandlerStack::create(); |
|
192 | 1 | $stack->push(Middleware::log( |
|
193 | 1 | $this->getLogger(), |
|
194 | 1 | $this->getMessageFormatter() |
|
195 | 1 | )); |
|
196 | 1 | return $stack; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param Request | RequestInterface $request |
||
201 | * @return array |
||
202 | */ |
||
203 | 1 | private function prepareHeaders(RequestInterface $request) |
|
221 | |||
222 | /** |
||
223 | * @param Request | RequestInterface $request |
||
224 | * @return string |
||
225 | */ |
||
226 | 1 | private function prepareBody(RequestInterface $request) |
|
233 | } |
||
234 |