first commit

This commit is contained in:
2024-12-17 13:43:22 +01:00
commit 8e6cd8b410
21292 changed files with 3514826 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?php
namespace PrestaShop\Module\PsEventbus\Service;
use PrestaShop\Module\PsEventbus\Api\EventBusSyncClient;
use PrestaShop\Module\PsEventbus\Exception\EnvVarException;
use PrestaShop\Module\PsEventbus\Repository\EventbusSyncRepository;
use PrestaShopDatabaseException;
class ApiAuthorizationService
{
/**
* @var EventbusSyncRepository
*/
private $eventbusSyncStateRepository;
/**
* @var EventBusSyncClient
*/
private $eventBusSyncClient;
public function __construct(
EventbusSyncRepository $eventbusSyncStateRepository,
EventBusSyncClient $eventBusSyncClient
) {
$this->eventbusSyncStateRepository = $eventbusSyncStateRepository;
$this->eventBusSyncClient = $eventBusSyncClient;
}
/**
* Authorizes if the call to endpoint is legit and creates sync state if needed
*
* @param string $jobId
*
* @return array|bool
*
* @throws PrestaShopDatabaseException|EnvVarException
*/
public function authorizeCall($jobId)
{
$job = $this->eventbusSyncStateRepository->findJobById($jobId);
if ($job) {
return true;
}
$jobValidationResponse = $this->eventBusSyncClient->validateJobId($jobId);
if (is_array($jobValidationResponse) && (int) $jobValidationResponse['httpCode'] === 201) {
return $this->eventbusSyncStateRepository->insertJob($jobId, date(DATE_ATOM));
}
return $jobValidationResponse;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace PrestaShop\Module\PsEventbus\Service;
use Exception;
use PrestaShop\Module\PsEventbus\Formatter\JsonFormatter;
class CompressionService
{
/**
* @var JsonFormatter
*/
private $jsonFormatter;
public function __construct(JsonFormatter $jsonFormatter)
{
$this->jsonFormatter = $jsonFormatter;
}
/**
* Compresses data with gzip
*
* @param array $data
*
* @return string
*
* @throws Exception
*/
public function gzipCompressData($data)
{
if (!extension_loaded('zlib')) {
throw new Exception('Zlib extension for PHP is not enabled');
}
$dataJson = $this->jsonFormatter->formatNewlineJsonString($data);
if (!$encodedData = gzencode($dataJson)) {
throw new Exception('Failed encoding data to GZIP');
}
return $encodedData;
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace PrestaShop\Module\PsEventbus\Service;
use Context;
use PrestaShop\Module\PsEventbus\Exception\EnvVarException;
use PrestaShop\Module\PsEventbus\Repository\DeletedObjectsRepository;
use PrestaShopDatabaseException;
class DeletedObjectsService
{
/**
* @var Context
*/
private $context;
/**
* @var DeletedObjectsRepository
*/
private $deletedObjectsRepository;
/**
* @var ProxyService
*/
private $proxyService;
public function __construct(Context $context, DeletedObjectsRepository $deletedObjectsRepository, ProxyService $proxyService)
{
$this->context = $context;
$this->deletedObjectsRepository = $deletedObjectsRepository;
$this->proxyService = $proxyService;
}
/**
* @param string $jobId
*
* @return array
*
* @throws PrestaShopDatabaseException|EnvVarException
*/
public function handleDeletedObjectsSync($jobId)
{
$deletedObjects = $this->deletedObjectsRepository->getDeletedObjectsGrouped($this->context->shop->id);
if (empty($deletedObjects)) {
return [
'total_objects' => 0,
];
}
$data = $this->formatData($deletedObjects);
$response = $this->proxyService->delete($jobId, $data);
if ($response['httpCode'] == 200) {
foreach ($data as $dataItem) {
$this->deletedObjectsRepository->removeDeletedObjects(
$dataItem['collection'],
$dataItem['deleteIds'],
$this->context->shop->id
);
}
}
return array_merge(
[
'job_id' => $jobId,
'total_objects' => count($data),
],
$response
);
}
/**
* @param array $data
*
* @return array
*/
private function formatData(array $data)
{
return array_map(function ($dataItem) {
return [
'collection' => $dataItem['type'],
'deleteIds' => explode(';', $dataItem['ids']),
];
}, $data);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace PrestaShop\Module\PsEventbus\Service;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Ring\Exception\ConnectException;
use PrestaShop\Module\PsEventbus\Api\EventBusProxyClient;
use PrestaShop\Module\PsEventbus\Exception\EnvVarException;
use PrestaShop\Module\PsEventbus\Formatter\JsonFormatter;
class ProxyService
{
/**
* @var EventBusProxyClient
*/
private $eventBusProxyClient;
/**
* @var JsonFormatter
*/
private $jsonFormatter;
public function __construct(EventBusProxyClient $eventBusProxyClient, JsonFormatter $jsonFormatter)
{
$this->eventBusProxyClient = $eventBusProxyClient;
$this->jsonFormatter = $jsonFormatter;
}
/**
* @param string $jobId
* @param array $data
* @param int $scriptStartTime
*
* @return array
*
* @throws EnvVarException
*/
public function upload($jobId, $data, $scriptStartTime)
{
$dataJson = $this->jsonFormatter->formatNewlineJsonString($data);
try {
$response = $this->eventBusProxyClient->upload($jobId, $dataJson, $scriptStartTime);
} catch (ClientException $exception) {
return ['error' => $exception->getMessage()];
} catch (ConnectException $exception) {
return ['error' => $exception->getMessage()];
}
return $response;
}
/**
* @param string $jobId
* @param array $data
*
* @return array
*
* @throws EnvVarException
*/
public function delete($jobId, $data)
{
$dataJson = $this->jsonFormatter->formatNewlineJsonString($data);
try {
$response = $this->eventBusProxyClient->delete($jobId, $dataJson);
} catch (ClientException $exception) {
return ['error' => $exception->getMessage()];
}
return $response;
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace PrestaShop\Module\PsEventbus\Service;
use PrestaShop\Module\PsEventbus\Exception\ApiException;
use PrestaShop\Module\PsEventbus\Exception\EnvVarException;
use PrestaShop\Module\PsEventbus\Provider\PaginatedApiDataProviderInterface;
use PrestaShop\Module\PsEventbus\Repository\EventbusSyncRepository;
use PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository;
use PrestaShopDatabaseException;
class SynchronizationService
{
/**
* @var EventbusSyncRepository
*/
private $eventbusSyncRepository;
/**
* @var IncrementalSyncRepository
*/
private $incrementalSyncRepository;
/**
* @var ProxyService
*/
private $proxyService;
public function __construct(EventbusSyncRepository $eventbusSyncRepository, IncrementalSyncRepository $incrementalSyncRepository, ProxyService $proxyService)
{
$this->eventbusSyncRepository = $eventbusSyncRepository;
$this->incrementalSyncRepository = $incrementalSyncRepository;
$this->proxyService = $proxyService;
}
/**
* @param PaginatedApiDataProviderInterface $dataProvider
* @param string $type
* @param string $jobId
* @param string $langIso
* @param int $offset
* @param int $limit
* @param string $dateNow
* @param int $scriptStartTime
*
* @return array
*
* @throws PrestaShopDatabaseException|EnvVarException|ApiException
*/
public function handleFullSync(PaginatedApiDataProviderInterface $dataProvider, $type, $jobId, $langIso, $offset, $limit, $dateNow, $scriptStartTime)
{
$response = [];
$data = $dataProvider->getFormattedData($offset, $limit, $langIso);
if (!empty($data)) {
$response = $this->proxyService->upload($jobId, $data, $scriptStartTime);
if ($response['httpCode'] == 201) {
$offset += $limit;
}
}
$remainingObjects = (int) $dataProvider->getRemainingObjectsCount($offset, $langIso);
if ($remainingObjects <= 0) {
$remainingObjects = 0;
$offset = 0;
}
$this->eventbusSyncRepository->updateTypeSync($type, $offset, $dateNow, $remainingObjects == 0, $langIso);
return array_merge([
'total_objects' => count($data),
'has_remaining_objects' => $remainingObjects > 0,
'remaining_objects' => $remainingObjects,
'md5' => $this->getPayloadMd5($data),
], $response);
}
/**
* @param PaginatedApiDataProviderInterface $dataProvider
* @param string $type
* @param string $jobId
* @param int $limit
* @param string $langIso
* @param int $scriptStartTime
*
* @return array
*
* @throws PrestaShopDatabaseException|EnvVarException
*/
public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataProvider, $type, $jobId, $limit, $langIso, $scriptStartTime)
{
$response = [];
$incrementalData = $dataProvider->getFormattedDataIncremental($limit, $langIso);
$objectIds = $incrementalData['ids'];
$data = $incrementalData['data'];
if (!empty($data)) {
$response = $this->proxyService->upload($jobId, $data, $scriptStartTime);
if ($response['httpCode'] == 201 && !empty($objectIds)) {
$this->incrementalSyncRepository->removeIncrementalSyncObjects($type, $objectIds, $langIso);
}
}
$remainingObjects = $this->incrementalSyncRepository->getRemainingIncrementalObjects($type, $langIso);
return array_merge([
'total_objects' => count($data),
'has_remaining_objects' => $remainingObjects > 0,
'remaining_objects' => $remainingObjects,
'md5' => $this->getPayloadMd5($data),
], $response);
}
/**
* @param array $payload
*
* @return string
*/
private function getPayloadMd5($payload)
{
return md5(
implode(' ', array_map(function ($payloadItem) {
return $payloadItem['id'];
}, $payload))
);
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;