· 4 years ago · Jun 08, 2021, 05:00 PM
1<?php
2/**
3 *
4 * * This file is part of Boxberry Api.
5 * *
6 * * (c) 2016, T. I. R. Ltd.
7 * * Evgeniy Mosunov, Alexander Borovikov
8 * *
9 * * For the full copyright and license information, please view LICENSE
10 * * file that was distributed with this source code
11 * *
12 * * File: Client.php
13 * * Created: 26.07.2016
14 * *
15
16 */
17namespace Boxberry\Client;
18
19use Boxberry\Client\Exceptions\BadSettingsException;
20use Boxberry\Client\Exceptions\UnknownTypeException;
21use Boxberry\Client\Exceptions\BadResponseException;
22use Boxberry\Requests;
23use Boxberry\Requests\Request;
24
25use Boxberry\Requests\DeliveryCostsRequest;
26
27/**
28 * Базовый класс, который создает объекты запросов и исполняет их.
29 * Class Client
30 * @package Boxberry\Client
31 */
32class Client
33{
34 /**
35 * @var string
36 */
37 protected $key = null;
38
39 /**
40 * @var string
41 */
42 protected $api_url;
43
44 /**
45 * @var string
46 */
47 protected $production_url = 'https://api.boxberry.ru/json.php';
48
49 /**
50 * @var string
51 */
52 protected $debug_url = 'https://test-ecom.boxberry.ru/json.php';
53
54 /**
55 * @var bool
56 */
57 protected $debug_mode_enabled;
58
59 public function __construct()
60 {
61 $this->api_url = $this->production_url;
62 }
63
64 public function enableDebugMode()
65 {
66 $this->api_url = $this->debug_url;
67 $this->debug_mode_enabled = true;
68 }
69
70 public function disableDebugMode()
71 {
72 $this->api_url = $this->production_url;
73 $this->debug_mode_enabled = false;
74 }
75
76 public function isDebugModeEnabled()
77 {
78 return $this->debug_mode_enabled;
79 }
80
81 /**
82 * @return string
83 */
84 public function getKey()
85 {
86 return $this->key;
87 }
88
89 /**
90 * @param string $key
91 */
92 public function setKey($key)
93 {
94 $this->key = $key;
95 }
96
97 /**
98 * @return string
99 */
100 public function getApiUrl()
101 {
102 return $this->api_url;
103 }
104
105 /**
106 * @param string $api_url
107 */
108 public function setApiUrl($api_url)
109 {
110 if ($this->isDebugModeEnabled()) {
111 $this->debug_url = $api_url;
112 } else {
113 $this->production_url = $api_url;
114 }
115 $this->api_url = $api_url;
116 }
117
118
119 /**
120 * @return Requests\CourierListCitiesRequest
121 */
122 public static function getCourierListCities()
123 {
124 return new Requests\CourierListCitiesRequest();
125 }
126
127 /**
128 * @return Requests\DeliveryCostsRequest
129 */
130 public static function getDeliveryCosts()
131 {
132 return new Requests\DeliveryCostsRequest();
133 }
134
135 /**
136 * @return Requests\WidgetSettingsRequest
137 */
138 public static function getWidgetSettings()
139 {
140 return new Requests\WidgetSettingsRequest();
141 }
142
143 /**
144 * @return Requests\GetKeyIntegration
145 */
146 public static function getKeyIntegration()
147 {
148 return new Requests\GetKeyIntegration();
149 }
150
151 /**
152 * @return Requests\ListCitiesRequest
153 */
154 public static function getListCities()
155 {
156 return new Requests\ListCitiesRequest();
157 }
158
159 /**
160 * @return Requests\ListCitiesFullRequest
161 */
162 public static function getListCitiesFull()
163 {
164 return new Requests\ListCitiesFullRequest();
165 }
166
167 /**
168 * @return Requests\ListPointsRequest
169 */
170 public static function getListPoints()
171 {
172 return new Requests\ListPointsRequest();
173 }
174
175 /**
176 * @return Requests\ListServicesRequest
177 */
178 public static function getListServices()
179 {
180 return new Requests\ListServicesRequest();
181 }
182
183 /**
184 * @return Requests\ListStatusesRequest
185 */
186 public static function getListStatuses()
187 {
188 return new Requests\ListStatusesRequest();
189 }
190
191 /**
192 * @return Requests\ListStatusesFullRequest
193 */
194 public static function getListStatusesFull()
195 {
196 return new Requests\ListStatusesFullRequest();
197 }
198
199 /**
200 * @return Requests\ListZipsRequest
201 */
202 public static function getListZips()
203 {
204 return new Requests\ListZipsRequest();
205 }
206
207 /**
208 * @return Requests\ZipCheckRequest
209 */
210 public static function getZipCheck()
211 {
212 return new Requests\ZipCheckRequest();
213 }
214
215 /**
216 * @return Requests\ParselCheckRequest
217 */
218 public static function getParselCheck()
219 {
220 return new Requests\ParselCheckRequest();
221 }
222
223 /**
224 * @return Requests\ParselCreateRequest
225 */
226 public static function getParselCreate()
227 {
228 return new Requests\ParselCreateRequest();
229 }
230
231 /**
232 * @return Requests\ParselDelRequest
233 */
234 public static function getParselDel()
235 {
236 return new Requests\ParselDelRequest();
237 }
238
239 /**
240 * @return Requests\ParselListRequest
241 */
242 public static function getParselList()
243 {
244 return new Requests\ParselListRequest();
245 }
246
247 /**
248 * @return Requests\ParselSendRequest
249 */
250 public static function getParselSend()
251 {
252 return new Requests\ParselSendRequest();
253 }
254
255 /**
256 * @return Requests\ParselSendStoryRequest
257 */
258 public static function getParselSendStory()
259 {
260 return new Requests\ParselSendStoryRequest();
261 }
262
263 /**
264 * @return Requests\ParselStoryRequest
265 */
266 public static function getParselStory()
267 {
268 return new Requests\ParselStoryRequest();
269 }
270
271 /**
272 * @return Requests\PointsByPostcodeRequest
273 */
274 public static function getPointsByPostCode()
275 {
276 return new Requests\PointsByPostcodeRequest();
277 }
278
279 /**
280 * @return Requests\PointsDescriptionRequest
281 */
282 public static function getPointsDescription()
283 {
284 return new Requests\PointsDescriptionRequest();
285 }
286
287 /**
288 * @return Requests\PointsForParcelsRequest
289 */
290 public static function getPointsForParcels()
291 {
292 return new Requests\PointsForParcelsRequest();
293 }
294
295 /**
296 * Проверяет правильно ли заполнен request, подготавливает запрос для отправки.
297 * Возвращается тип, который ожидался в нужном request.
298 * @param Request $request
299 * @return Object|bool
300 * @throws BadSettingsException
301 * @throws Requests\Exceptions\RequiredFieldsNullException
302 * @throws UnknownTypeException
303 */
304 public function execute( $request)
305 {
306// die ('abbbbbbbbbbbbbbbbbb'); єта строка не достигается.
307
308 if ($this->api_url === null || $this->key === null) {
309 throw new BadSettingsException('Проверьте секретный ключ и адрес апи');
310 }
311
312 if ($request->checkRequiredFields() === false) {
313 throw new Requests\Exceptions\RequiredFieldsNullException('Не все обязательные параметры заполнены');
314 }
315
316 $serializer = new Serializer();
317
318 if (property_exists($request, 'method') && $request->method == 'POST') {
319 $data = array(
320 'method' => $request->getClassName(),
321 'token' => $this->key,
322 'sdata' => json_encode($serializer->toArray($request))
323 );
324 $answer = HTTP::post($this->api_url, $data)->getAnswer();
325 } else {
326 $data = array(
327 'method' => $request->getClassName(),
328 'token' => $this->key
329 );
330 $data = array_merge($data, $serializer->toArray($request));
331 $answer = HTTP::get($this->api_url, $data)->getAnswer();
332 }
333 /////////////
334 unset($serializer);
335 $answerClass = $request->getResultClass();
336 $type = $this->getType($answerClass);
337
338 if ($type === false) {
339 throw new UnknownTypeException('Неизвестная ошибка');
340 }
341
342 if ($type == 'class') {
343 return new $answerClass($answer);
344 } elseif ($type == 'bool') {
345 return $answer['text'] == 'ok';
346 } else {
347 throw new UnknownTypeException('Неизвестная ошибка');
348 }
349
350 }
351
352 /**
353 * Проверяет принадлежит ли возвращаемый тип классу или типу bool, иначе возвращает false
354 * @param $type
355 * @return bool|string
356 */
357 protected function getType($type)
358 {
359 if (class_exists($type)) {
360 return 'class';
361 } elseif ($type == 'bool') {
362 return 'bool';
363 }
364 return false;
365 }
366}