diff --git a/config/common/new-repository.yml b/config/common/new-repository.yml index 57f80799..a6ea9bbe 100644 --- a/config/common/new-repository.yml +++ b/config/common/new-repository.yml @@ -54,3 +54,7 @@ services: PrestaShop\Module\PsEventbus\Repository\NewRepository\CustomerRepository: class: PrestaShop\Module\PsEventbus\Repository\NewRepository\CustomerRepository public: true + + PrestaShop\Module\PsEventbus\Repository\NewRepository\CurrencyRepository: + class: PrestaShop\Module\PsEventbus\Repository\NewRepository\CurrencyRepository + public: true diff --git a/config/common/repository.yml b/config/common/repository.yml index f986d80f..7eaa0b12 100644 --- a/config/common/repository.yml +++ b/config/common/repository.yml @@ -15,10 +15,6 @@ services: arguments: - '@=service("prestashop.adapter.legacy.context").getContext()' - PrestaShop\Module\PsEventbus\Repository\CurrencyRepository: - class: PrestaShop\Module\PsEventbus\Repository\CurrencyRepository - public: true - PrestaShop\Module\PsEventbus\Repository\ModuleRepository: class: PrestaShop\Module\PsEventbus\Repository\ModuleRepository public: true @@ -29,10 +25,10 @@ services: arguments: - '@=service("prestashop.adapter.legacy.context").getContext()' - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' - - '@PrestaShop\Module\PsEventbus\Repository\CurrencyRepository' - '@PrestaShop\Module\PsEventbus\Repository\LanguageRepository' - '@PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository' - '@PrestaShop\Module\PsEventbus\Repository\ShopRepository' + - '@PrestaShop\Module\PsEventbus\Service\ShopContent\CurrenciesService' - '@PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandlerInterface' - '%ps_eventbus.sync_api_url%' - '%ps_eventbus.live_sync_api_url%' diff --git a/config/front/services.yml b/config/front/services.yml index 6d8609cc..2caf84ae 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -153,3 +153,9 @@ services: public: true arguments: - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\CustomerRepository' + + PrestaShop\Module\PsEventbus\Service\ShopContent\CurrenciesService: + class: PrestaShop\Module\PsEventbus\Service\ShopContent\CurrenciesService + public: true + arguments: + - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\CurrencyRepository' diff --git a/e2e/src/helpers/shop-contents.ts b/e2e/src/helpers/shop-contents.ts index 38cf2246..d82316dd 100644 --- a/e2e/src/helpers/shop-contents.ts +++ b/e2e/src/helpers/shop-contents.ts @@ -50,6 +50,7 @@ export const shopContentMapping = { product_carriers: "product-carriers", categories: "categories", customers: "customers", + currencies: "currencies", } as const; type ShopContentMapping = typeof shopContentMapping; diff --git a/src/Decorator/CurrencyDecorator.php b/src/Decorator/CurrencyDecorator.php deleted file mode 100644 index 64060be8..00000000 --- a/src/Decorator/CurrencyDecorator.php +++ /dev/null @@ -1,36 +0,0 @@ - $currencies - * - * @return void - */ - public function decorateCurrencies(&$currencies) - { - foreach ($currencies as &$currency) { - $this->castPropertyValues($currency); - } - } - - /** - * @param array $currency - * - * @return void - */ - private function castPropertyValues(&$currency) - { - $currency['id_currency'] = (int) $currency['id_currency']; - $currency['conversion_rate'] = (float) $currency['conversion_rate']; - $currency['deleted'] = (bool) $currency['deleted']; - $currency['active'] = (bool) $currency['active']; - - // https://github.com/PrestaShop/PrestaShop/commit/37807f66b40b0cebb365ef952e919be15e9d6b2f#diff-3f41d3529ffdbfd1b994927eb91826a32a0560697025a734cf128a2c8e092a45R124 - if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.6.0', '>=')) { - $currency['precision'] = (int) $currency['precision']; - } - } -} diff --git a/src/Repository/CurrencyRepository.php b/src/Repository/CurrencyRepository.php deleted file mode 100644 index 9116055c..00000000 --- a/src/Repository/CurrencyRepository.php +++ /dev/null @@ -1,155 +0,0 @@ -db = \Db::getInstance(); - } - - /** - * @return mixed - */ - private function isLangAvailable() - { - return defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.6', '>='); - } - - /** - * @return array - */ - public function getCurrenciesIsoCodes() - { - $currencies = \Currency::getCurrencies(); - - return array_map(function ($currency) { - return $currency['iso_code']; - }, $currencies); - } - - /** - * @return string - */ - public function getDefaultCurrencyIsoCode() - { - $currency = \Currency::getDefaultCurrency(); - - return $currency instanceof \Currency ? $currency->iso_code : ''; - } - - /** - * @param int $offset - * @param int $limit - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getCurrencies($offset, $limit) - { - $query = $this->getBaseQuery(); - - $this->addSelectParameters($query); - - $query->limit($limit, $offset); - - return $this->db->executeS($query); - } - - /** - * @param int $offset - * - * @return int - */ - public function getRemainingCurrenciesCount($offset) - { - $query = $this->getBaseQuery() - ->select('(COUNT(c.id_currency) - ' . (int) $offset . ') as count'); - - return (int) $this->db->getValue($query); - } - - /** - * @param int $limit - * @param array $currencyIds - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getCurrenciesIncremental($limit, $currencyIds) - { - $query = $this->getBaseQuery(); - - $this->addSelectParameters($query); - - $query->where('c.id_currency IN(' . implode(',', array_map('intval', $currencyIds)) . ')') - ->limit($limit); - - return $this->db->executeS($query); - } - - /** - * @return \DbQuery - */ - public function getBaseQuery() - { - $query = new \DbQuery(); - $query->from('currency', 'c'); - if ($this->isLangAvailable()) { - $query->innerJoin('currency_lang', 'cl', 'cl.id_currency = c.id_currency'); - } - - return $query; - } - - /** - * @param int $offset - * @param int $limit - * - * @return array - * - * @throws \PrestaShopDatabaseException - */ - public function getQueryForDebug($offset, $limit) - { - $query = $this->getBaseQuery(); - - $this->addSelectParameters($query); - - $query->limit($limit, $offset); - - $queryStringified = preg_replace('/\s+/', ' ', $query->build()); - - return array_merge( - (array) $query, - ['queryStringified' => $queryStringified] - ); - } - - /** - * @param \DbQuery $query - * - * @return void - */ - private function addSelectParameters(\DbQuery $query) - { - if ($this->isLangAvailable()) { - $query->select('c.id_currency, cl.name, c.iso_code, c.conversion_rate, c.deleted, c.active'); - } else { - $query->select('c.id_currency, \'\' as name, c.iso_code, c.conversion_rate, c.deleted, c.active'); - } - - // https://github.com/PrestaShop/PrestaShop/commit/37807f66b40b0cebb365ef952e919be15e9d6b2f#diff-3f41d3529ffdbfd1b994927eb91826a32a0560697025a734cf128a2c8e092a45R124 - if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.6.0', '>=')) { - $query->select('c.precision'); - } - } -} diff --git a/src/Repository/NewRepository/CurrencyRepository.php b/src/Repository/NewRepository/CurrencyRepository.php new file mode 100644 index 00000000..d20e49d5 --- /dev/null +++ b/src/Repository/NewRepository/CurrencyRepository.php @@ -0,0 +1,119 @@ +generateMinimalQuery(self::TABLE_NAME, 'c'); + + if ($this->isCurrencyLangAvailable()) { + $this->query->innerJoin('currency_lang', 'cl', 'cl.id_currency = c.id_currency'); + } + + if ($withSelecParameters) { + + $this->query + ->select('c.id_currency') + ->select('c.iso_code') + ->select('c.conversion_rate') + ->select('c.deleted') + ->select('c.active') + ; + + if ($this->isCurrencyLangAvailable()) { + $this->query->select('cl.name'); + } else { + $this->query->select('\'\' as name'); + } + + // https://github.com/PrestaShop/PrestaShop/commit/37807f66b40b0cebb365ef952e919be15e9d6b2f#diff-3f41d3529ffdbfd1b994927eb91826a32a0560697025a734cf128a2c8e092a45R124 + if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.6.0', '>=')) { + $this->query->select('c.precision'); + } + } + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * @param bool $debug + * + * @return array + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function retrieveContentsForFull($offset, $limit, $langIso, $debug) + { + $this->generateFullQuery($langIso, true); + + $this->query->limit((int) $limit, (int) $offset); + + return $this->runQuery($debug); + } + + /** + * @param int $limit + * @param array $contentIds + * @param string $langIso + * @param bool $debug + * + * @return array + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function retrieveContentsForIncremental($limit, $contentIds, $langIso, $debug) + { + $this->generateFullQuery($langIso, true); + + $this->query + ->where('c.id_currency IN(' . implode(',', array_map('intval', $contentIds)) . ')') + ->limit($limit) + ; + + return $this->runQuery($debug); + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * + * @return int + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function countFullSyncContentLeft($offset, $limit, $langIso) + { + $this->generateFullQuery($langIso, false); + + $this->query->select('(COUNT(*) - ' . (int) $offset . ') as count'); + + $result = $this->runQuery(false); + + return $result[0]['count']; + } + + /** + * @return mixed + */ + private function isCurrencyLangAvailable() + { + return defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.6', '>='); + } +} diff --git a/src/Repository/ServerInformationRepository.php b/src/Repository/ServerInformationRepository.php index 02bd7e17..75403f0b 100644 --- a/src/Repository/ServerInformationRepository.php +++ b/src/Repository/ServerInformationRepository.php @@ -6,13 +6,14 @@ use PrestaShop\Module\PsEventbus\Config\Config; use PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandlerInterface; use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CurrenciesService; class ServerInformationRepository { /** - * @var CurrencyRepository + * @var CurrenciesService */ - private $currencyRepository; + private $currenciesService; /** * @var LanguageRepository */ @@ -49,10 +50,10 @@ class ServerInformationRepository /** * @param \Context $context * @param PsAccountsAdapterService $psAccountsAdapterService - * @param CurrencyRepository $currencyRepository * @param LanguageRepository $languageRepository * @param ConfigurationRepository $configurationRepository * @param ShopRepository $shopRepository + * @param CurrenciesService $currenciesService * @param ErrorHandlerInterface $errorHandler * @param string $eventbusSyncApiUrl * @param string $eventbusLiveSyncApiUrl @@ -63,16 +64,16 @@ class ServerInformationRepository public function __construct( \Context $context, PsAccountsAdapterService $psAccountsAdapterService, - CurrencyRepository $currencyRepository, LanguageRepository $languageRepository, ConfigurationRepository $configurationRepository, ShopRepository $shopRepository, + CurrenciesService $currenciesService, ErrorHandlerInterface $errorHandler, $eventbusSyncApiUrl, $eventbusLiveSyncApiUrl, $eventbusProxyApiUrl ) { - $this->currencyRepository = $currencyRepository; + $this->currenciesService = $currenciesService; $this->languageRepository = $languageRepository; $this->configurationRepository = $configurationRepository; $this->shopRepository = $shopRepository; @@ -123,8 +124,8 @@ public function getServerInformation($langIso = '') 'cart_is_persistent' => $this->configurationRepository->get('PS_CART_FOLLOWING') == '1', 'default_language' => $this->languageRepository->getDefaultLanguageIsoCode(), 'languages' => implode(';', $this->languageRepository->getLanguagesIsoCodes()), - 'default_currency' => $this->currencyRepository->getDefaultCurrencyIsoCode(), - 'currencies' => implode(';', $this->currencyRepository->getCurrenciesIsoCodes()), + 'default_currency' => $this->currenciesService->getDefaultCurrencyIsoCode(), + 'currencies' => implode(';', $this->currenciesService->getCurrenciesIsoCodes()), 'weight_unit' => $this->configurationRepository->get('PS_WEIGHT_UNIT'), 'distance_unit' => $this->configurationRepository->get('PS_BASE_DISTANCE_UNIT'), 'volume_unit' => $this->configurationRepository->get('PS_VOLUME_UNIT'), diff --git a/src/Service/ShopContent/CurrenciesService.php b/src/Service/ShopContent/CurrenciesService.php new file mode 100644 index 00000000..03233787 --- /dev/null +++ b/src/Service/ShopContent/CurrenciesService.php @@ -0,0 +1,125 @@ +currencyRepository = $currencyRepository; + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForFull($offset, $limit, $langIso, $debug) + { + $result = $this->currencyRepository->retrieveContentsForFull($offset, $limit, $langIso, $debug); + + if (empty($result)) { + return []; + } + + $this->castCurrencies($result); + + return array_map(function ($item) { + return [ + 'id' => $item['id_currency'], + 'collection' => Config::COLLECTION_CURRENCIES, + 'properties' => $item, + ]; + }, $result); + } + + /** + * @param int $limit + * @param array $contentIds + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForIncremental($limit, $contentIds, $langIso, $debug) + { + $result = $this->currencyRepository->retrieveContentsForIncremental($limit, $contentIds, $langIso, $debug); + + if (empty($result)) { + return []; + } + + $this->castCurrencies($result); + + return array_map(function ($item) { + return [ + 'id' => $item['id_currency'], + 'collection' => Config::COLLECTION_CURRENCIES, + 'properties' => $item, + ]; + }, $result); + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * + * @return int + */ + public function getFullSyncContentLeft($offset, $limit, $langIso) + { + return $this->currencyRepository->countFullSyncContentLeft($offset, $limit, $langIso); + } + + /** + * @param array $currencies + * + * @return void + */ + private function castCurrencies(&$currencies) + { + foreach ($currencies as &$currency) { + $currency['id_currency'] = (int) $currency['id_currency']; + $currency['conversion_rate'] = (float) $currency['conversion_rate']; + $currency['deleted'] = (bool) $currency['deleted']; + $currency['active'] = (bool) $currency['active']; + + // https://github.com/PrestaShop/PrestaShop/commit/37807f66b40b0cebb365ef952e919be15e9d6b2f#diff-3f41d3529ffdbfd1b994927eb91826a32a0560697025a734cf128a2c8e092a45R124 + if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.6.0', '>=')) { + $currency['precision'] = (int) $currency['precision']; + } + } + } + + /** + * @return array + */ + public function getCurrenciesIsoCodes() + { + $currencies = \Currency::getCurrencies(); + + return array_map(function ($currency) { + return $currency['iso_code']; + }, $currencies); + } + + /** + * @return string + */ + public function getDefaultCurrencyIsoCode() + { + $currency = \Currency::getDefaultCurrency(); + + return $currency instanceof \Currency ? $currency->iso_code : ''; + } +}