Skip to content

Commit

Permalink
feat: sync translations
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john committed Nov 22, 2023
1 parent 68f2772 commit ed9c9d8
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 0 deletions.
25 changes: 25 additions & 0 deletions controllers/front/apiTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use PrestaShop\Module\PsEventbus\Config\Config;
use PrestaShop\Module\PsEventbus\Controller\AbstractApiController;
use PrestaShop\Module\PsEventbus\Provider\TranslationDataProvider;

class ps_EventbusApiTranslationsModuleFrontController extends AbstractApiController
{
public $type = Config::COLLECTION_TRANSLATIONS;

/**
* @return void
*
* @throws PrestaShopException
*/
public function postProcess()
{
/** @var TranslationDataProvider $translationDataProvider */
$translationDataProvider = $this->module->getService(TranslationDataProvider::class);

$response = $this->handleDataSync($translationDataProvider);

$this->exitWithResponse($response);
}
}
1 change: 1 addition & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Config
public const COLLECTION_SUPPLIERS = 'suppliers';
public const COLLECTION_PRODUCT_SUPPLIERS = 'product_suppliers';
public const COLLECTION_LANGUAGES = 'languages';
public const COLLECTION_TRANSLATIONS = 'translations';

/**
* @param mixed $message
Expand Down
54 changes: 54 additions & 0 deletions src/Decorator/TranslationDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace PrestaShop\Module\PsEventbus\Decorator;

use PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository;
use PrestaShop\Module\PsEventbus\Repository\ShopRepository;

class TranslationDecorator
{
/**
* @var string
*/
private $timezone;

Check failure on line 13 in src/Decorator/TranslationDecorator.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8.7)

Property PrestaShop\Module\PsEventbus\Decorator\TranslationDecorator::$timezone is never read, only written.

/**
* @var string
*/
private $createdAt;

Check failure on line 18 in src/Decorator/TranslationDecorator.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8.7)

Property PrestaShop\Module\PsEventbus\Decorator\TranslationDecorator::$createdAt is never read, only written.

public function __construct(
ConfigurationRepository $configurationRepository,
ShopRepository $shopRepository
) {
$this->timezone = (string) $configurationRepository->get('PS_TIMEZONE');
$this->createdAt = $shopRepository->getCreatedAt();
}

/**
* @param array $translations
*
* @return void
*/
public function decorateTranslations(array &$translations)
{
foreach ($translations as &$translation) {
$this->castPropertyValues($translation);
}
}

/**
* @param array $translation
*
* @return void
*/
private function castPropertyValues(array &$translation)
{
$translation['id_translation'] = (int) $translation['id_translation'];
$translation['id_lang'] = (int) $translation['id_lang'];
$translation['key'] = (string) $translation['key'];
$translation['translation'] = (string) $translation['translation'];
$translation['domain'] = (string) $translation['domain'];
$translation['theme'] = (string) $translation['theme'];
}
}
91 changes: 91 additions & 0 deletions src/Provider/TranslationDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace PrestaShop\Module\PsEventbus\Provider;

use PrestaShop\Module\PsEventbus\Config\Config;
use PrestaShop\Module\PsEventbus\Decorator\TranslationDecorator;
use PrestaShop\Module\PsEventbus\Repository\TranslationRepository;

class TranslationDataProvider implements PaginatedApiDataProviderInterface
{
/**
* @var TranslationRepository
*/
private $translationRepository;

/**
* @var TranslationDecorator
*/
private $translationDecorator;

public function __construct(TranslationRepository $translationRepository, TranslationDecorator $translationDecorator)
{
$this->translationRepository = $translationRepository;
$this->translationDecorator = $translationDecorator;
}

/**
* @param int $offset
* @param int $limit
* @param string $langIso
*
* @return array
*
* @throws \PrestaShopDatabaseException
*/
public function getFormattedData($offset, $limit, $langIso)
{
$translations = $this->translationRepository->getTranslationsSync($offset, $limit);

Check failure on line 38 in src/Provider/TranslationDataProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8.7)

Call to an undefined method PrestaShop\Module\PsEventbus\Repository\TranslationRepository::getTranslationsSync().

if (!is_array($translations)) {
return [];
}
$this->translationDecorator->decorateTranslations($translations);

return array_map(function ($translation) {
return [
'id' => $translation['id_lang'],
'collection' => Config::COLLECTION_TRANSLATIONS,
'properties' => $translation,
];
}, $translations);
}

/**
* @param int $offset
* @param string $langIso
*
* @return int
*/
public function getRemainingObjectsCount($offset, $langIso)
{
return (int) $this->translationRepository->getRemainingTranslationsCount($offset);
}

/**
* @param int $limit
* @param string $langIso
* @param array $objectIds
*
* @return array
*
* @throws \PrestaShopDatabaseException
*/
public function getFormattedDataIncremental($limit, $langIso, $objectIds)
{
$translations = $this->translationRepository->getTranslationsIncremental($limit, $objectIds);

if (!is_array($translations)) {
return [];
}
$this->translationDecorator->decorateTranslations($translations);

return array_map(function ($translation) {
return [
'id' => $translation['id_lang'],
'collection' => Config::COLLECTION_TRANSLATIONS,
'properties' => $translation,
];
}, $translations);
}
}
104 changes: 104 additions & 0 deletions src/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace PrestaShop\Module\PsEventbus\Repository;

class TranslationRepository
{
/**
* @var \Db
*/
private $db;

/**
* @var \Context
*/
private $context;

public function __construct(\Db $db, \Context $context)
{
$this->db = $db;
$this->context = $context;
}

/**
* @param int $shopId
*
* @return \DbQuery
*/
public function getBaseQuery($shopId)
{
$query = new \DbQuery();
$query->from('translation', 'c')
->where('c.id_shop = ' . (int) $shopId);

return $query;
}

/**
* @param int $offset
* @param int $limit
*
* @return array|bool|\mysqli_result|\PDOStatement|resource|null
*
* @throws \PrestaShopDatabaseException
*/
public function getTranslations($offset, $limit)
{
/** @var int $shopId */
$shopId = $this->context->shop->id;
$query = $this->getBaseQuery($shopId);

$this->addSelectParameters($query);

$query->limit($limit, $offset);

return $this->db->executeS($query);
}

/**
* @param int $offset
*
* @return int
*/
public function getRemainingTranslationsCount($offset)
{
/** @var int $shopId */
$shopId = $this->context->shop->id;
$query = $this->getBaseQuery($shopId)
->select('(COUNT(c.id_translation) - ' . (int) $offset . ') as count');

return (int) $this->db->getValue($query);
}

/**
* @param int $limit
* @param array $translationIds
*
* @return array|bool|\mysqli_result|\PDOStatement|resource|null
*
* @throws \PrestaShopDatabaseException
*/
public function getTranslationsIncremental($limit, $translationIds)
{
/** @var int $shopId */
$shopId = $this->context->shop->id;
$query = $this->getBaseQuery($shopId);

$this->addSelectParameters($query);

$query->where('c.id_translation IN(' . implode(',', array_map('intval', $translationIds)) . ')')
->limit($limit);

return $this->db->executeS($query);
}

/**
* @param \DbQuery $query
*
* @return void
*/
private function addSelectParameters(\DbQuery $query)
{
$query->select('c.id_translation, c.id_lang, c.key, c.translation, c.domain, c.theme');
}
}

0 comments on commit ed9c9d8

Please sign in to comment.