Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MiniShop3\Controllers\Api\Web;

use MiniShop3\Router\ApiErrorCode;
use MiniShop3\Router\DomainMs2Response;
use MiniShop3\Router\HttpStatus;
use MiniShop3\Router\Response;
use MiniShop3\Services\Api\WebApiContextResolver;
Expand Down Expand Up @@ -30,9 +32,9 @@ public function __construct(modX $modx)
* POST /api/v1/cart/add
*
* @param array $params URL parameters
* @return array Response ['success' => bool, 'message' => '', 'data' => [...]]
* @return Response
*/
public function add(array $params = []): array
public function add(array $params = []): Response
{
$input = $this->getRequestData();

Expand All @@ -59,9 +61,9 @@ public function add(array $params = []): array
* POST /api/v1/cart/change
*
* @param array $params URL parameters
* @return array Response ['success' => bool, 'message' => '', 'data' => [...]]
* @return Response
*/
public function change(array $params = []): array
public function change(array $params = []): Response
{
$input = $this->getRequestData();

Expand All @@ -77,7 +79,7 @@ public function change(array $params = []): array
return Response::error(
$this->modx->lexicon('ms3_err_product_key_required'),
HttpStatus::BAD_REQUEST
)->getData();
);
}

$ms3 = $this->modx->services->get('ms3');
Expand All @@ -94,9 +96,9 @@ public function change(array $params = []): array
* POST /api/v1/cart/change-option
*
* @param array $params URL parameters
* @return array Response ['success' => bool, 'message' => '', 'data' => [...]]
* @return Response
*/
public function changeOption(array $params = []): array
public function changeOption(array $params = []): Response
{
$input = $this->getRequestData();

Expand All @@ -112,14 +114,14 @@ public function changeOption(array $params = []): array
return Response::error(
$this->modx->lexicon('ms3_err_product_key_required'),
HttpStatus::BAD_REQUEST
)->getData();
);
}

if (!is_array($options) || $options === []) {
return Response::error(
$this->modx->lexicon('ms3_cart_change_options_error'),
HttpStatus::BAD_REQUEST
)->getData();
);
}

$ms3 = $this->modx->services->get('ms3');
Expand All @@ -136,9 +138,9 @@ public function changeOption(array $params = []): array
* POST /api/v1/cart/remove
*
* @param array $params URL parameters
* @return array Response ['success' => bool, 'message' => '', 'data' => [...]]
* @return Response
*/
public function remove(array $params = []): array
public function remove(array $params = []): Response
{
$input = $this->getRequestData();

Expand All @@ -153,7 +155,7 @@ public function remove(array $params = []): array
return Response::error(
$this->modx->lexicon('ms3_err_product_key_required'),
HttpStatus::BAD_REQUEST
)->getData();
);
}

$ms3 = $this->modx->services->get('ms3');
Expand All @@ -170,9 +172,9 @@ public function remove(array $params = []): array
* GET /api/v1/cart/get
*
* @param array $params URL parameters
* @return array Response ['success' => bool, 'message' => '', 'data' => [...]]
* @return Response
*/
public function get(array $params = []): array
public function get(array $params = []): Response
{
$token = $_REQUEST['ms3_token'] ?? '';

Expand All @@ -194,9 +196,9 @@ public function get(array $params = []): array
* POST /api/v1/cart/clean
*
* @param array $params URL parameters
* @return array Response ['success' => bool, 'message' => '', 'data' => [...]]
* @return Response
*/
public function clean(array $params = []): array
public function clean(array $params = []): Response
{
$token = $_REQUEST['ms3_token'] ?? '';

Expand All @@ -214,14 +216,15 @@ public function clean(array $params = []): array
}

/**
* @return array{success: bool, message: string, code: int, errors: mixed}
* @return Response
*/
private function tokenRequiredError(): array
private function tokenRequiredError(): Response
{
return Response::error(
return Response::errorWithCode(
ApiErrorCode::TOKEN_REQUIRED,
$this->modx->lexicon('ms3_customer_err_token_required'),
HttpStatus::UNAUTHORIZED
)->getData();
);
}

/**
Expand Down Expand Up @@ -251,34 +254,29 @@ protected function getRequestData(): array
}

/**
* Transform Cart response to API format
* Transform Cart domain MS2-array to Web API Response (#572).
*
* @param array $result Response from Cart controller
* @return array Response in API format ['success' => bool, 'message' => '', 'data' => [...]]
* @param array<string, mixed> $result
* @return Response
*/
protected function transformResponse(array $result): array
protected function transformResponse(array $result): Response
{
$input = $this->getRequestData();
$renderTokens = $input['render'] ?? null;

if (!empty($renderTokens) && $result['success']) {
if (!empty($renderTokens) && !empty($result['success'])) {
$customerToken = $_REQUEST['ms3_token'] ?? '';

$renderedHtml = $this->renderSnippets($renderTokens, $customerToken);
if (!empty($renderedHtml)) {
if (!empty($renderedHtml) && is_array($result['data'] ?? null)) {
$result['data']['render'] = $renderedHtml;
}
}

if ($result['success']) {
return Response::success($result['data'], $result['message'] ?? '')->getData();
} else {
return Response::error(
$result['message'] ?? $this->modx->lexicon('ms3_err_unknown'),
HttpStatus::BAD_REQUEST,
$result['data'] ?? []
)->getData();
}
return DomainMs2Response::fromDomain(
$result,
$this->modx->lexicon('ms3_err_unknown')
);
}

/**
Expand Down
Loading