Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
32 | class PartnerClient extends AbstractServiceClient |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Order is being processed |
||
37 | */ |
||
38 | const ORDER_STATUS_PROCESSING = 'PROCESSING'; |
||
39 | |||
40 | /** |
||
41 | * Order submitted to the delivery |
||
42 | */ |
||
43 | const ORDER_STATUS_DELIVERY = 'DELIVERY'; |
||
44 | |||
45 | /** |
||
46 | * Order delivered to the point of self-delivery |
||
47 | */ |
||
48 | const ORDER_STATUS_PICKUP = 'PICKUP'; |
||
49 | |||
50 | /** |
||
51 | * The order is received by the buyer |
||
52 | */ |
||
53 | const ORDER_STATUS_DELIVERED = 'DELIVERED'; |
||
54 | |||
55 | /** |
||
56 | * Order canceled. |
||
57 | */ |
||
58 | const ORDER_STATUS_CANCELLED = 'CANCELLED'; |
||
59 | |||
60 | //Sub-statuses for status CANCELLED |
||
61 | // - the buyer is not finalized the reserved order on time |
||
62 | const ORDER_SUBSTATUS_RESERVATION_EXPIRED = 'RESERVATION_EXPIRED'; |
||
63 | // - the buyer did not pay for the order ( for the type of payment PREPAID) |
||
64 | const ORDER_SUBSTATUS_USER_NOT_PAID = 'USER_NOT_PAID'; |
||
65 | // - failed to communicate with the buyer |
||
66 | const ORDER_SUBSTATUS_USER_UNREACHABLE = 'USER_UNREACHABLE'; |
||
67 | // - buyer canceled the order for cause |
||
68 | const ORDER_SUBSTATUS_USER_CHANGED_MIND = 'USER_CHANGED_MIND'; |
||
69 | // - the buyer is not satisfied with the terms of delivery |
||
70 | const ORDER_SUBSTATUS_USER_REFUSED_DELIVERY = 'USER_REFUSED_DELIVERY'; |
||
71 | // - the buyer did not fit the goods |
||
72 | const ORDER_SUBSTATUS_USER_REFUSED_PRODUCT = 'USER_REFUSED_PRODUCT'; |
||
73 | // - shop can not fulfill the order |
||
74 | const ORDER_SUBSTATUS_SHOP_FAILED = 'SHOP_FAILED'; |
||
75 | // - the buyer is not satisfied with the quality of the goods |
||
76 | const ORDER_SUBSTATUS_USER_REFUSED_QUALITY = 'USER_REFUSED_QUALITY'; |
||
77 | // - buyer changes the composition of the order |
||
78 | const ORDER_SUBSTATUS_REPLACING_ORDER = 'REPLACING_ORDER'; |
||
79 | //- store does not process orders on time |
||
80 | const ORDER_SUBSTATUS_PROCESSING_EXPIRED = 'PROCESSING_EXPIRED'; |
||
81 | |||
82 | //Способ оплаты заказа |
||
83 | //предоплата через Яндекс; |
||
84 | const PAYMENT_METHOD_YANDEX = 'YANDEX'; |
||
85 | //предоплата напрямую магазину, |
||
86 | //не принимающему предоплату через Яндекс. |
||
87 | const PAYMENT_METHOD_SHOP_PREPAID = 'SHOP_PREPAID'; |
||
88 | // наличный расчет при получении заказа; |
||
89 | const PAYMENT_METHOD_CASH_ON_DELIVERY = 'CASH_ON_DELIVERY'; |
||
90 | // оплата банковской картой при получении заказа. |
||
91 | const PAYMENT_METHOD_CARD_ON_DELIVERY = 'CARD_ON_DELIVERY'; |
||
92 | |||
93 | //Типы доставки |
||
94 | //курьерская доставка |
||
95 | const DELIVERY_TYPE_DELIVERY = 'DELIVERY'; |
||
96 | //самовывоз |
||
97 | const DELIVERY_TYPE_PICKUP = 'PICKUP'; |
||
98 | //почта |
||
99 | const DELIVERY_TYPE_POST = 'POST'; |
||
100 | |||
101 | const ORDER_DECLINE_REASON_OUT_OF_DATE = 'OUT_OF_DATE'; |
||
102 | |||
103 | /** |
||
104 | * Requested version of API |
||
105 | * @var string |
||
106 | */ |
||
107 | private $version = 'v2'; |
||
108 | |||
109 | /** |
||
110 | * Application id |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | protected $clientId; |
||
115 | |||
116 | /** |
||
117 | * User login |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | protected $login; |
||
122 | |||
123 | /** |
||
124 | * Campaign Id |
||
125 | * |
||
126 | * @var string |
||
127 | */ |
||
128 | protected $campaignId; |
||
129 | |||
130 | /** |
||
131 | * API domain |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $serviceDomain = 'api.partner.market.yandex.ru'; |
||
136 | |||
137 | /** |
||
138 | * Get url to service resource with parameters |
||
139 | * |
||
140 | * @param string $resource |
||
141 | * @see http://api.yandex.ru/market/partner/doc/dg/concepts/method-call.xml |
||
142 | * @return string |
||
143 | */ |
||
144 | 13 | public function getServiceUrl($resource = '') |
|
149 | |||
150 | /** |
||
151 | * @param string $token access token |
||
152 | */ |
||
153 | 15 | public function __construct($token = '') |
|
157 | |||
158 | /** |
||
159 | * @param string $clientId |
||
160 | */ |
||
161 | 1 | public function setClientId($clientId) |
|
165 | |||
166 | /** |
||
167 | * @param string $login |
||
168 | */ |
||
169 | 1 | public function setLogin($login) |
|
173 | |||
174 | /** |
||
175 | * @param string $campaignId |
||
176 | */ |
||
177 | 1 | public function setCampaignId($campaignId) |
|
181 | |||
182 | /** |
||
183 | * @return string |
||
184 | */ |
||
185 | 1 | public function getCampaignId() |
|
186 | { |
||
187 | 1 | return $this->campaignId; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | 1 | public function getClientId() |
|
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | 1 | public function getLogin() |
|
205 | |||
206 | /** |
||
207 | * Sends a request |
||
208 | * |
||
209 | * @param string $method HTTP method |
||
210 | * @param string|UriInterface $uri URI object or string. |
||
211 | * @param array $options Request options to apply. |
||
212 | * |
||
213 | * @return Response |
||
214 | * |
||
215 | * @throws ForbiddenException |
||
216 | * @throws UnauthorizedException |
||
217 | * @throws PartnerRequestException |
||
218 | */ |
||
219 | 5 | View Code Duplication | protected function sendRequest($method, $uri, array $options = []) |
252 | |||
253 | /** |
||
254 | * Get OAuth data for header request |
||
255 | * |
||
256 | * @see http://api.yandex.ru/market/partner/doc/dg/concepts/authorization.xml |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | 1 | public function getAccessToken() |
|
265 | |||
266 | /** |
||
267 | * Get Balance Campaign |
||
268 | * @link https://tech.yandex.ru/market/partner/doc/dg/reference/get-campaigns-id-balance-docpage/ |
||
269 | * |
||
270 | * @return Models\Balance |
||
271 | */ |
||
272 | 1 | View Code Duplication | public function getBalance() |
273 | { |
||
274 | 1 | $resource = 'campaigns/' . $this->campaignId . '/balance.json'; |
|
275 | |||
276 | 1 | $response = $this->sendRequest('GET', $this->getServiceUrl($resource)); |
|
277 | |||
278 | 1 | $decodedResponseBody = $this->getDecodedBody($response->getBody()); |
|
279 | |||
280 | 1 | $getBalanceResponse = new Models\GetBalanceResponse($decodedResponseBody); |
|
281 | 1 | return $getBalanceResponse->getBalance(); |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * Get User Campaigns |
||
286 | * |
||
287 | * Returns the user to the list of campaigns Yandex.market. |
||
288 | * The list coincides with the list of campaigns |
||
289 | * that are displayed in the partner interface Yandex.Market on page "My shops." |
||
290 | * |
||
291 | * @see http://api.yandex.ru/market/partner/doc/dg/reference/get-campaigns.xml |
||
292 | * |
||
293 | * @return Models\Campaigns |
||
294 | */ |
||
295 | 1 | View Code Duplication | public function getCampaigns() |
296 | { |
||
297 | 1 | $resource = 'campaigns.json'; |
|
298 | |||
299 | 1 | $response = $this->sendRequest('GET', $this->getServiceUrl($resource)); |
|
300 | |||
301 | 1 | $decodedResponseBody = $this->getDecodedBody($response->getBody()); |
|
302 | |||
303 | 1 | $getCampaignsResponse = new Models\GetCampaignsResponse($decodedResponseBody); |
|
304 | 1 | return $getCampaignsResponse->getCampaigns(); |
|
305 | } |
||
306 | |||
307 | /** |
||
308 | * Get User Campaigns by Login |
||
309 | * |
||
310 | * @link https://tech.yandex.ru/market/partner/doc/dg/reference/get-campaigns-by-login-docpage/ |
||
311 | * |
||
312 | * @return Models\Campaigns |
||
313 | */ |
||
314 | View Code Duplication | public function getCampaignsByLogin($login) |
|
315 | { |
||
316 | $resource = 'campaigns/by_login/' . $login . '.json'; |
||
317 | |||
318 | $response = $this->sendRequest('GET', $this->getServiceUrl($resource)); |
||
319 | |||
320 | $decodedResponseBody = $this->getDecodedBody($response->getBody()); |
||
321 | |||
322 | $getCampaignsResponse = new Models\GetCampaignsResponse($decodedResponseBody); |
||
323 | return $getCampaignsResponse->getCampaigns(); |
||
324 | } |
||
325 | |||
326 | |||
327 | /** |
||
328 | * Get information about orders by campaign id |
||
329 | * |
||
330 | * @param array $params |
||
331 | * |
||
332 | * Returns information on the requested orders. |
||
333 | * Available filtering by date ordering and order status. |
||
334 | * The maximum range of dates in a single request for a resource - 30 days. |
||
335 | * |
||
336 | * @see http://api.yandex.ru/market/partner/doc/dg/reference/get-campaigns-id-orders.xml |
||
337 | * |
||
338 | * @return Models\GetOrdersResponse |
||
339 | */ |
||
340 | 1 | public function getOrdersResponse($params = []) |
|
341 | { |
||
342 | 1 | $resource = 'campaigns/' . $this->campaignId . '/orders.json'; |
|
343 | 1 | $resource .= '?' . http_build_query($params); |
|
344 | |||
345 | 1 | $response = $this->sendRequest('GET', $this->getServiceUrl($resource)); |
|
346 | |||
347 | 1 | $decodedResponseBody = $this->getDecodedBody($response->getBody()); |
|
348 | |||
349 | 1 | return new Models\GetOrdersResponse($decodedResponseBody); |
|
350 | } |
||
351 | |||
352 | |||
353 | /** |
||
354 | * Get only orders data without pagination |
||
355 | * |
||
356 | * @param array $params |
||
357 | * @return null|Models\Orders |
||
358 | */ |
||
359 | 1 | public function getOrders($params = []) |
|
363 | |||
364 | |||
365 | /** |
||
366 | * Get order info |
||
367 | * |
||
368 | * @param int $orderId |
||
369 | * @return Models\Order |
||
370 | * |
||
371 | * @link http://api.yandex.ru/market/partner/doc/dg/reference/get-campaigns-id-orders-id.xml |
||
372 | */ |
||
373 | 6 | View Code Duplication | public function getOrder($orderId) |
384 | |||
385 | /** |
||
386 | * Get Region |
||
387 | * |
||
388 | * @return Models\Region |
||
389 | * |
||
390 | * @link https://tech.yandex.ru/market/partner/doc/dg/reference/get-campaigns-id-region-docpage/ |
||
391 | */ |
||
392 | 1 | View Code Duplication | public function getRegion() |
403 | |||
404 | /** |
||
405 | * @param $method [main | main-daily | main-weekly | main-monthly] |
||
406 | * @param array $params |
||
407 | * @return Models\Stats |
||
408 | * |
||
409 | * @link https://tech.yandex.ru/market/partner/doc/dg/reference/get-campaigns-id-stats-main-docpage/ |
||
410 | */ |
||
411 | 1 | public function getStats($method, $params = []) |
|
423 | |||
424 | /** |
||
425 | * Send changed status to Yandex.Market |
||
426 | * |
||
427 | * @param int $orderId |
||
428 | * @param string $status |
||
429 | * @param null|string $subStatus |
||
430 | * @return Models\Order |
||
431 | * |
||
432 | * @link http://api.yandex.ru/market/partner/doc/dg/reference/put-campaigns-id-orders-id-status.xml |
||
433 | */ |
||
434 | 1 | public function setOrderStatus($orderId, $status, $subStatus = null) |
|
460 | |||
461 | |||
462 | /** |
||
463 | * Update changed delivery parameters |
||
464 | * |
||
465 | * @param int $orderId |
||
466 | * @param Models\Delivery $delivery |
||
467 | * @return Models\Order |
||
468 | * |
||
469 | * Example: |
||
470 | * PUT /v2/campaigns/10003/order/12345/delivery.json HTTP/1.1 |
||
471 | * |
||
472 | * @link http://api.yandex.ru/market/partner/doc/dg/reference/put-campaigns-id-orders-id-delivery.xml |
||
473 | */ |
||
474 | 1 | public function updateDelivery($orderId, Models\Delivery $delivery) |
|
493 | } |
||
494 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.