Skip to content

Commit

Permalink
fix: json encode for live sync (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john authored Feb 4, 2025
1 parent b9abf26 commit 000895c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 72 deletions.
18 changes: 6 additions & 12 deletions src/Api/CloudSyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

namespace PrestaShop\Module\PsEventbus\Api;

use PrestaShop\Module\PsEventbus\Formatter\JsonFormatter;
use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService;

if (!defined('_PS_VERSION_')) {
Expand Down Expand Up @@ -60,11 +59,6 @@ class CloudSyncClient
*/
private $module;

/**
* @var JsonFormatter
*/
private $jsonFormatter;

/**
* Accounts JSON Web token
*
Expand Down Expand Up @@ -94,20 +88,17 @@ class CloudSyncClient
* @param string $syncApiUrl
* @param \Ps_eventbus $module
* @param PsAccountsAdapterService $psAccountsAdapterService
* @param JsonFormatter $jsonFormatter
*/
public function __construct(
$collectorApiUrl,
$liveSyncApiUrl,
$syncApiUrl,
\Ps_eventbus $module,
PsAccountsAdapterService $psAccountsAdapterService,
JsonFormatter $jsonFormatter
PsAccountsAdapterService $psAccountsAdapterService
) {
$this->module = $module;
$this->jwt = $psAccountsAdapterService->getOrRefreshToken();
$this->shopId = $psAccountsAdapterService->getShopUuid();
$this->jsonFormatter = $jsonFormatter;

$this->collectorApiUrl = $collectorApiUrl;
$this->liveSyncApiUrl = $liveSyncApiUrl;
Expand Down Expand Up @@ -141,7 +132,7 @@ public function upload($jobId, $data, $startTime, $fullSyncRequested = null)
'Full-Sync-Requested' => $fullSyncRequested ? '1' : '0',
'User-Agent' => 'ps-eventbus/' . $this->module->version,
],
$this->jsonFormatter->formatNewlineJsonString($data),
$data,
true
);

Expand Down Expand Up @@ -172,7 +163,10 @@ public function liveSync($shopContent, $action)
'User-Agent' => 'ps-eventbus/' . $this->module->version,
'Content-Type' => 'application/json',
],
'{"shopContents": ["' . $kebabCasedShopContent . '"], "action": "' . $action . '"}'
[
'shopContents' => [$kebabCasedShopContent],
'action' => $action,
]
);

return [
Expand Down
22 changes: 19 additions & 3 deletions src/Api/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ private function init()
return $this;
}

/**
* @param array<mixed> $data
*
* @return string
*/
private function formatNewlineJsonString($data)
{
$jsonArray = array_map(function ($dataItem) {
return json_encode($dataItem, JSON_UNESCAPED_SLASHES);
}, $data);

$json = implode("\r\n", $jsonArray);

return str_replace('\\u0000', '', $json);
}

/**
* Set the timeout for the current request.
*
Expand Down Expand Up @@ -355,12 +371,12 @@ public function get($url, $headers, $data = null)
*
* @param string $url The url to make the post request
* @param array $headers Optional headers to pass to the url
* @param array|object|string $data Post data to pass to the url
* @param array $data Post data to pass to the url
* @param bool $isFile If there is a multipart, set it to true or False if it is a json
*
* @return self
*/
public function post($url, $headers = null, $data = null, $isFile = null)
public function post($url, array $headers = [], array $data = [], $isFile = null)
{
if (is_null($isFile)) {
$isFile = false;
Expand All @@ -372,7 +388,7 @@ public function post($url, $headers = null, $data = null, $isFile = null)
if ($isFile) {
// Créer un fichier temporaire
$temp = tmpfile();
fwrite($temp, $data);
fwrite($temp, $this->formatNewlineJsonString($data));
rewind($temp);

// Sauvegarder le fichier temporaire pour cURL
Expand Down
50 changes: 0 additions & 50 deletions src/Formatter/JsonFormatter.php

This file was deleted.

4 changes: 1 addition & 3 deletions src/ServiceContainer/Provider/ApiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace PrestaShop\Module\PsEventbus\ServiceContainer\Provider;

use PrestaShop\Module\PsEventbus\Api\CloudSyncClient;
use PrestaShop\Module\PsEventbus\Formatter\JsonFormatter;
use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService;
use PrestaShop\Module\PsEventbus\ServiceContainer\Contract\IServiceProvider;
use PrestaShop\Module\PsEventbus\ServiceContainer\ServiceContainer;
Expand All @@ -41,8 +40,7 @@ public function provide(ServiceContainer $container)
$container->getParameter('ps_eventbus.live_sync_api_url'),
$container->getParameter('ps_eventbus.sync_api_url'),
$container->get('ps_eventbus.module'),
$container->get(PsAccountsAdapterService::class),
$container->get(JsonFormatter::class)
$container->get(PsAccountsAdapterService::class)
);
});
}
Expand Down
4 changes: 0 additions & 4 deletions src/ServiceContainer/Provider/CommonProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace PrestaShop\Module\PsEventbus\ServiceContainer\Provider;

use PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter;
use PrestaShop\Module\PsEventbus\Formatter\JsonFormatter;
use PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler;
use PrestaShop\Module\PsEventbus\Helper\ModuleHelper;
use PrestaShop\Module\PsEventbus\Service\PresenterService;
Expand Down Expand Up @@ -53,9 +52,6 @@ public function provide(ServiceContainer $container)
$container->get(ErrorHandler::class)
);
});
$container->registerProvider(JsonFormatter::class, static function () {
return new JsonFormatter();
});
$container->registerProvider(ArrayFormatter::class, static function () {
return new ArrayFormatter();
});
Expand Down

0 comments on commit 000895c

Please sign in to comment.