Skip to content

Commit

Permalink
Merge branch 'WP-57' into 'main'
Browse files Browse the repository at this point in the history
chore: refactoring

See merge request ecommerce_modules/cms/wordpress/wordpress!28
  • Loading branch information
vermorag committed Sep 17, 2024
2 parents e71ee81 + f54c7c0 commit e20223d
Show file tree
Hide file tree
Showing 37 changed files with 769 additions and 681 deletions.
10 changes: 3 additions & 7 deletions src/Actions/CalculateDeliveryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,11 @@ static function ($carry, $item) use ($package) {
continue;
}

$delivery = json_decode($calcResult, true);

if (!$this->checkDeliveryResponse($delivery)) {
if (!$this->checkDeliveryResponse($calcResult)) {
continue;
}

foreach ($delivery['tariff_codes'] as $tariff) {
foreach ($calcResult['tariff_codes'] as $tariff) {
if (isset($this->rates[$tariff['tariff_code']]) ||
!in_array((string) $tariff['tariff_code'], $tariffList ?: [], true)) {
continue;
Expand Down Expand Up @@ -196,9 +194,7 @@ static function ($carry, $item) use ($package) {
return $tariff;
}

$delivery = json_decode($tariffInfo, true);

$cost = $delivery['total_sum'];
$cost = $tariffInfo['total_sum'];

if (isset($rule['type']) && $rule['type'] === 'amount') {
$cost += $rule['value'];
Expand Down
75 changes: 48 additions & 27 deletions src/Actions/CallCourier.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ public function call(int $orderId, $data)
$param = $this->createRequestData($data);
}

$response = $this->api->callCourier($param);
$courierObj = json_decode($response);
$courierObj = $this->api->callCourier($param);

if (property_exists($courierObj, 'errors') &&
$courierObj->errors[0]->code === 'v2_intake_exists_by_order') {
if (isset($courierObj['errors']) && $courierObj['errors'][0]['code'] === 'v2_intake_exists_by_order') {
$validate
= new Validate(false,
esc_html__('An error occurred while creating request. Request to call a courier for this invoice already exists',
'cdekdelivery'));
= new Validate(
false, esc_html__(
'An error occurred while creating request. Request to call a courier for this invoice already exists',
'cdekdelivery',
),
);

return $validate->response();
}
Expand All @@ -72,8 +73,7 @@ public function call(int $orderId, $data)
return $validate->response();
}

$courierInfoJson = $this->api->courierInfo($courierObj->entity->uuid);
$courierInfo = json_decode($courierInfoJson);
$courierInfo = $this->api->courierInfo($courierObj['entity']['uuid']);

sleep(5);

Expand All @@ -82,28 +82,47 @@ public function call(int $orderId, $data)
return $validate->response();
}

if (!property_exists($courierInfo, 'entity')) {
if (!isset($courierInfo['entity'])) {
$validate
= new Validate(false, sprintf(esc_html__(/* translators: %s: uuid of request*/ 'Request has been created, but an error occurred while obtaining the request number. Request uuid: %s', 'cdekdelivery'),
$courierInfo->requests[0]->request_uuid));
= new Validate(
false, sprintf(
esc_html__(/* translators: %s: uuid of request*/
'Request has been created, but an error occurred while obtaining the request number. Request uuid: %s',
'cdekdelivery',
),
$courierInfo['requests'][0]['request_uuid'],
),
);

return $validate->response();
}

$intakeNumber = $courierInfo->entity->intake_number;
$intakeNumber = $courierInfo['entity']['intake_number'];

CourierMetaData::addMetaByOrderId($orderId, [
'courier_number' => $intakeNumber,
'courier_uuid' => $courierObj->entity->uuid,
'not_cons' => Tariff::isTariffFromDoor($tariffId),
]);
'courier_number' => $intakeNumber,
'courier_uuid' => $courierObj['entity']['uuid'],
'not_cons' => Tariff::isTariffFromDoor($tariffId),
]);

$message
= sprintf(esc_html__(/* translators: 1: number of request 2: uuid of request*/'Request has been created to call a courier: Number: %1$s | Uuid: %2$s', 'cdekdelivery'),
$intakeNumber, $courierObj->entity->uuid);
= sprintf(
esc_html__(/* translators: 1: number of request 2: uuid of request*/
'Request has been created to call a courier: Number: %1$s | Uuid: %2$s',
'cdekdelivery',
),
$intakeNumber,
$courierObj['entity']['uuid'],
);
Note::send($orderId, $message);

$validate = new Validate(true, sprintf(esc_html__(/* translators: %s: uuid of application*/'Request number: %s', 'cdekdelivery'), $intakeNumber));
$validate = new Validate(
true,
sprintf(
esc_html__(/* translators: %s: uuid of application*/ 'Request number: %s', 'cdekdelivery'),
$intakeNumber,
),
);

return $validate->response();
}
Expand Down Expand Up @@ -190,11 +209,10 @@ public function delete($orderId)
return $validate->response();
}

$response = $this->api->callCourierDelete($courierMeta['courier_uuid']);
$courierObj = json_decode($response);
$courierObj = $this->api->callCourierDelete($courierMeta['courier_uuid']);

if (property_exists($courierObj, 'errors') &&
$courierObj->errors[0]->code === 'v2_entity_has_final_status') {
if (isset($courierObj['errors']) &&
$courierObj['errors'][0]['code'] === 'v2_entity_has_final_status') {
$validate = new Validate(true);

return $validate->response();
Expand All @@ -207,9 +225,12 @@ public function delete($orderId)

CourierMetaData::cleanMetaByOrderId($orderId);

$message = sprintf(esc_html__(/* translators: %s: request number */'Deleting a request to call a courier: %s',
'cdekdelivery'),
$courierObj->entity->uuid);
$message = sprintf(
esc_html__(/* translators: %s: request number */ 'Deleting a request to call a courier: %s',
'cdekdelivery',
),
$courierObj['entity']['uuid'],
);

Note::send($orderId, $message);

Expand Down
3 changes: 1 addition & 2 deletions src/Actions/CreateOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ private function getCdekOrderNumber(string $orderUuid, int $iteration = 1): ?str
return null;
}

$orderInfoJson = $this->api->getOrder($orderUuid);
$orderInfo = json_decode($orderInfoJson, true);
$orderInfo = $this->api->getOrder($orderUuid);

return $orderInfo['entity']['cdek_number'] ?? $this->getCdekOrderNumber($orderUuid, $iteration + 1);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Actions/DeleteOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ public function __invoke(int $orderId): array
OrderMetaData::cleanMetaByOrderId($orderId);

$order = $this->api->getOrder($orderNumber);
$orderObj = json_decode($order);

$validate = ValidateGetOrder::validate($orderObj, $orderNumber, $orderId);
$validate = ValidateGetOrder::validate($order, $orderNumber, $orderId);
if (!$validate->state) {
return $validate->response();
}

$delete = $this->api->deleteOrder($orderNumber);
$delete = json_decode($delete);

$validate = ValidateDeleteOrder::validate($delete, $orderNumber, $orderId);
if (!$validate->state) {
Expand Down
6 changes: 3 additions & 3 deletions src/Actions/GenerateBarcodeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __invoke(string $cdekUuid): array
if (isset($order['related_entities'])) {
foreach ($order['related_entities'] as $entity) {
if ($entity['type'] === 'barcode' && isset($entity['url'])) {
$barcodeInfo = json_decode($this->api->getBarcode($entity['uuid']), true);
$barcodeInfo = $this->api->getBarcode($entity['uuid']);

if ($barcodeInfo['entity']['format'] !==
BarcodeFormat::getByIndex(Helper::getActualShippingMethod()
Expand All @@ -56,7 +56,7 @@ public function __invoke(string $cdekUuid): array
}
}

$barcode = json_decode($this->api->createBarcode($order['entity']['uuid']), true);
$barcode = $this->api->createBarcode($order['entity']['uuid']);

if (!isset($barcode['entity'])) {
return [
Expand All @@ -69,7 +69,7 @@ public function __invoke(string $cdekUuid): array
sleep(Config::GRAPHICS_FIRST_SLEEP);

for ($i = 0; $i < Config::MAX_REQUEST_RETRIES_FOR_GRAPHICS; $i++) {
$barcodeInfo = json_decode($this->api->getBarcode($barcode['entity']['uuid']), true);
$barcodeInfo = $this->api->getBarcode($barcode['entity']['uuid']);

if (isset($barcodeInfo['entity']['url'])) {
return [
Expand Down
7 changes: 3 additions & 4 deletions src/Actions/GenerateWaybillAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke(string $orderUuid): array
Config::GRAPHICS_FIRST_SLEEP +
Config::GRAPHICS_TIMEOUT_SEC * Config::MAX_REQUEST_RETRIES_FOR_GRAPHICS);

$order = json_decode($this->api->getOrder($orderUuid), true);
$order = $this->api->getOrder($orderUuid);

if (!isset($order['entity'])) {
return [
Expand All @@ -46,7 +46,7 @@ public function __invoke(string $orderUuid): array
}
}

$waybill = json_decode($this->api->createWaybill($order['entity']['uuid']), true, 512, JSON_THROW_ON_ERROR);
$waybill = $this->api->createWaybill($order['entity']['uuid']);

if (!isset($waybill['entity'])) {
return [
Expand All @@ -59,8 +59,7 @@ public function __invoke(string $orderUuid): array
sleep(Config::GRAPHICS_FIRST_SLEEP);

for ($i = 0; $i < Config::MAX_REQUEST_RETRIES_FOR_GRAPHICS; $i++) {
$waybillInfo = json_decode($this->api->getWaybill($waybill['entity']['uuid']), true, 512,
JSON_THROW_ON_ERROR);
$waybillInfo = $this->api->getWaybill($waybill['entity']['uuid']);

if (isset($waybillInfo['entity']['url'])) {
return [
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/Schedule/CollectOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function start(): void

$maxPages = $result->max_num_pages;

$response = $this->cdekCoreApi->sendTaskData(
$response = $this->cdekCoreApi->putTaskResult(
$this->taskId,
new TaskOutputData(
'success',
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/Schedule/ReindexOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function start(): void
);
}

$this->initData($this->cdekCoreApi->sendTaskData(
$this->initData($this->cdekCoreApi->putTaskResult(
$this->taskId,
new TaskOutputData('success'),
));
Expand Down
Loading

0 comments on commit e20223d

Please sign in to comment.