From 2b47db39ce71676e96c01d5a69b8c7f9b0cfff57 Mon Sep 17 00:00:00 2001 From: Jonathan Renard <1273438+fox-john@users.noreply.github.com> Date: Mon, 9 Sep 2024 15:33:26 +0200 Subject: [PATCH] feat: pseventbus v4 carriers (#353) --- OLD/Provider/CarrierDataProvider.php | 137 ---------- config/common/builder.yml | 2 +- config/common/new-repository.yml | 4 + config/common/repository.yml | 6 - config/front/services.yml | 8 + e2e/src/full-sync.spec.ts | 5 +- e2e/src/helpers/shop-contents.ts | 6 +- e2e/src/reject-invalid-job-id.spec.ts | 5 +- src/Builder/CarrierBuilder.php | 152 ++++++++--- src/Config/Config.php | 2 + src/DTO/Carrier.php | 81 ++---- src/Repository/CarrierRepository.php | 235 ------------------ .../NewRepository/CarrierRepository.php | 103 ++++++++ src/Service/ShopContent/CarriersService.php | 113 +++++++++ src/Service/SynchronizationService.php | 6 +- src/Traits/UseHooks.php | 9 + 16 files changed, 389 insertions(+), 485 deletions(-) delete mode 100644 OLD/Provider/CarrierDataProvider.php delete mode 100644 src/Repository/CarrierRepository.php create mode 100644 src/Repository/NewRepository/CarrierRepository.php create mode 100644 src/Service/ShopContent/CarriersService.php diff --git a/OLD/Provider/CarrierDataProvider.php b/OLD/Provider/CarrierDataProvider.php deleted file mode 100644 index 1a4a709f..00000000 --- a/OLD/Provider/CarrierDataProvider.php +++ /dev/null @@ -1,137 +0,0 @@ -configurationRepository = $configurationRepository; - $this->carrierBuilder = $carrierBuilder; - $this->carrierRepository = $carrierRepository; - $this->languageRepository = $languageRepository; - } - - /** - * @param int $offset - * @param int $limit - * @param string $langIso - * - * @return array - * - * @@throws \PrestaShopDatabaseException - */ - public function getFormattedData($offset, $limit, $langIso) - { - $currency = new \Currency((int) $this->configurationRepository->get('PS_CURRENCY_DEFAULT')); - - $langId = $this->languageRepository->getLanguageIdByIsoCode($langIso); - /** @var array $carriers */ - $carriers = $this->carrierRepository->getAllCarrierProperties($offset, $limit, $langId); - - /** @var string $configurationPsWeightUnit */ - $configurationPsWeightUnit = $this->configurationRepository->get('PS_WEIGHT_UNIT'); - /** @var EventBusCarrier[] $eventBusCarriers */ - $eventBusCarriers = $this->carrierBuilder->buildCarriers( - $carriers, - $langId, - $currency, - $configurationPsWeightUnit - ); - - return $eventBusCarriers; - } - - public function getFormattedDataIncremental($limit, $langIso, $objectIds) - { - /** @var array $shippingIncremental */ - $shippingIncremental = $this->carrierRepository->getShippingIncremental(Config::COLLECTION_CARRIERS, $langIso); - - if (!$shippingIncremental) { - return []; - } - - $currency = new \Currency((int) $this->configurationRepository->get('PS_CURRENCY_DEFAULT')); - - $langId = $this->languageRepository->getLanguageIdByIsoCode($langIso); - - $carrierIds = array_column($shippingIncremental, 'id_object'); - /** @var array $carriers */ - $carriers = $this->carrierRepository->getCarrierProperties($carrierIds, $langId); - - /** @var string $configurationPsWeightUnit */ - $configurationPsWeightUnit = $this->configurationRepository->get('PS_WEIGHT_UNIT'); - /** @var EventBusCarrier[] $eventBusCarriers */ - $eventBusCarriers = $this->carrierBuilder->buildCarriers( - $carriers, - $langId, - $currency, - $configurationPsWeightUnit - ); - - return $eventBusCarriers; - } - - /** - * @param int $offset - * @param string $langIso - * - * @return int - * - * @@throws \PrestaShopDatabaseException - */ - public function getRemainingObjectsCount($offset, $langIso) - { - $langId = $this->languageRepository->getLanguageIdByIsoCode($langIso); - - return (int) $this->carrierRepository->getRemainingCarriersCount($offset, $langId); - } - - /** - * @param int $offset - * @param int $limit - * @param string $langIso - * - * @return array - * - * @@throws \PrestaShopDatabaseException - */ - public function getQueryForDebug($offset, $limit, $langIso) - { - $langId = $this->languageRepository->getLanguageIdByIsoCode($langIso); - - return $this->carrierRepository->getQueryForDebug($offset, $limit, $langId); - } -} diff --git a/config/common/builder.yml b/config/common/builder.yml index 1caea5db..7e4e036c 100644 --- a/config/common/builder.yml +++ b/config/common/builder.yml @@ -3,8 +3,8 @@ services: class: PrestaShop\Module\PsEventbus\Builder\CarrierBuilder public: true arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CarrierRepository' - '@PrestaShop\Module\PsEventbus\Repository\CountryRepository' - '@PrestaShop\Module\PsEventbus\Repository\StateRepository' - '@PrestaShop\Module\PsEventbus\Repository\TaxRepository' - '@PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository' + - '@PrestaShop\Module\PsEventbus\Repository\LanguageRepository' diff --git a/config/common/new-repository.yml b/config/common/new-repository.yml index 8d64c837..fc336efc 100644 --- a/config/common/new-repository.yml +++ b/config/common/new-repository.yml @@ -14,3 +14,7 @@ services: PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderDetailRepository: class: PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderDetailRepository public: true + + PrestaShop\Module\PsEventbus\Repository\NewRepository\CarrierRepository: + class: PrestaShop\Module\PsEventbus\Repository\NewRepository\CarrierRepository + public: true diff --git a/config/common/repository.yml b/config/common/repository.yml index 59fe9882..471dd2c5 100644 --- a/config/common/repository.yml +++ b/config/common/repository.yml @@ -127,12 +127,6 @@ services: arguments: - '@=service("prestashop.adapter.legacy.context").getContext()' - PrestaShop\Module\PsEventbus\Repository\CarrierRepository: - class: PrestaShop\Module\PsEventbus\Repository\CarrierRepository - public: true - arguments: - - '@=service("prestashop.adapter.legacy.context").getContext()' - PrestaShop\Module\PsEventbus\Repository\CustomPriceRepository: class: PrestaShop\Module\PsEventbus\Repository\CustomPriceRepository public: true diff --git a/config/front/services.yml b/config/front/services.yml index 57c26aba..4f02827a 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -78,3 +78,11 @@ services: public: true arguments: - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderDetailRepository' + + PrestaShop\Module\PsEventbus\Service\ShopContent\CarriersService: + class: PrestaShop\Module\PsEventbus\Service\ShopContent\CarriersService + public: true + arguments: + - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\CarrierRepository' + - '@PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository' + - '@PrestaShop\Module\PsEventbus\Builder\CarrierBuilder' diff --git a/e2e/src/full-sync.spec.ts b/e2e/src/full-sync.spec.ts index 4ec84c0b..6a3f4e23 100644 --- a/e2e/src/full-sync.spec.ts +++ b/e2e/src/full-sync.spec.ts @@ -50,7 +50,7 @@ const specialFieldAssert: { [index: string]: (val) => void } = { } describe('Full Sync', () => { - let testIndex = 0; + let testTimestamp = 0; // gérer les cas ou un shopContent n'existe pas (pas de fixture du coup) const shopContents: ShopContent[] = shopContentList.filter( @@ -60,7 +60,8 @@ describe('Full Sync', () => { let jobId: string; beforeEach(() => { - jobId = `valid-job-full-${testIndex++}`; + testTimestamp = Date.now(); + jobId = `valid-job-full-${testTimestamp}`; }); // TODO : some versions of prestashop include ps_facebook out of the box, this test can't reliably be run for all versions diff --git a/e2e/src/helpers/shop-contents.ts b/e2e/src/helpers/shop-contents.ts index bf8a9bfe..7ed2ce5c 100644 --- a/e2e/src/helpers/shop-contents.ts +++ b/e2e/src/helpers/shop-contents.ts @@ -3,7 +3,6 @@ import R from "ramda"; // TEMPORARY DISABLED, WAIT ADD ALL SHOP CONTENT // AFTER UNCOMMENT THIS, CHANGE ALL "as ShopContent"CAST IN FULLSYNC TEST /* export const shopContentMapping = { - 'carriers': 'carriers', 'carrier_details': 'carrier-details', 'carts' : 'carts', 'cart_products': 'carts', @@ -35,10 +34,11 @@ import R from "ramda"; } as const; */ export const shopContentMapping = { + 'carriers': 'carriers', 'orders': 'orders', - 'order_histories': 'order-histories', 'order_cart_rules': 'order-cart-rules', - 'order_details': 'order-details' + 'order_details': 'order-details', + 'order_histories': 'order-histories' } as const; type ShopContentMapping = typeof shopContentMapping; diff --git a/e2e/src/reject-invalid-job-id.spec.ts b/e2e/src/reject-invalid-job-id.spec.ts index 807e133f..a0aee8f4 100644 --- a/e2e/src/reject-invalid-job-id.spec.ts +++ b/e2e/src/reject-invalid-job-id.spec.ts @@ -6,14 +6,15 @@ import {probe} from "./helpers/mock-probe"; import { ShopContent, shopContentList } from "./helpers/shop-contents"; describe('Reject invalid job-id', () => { - let testIndex = 0; + let testTimestamp = 0; const shopContents: ShopContent[] = shopContentList let jobId: string; beforeEach(() => { - jobId = `invalid-job-id-${testIndex++}` + testTimestamp = Date.now(); + jobId = `invalid-job-id-${testTimestamp}` }); it.each(shopContents)(`%s should return 454 with an invalid job id (sync-api status 454)`, async (shopContent) => { diff --git a/src/Builder/CarrierBuilder.php b/src/Builder/CarrierBuilder.php index aeb65fed..63ba7a62 100644 --- a/src/Builder/CarrierBuilder.php +++ b/src/Builder/CarrierBuilder.php @@ -5,66 +5,60 @@ use PrestaShop\Module\PsEventbus\DTO\Carrier as EventBusCarrier; use PrestaShop\Module\PsEventbus\DTO\CarrierDetail; use PrestaShop\Module\PsEventbus\DTO\CarrierTax; -use PrestaShop\Module\PsEventbus\Repository\CarrierRepository; use PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository; use PrestaShop\Module\PsEventbus\Repository\CountryRepository; +use PrestaShop\Module\PsEventbus\Repository\LanguageRepository; use PrestaShop\Module\PsEventbus\Repository\StateRepository; use PrestaShop\Module\PsEventbus\Repository\TaxRepository; +use PrestaShopDatabaseException; +use PrestaShopException; class CarrierBuilder { - /** - * @var CarrierRepository - */ - private $carrierRepository; - - /** - * @var CountryRepository - */ + /** @var CountryRepository */ private $countryRepository; - /** - * @var StateRepository - */ + /** @var StateRepository */ private $stateRepository; - /** - * @var TaxRepository - */ + /** @var TaxRepository */ private $taxRepository; - /** - * @var ConfigurationRepository - */ + /** @var ConfigurationRepository */ private $configurationRepository; + /** @var LanguageRepository */ + private $languageRepository; + public function __construct( - CarrierRepository $carrierRepository, CountryRepository $countryRepository, StateRepository $stateRepository, TaxRepository $taxRepository, - ConfigurationRepository $configurationRepository + ConfigurationRepository $configurationRepository, + LanguageRepository $languageRepository ) { - $this->carrierRepository = $carrierRepository; $this->countryRepository = $countryRepository; $this->stateRepository = $stateRepository; $this->taxRepository = $taxRepository; $this->configurationRepository = $configurationRepository; + $this->languageRepository = $languageRepository; } /** * @param array $carriers - * @param int $langId + * @param string $langIso * @param \Currency $currency * @param string $weightUnit * * @return array * - * @@throws \PrestaShopDatabaseException - * @@throws \PrestaShopException + * @@throws PrestaShopDatabaseException + * @@throws PrestaShopException */ - public function buildCarriers($carriers, $langId, \Currency $currency, $weightUnit) + public function buildCarriers($carriers, $langIso, \Currency $currency, $weightUnit) { + $langId = $this->languageRepository->getLanguageIdByIsoCode($langIso); + $eventBusCarriers = []; foreach ($carriers as $carrier) { $eventBusCarriers[] = $this->buildCarrier( @@ -74,15 +68,9 @@ public function buildCarriers($carriers, $langId, \Currency $currency, $weightUn ); } - $formattedCarriers = []; - /** @var EventBusCarrier $eventBusCarrier */ - foreach ($eventBusCarriers as $eventBusCarrier) { - /** @var array $eventBusCarrierSerialized */ - $eventBusCarrierSerialized = $eventBusCarrier->jsonSerialize(); - $formattedCarriers = array_merge($formattedCarriers, $eventBusCarrierSerialized); - } - - return $formattedCarriers; + return array_map(function ($eventBusCarrier) { + return $eventBusCarrier->jsonSerialize(); + }, $eventBusCarriers); } /** @@ -92,8 +80,8 @@ public function buildCarriers($carriers, $langId, \Currency $currency, $weightUn * * @return EventBusCarrier * - * @@throws \PrestaShopDatabaseException - * @@throws \PrestaShopException + * @@throws PrestaShopDatabaseException + * @@throws PrestaShopException */ public function buildCarrier(\Carrier $carrier, $currencyIsoCode, $weightUnit) { @@ -128,7 +116,7 @@ public function buildCarrier(\Carrier $carrier, $currencyIsoCode, $weightUnit) ->setCurrency($currencyIsoCode) ->setWeightUnit($weightUnit); - $deliveryPriceByRanges = $this->carrierRepository->getDeliveryPriceByRange($carrier); + $deliveryPriceByRanges = $this->getDeliveryPriceByRange($carrier); if (!$deliveryPriceByRanges) { return $eventBusCarrier; @@ -137,7 +125,7 @@ public function buildCarrier(\Carrier $carrier, $currencyIsoCode, $weightUnit) $carrierDetails = []; $carrierTaxes = []; foreach ($deliveryPriceByRanges as $deliveryPriceByRange) { - $range = $this->carrierRepository->getCarrierRange($deliveryPriceByRange); + $range = $this->getCarrierRange($deliveryPriceByRange); if (!$range) { continue; } @@ -162,6 +150,90 @@ public function buildCarrier(\Carrier $carrier, $currencyIsoCode, $weightUnit) return $eventBusCarrier; } + /** + * @param \Carrier $carrierObj + * + * @return array|false + */ + public function getDeliveryPriceByRange(\Carrier $carrierObj) + { + $rangeTable = $carrierObj->getRangeTable(); + switch ($rangeTable) { + case 'range_weight': + return $this->getCarrierByWeightRange($carrierObj, 'range_weight'); + case 'range_price': + return $this->getCarrierByPriceRange($carrierObj, 'range_price'); + default: + return false; + } + } + + /** + * @param \Carrier $carrierObj + * @param string $rangeTable + * + * @return array + */ + private function getCarrierByPriceRange( + \Carrier $carrierObj, + $rangeTable + ) { + $deliveryPriceByRange = \Carrier::getDeliveryPriceByRanges($rangeTable, (int) $carrierObj->id); + + $filteredRanges = []; + foreach ($deliveryPriceByRange as $range) { + $filteredRanges[$range['id_range_price']]['id_range_price'] = $range['id_range_price']; + $filteredRanges[$range['id_range_price']]['id_carrier'] = $range['id_carrier']; + $filteredRanges[$range['id_range_price']]['zones'][$range['id_zone']]['id_zone'] = $range['id_zone']; + $filteredRanges[$range['id_range_price']]['zones'][$range['id_zone']]['price'] = $range['price']; + } + + return $filteredRanges; + } + + /** + * @param \Carrier $carrierObj + * @param string $rangeTable + * + * @return array + */ + private function getCarrierByWeightRange( + \Carrier $carrierObj, + $rangeTable + ) { + $deliveryPriceByRange = \Carrier::getDeliveryPriceByRanges($rangeTable, (int) $carrierObj->id); + + $filteredRanges = []; + foreach ($deliveryPriceByRange as $range) { + $filteredRanges[$range['id_range_weight']]['id_range_weight'] = $range['id_range_weight']; + $filteredRanges[$range['id_range_weight']]['id_carrier'] = $range['id_carrier']; + $filteredRanges[$range['id_range_weight']]['zones'][$range['id_zone']]['id_zone'] = $range['id_zone']; + $filteredRanges[$range['id_range_weight']]['zones'][$range['id_zone']]['price'] = $range['price']; + } + + return $filteredRanges; + } + + /** + * @param array $deliveryPriceByRange + * + * @return false|\RangeWeight|\RangePrice + * + * @throws \PrestaShopDatabaseException + * @throws \PrestaShopException + */ + public function getCarrierRange($deliveryPriceByRange) + { + if (isset($deliveryPriceByRange['id_range_weight'])) { + return new \RangeWeight($deliveryPriceByRange['id_range_weight']); + } + if (isset($deliveryPriceByRange['id_range_price'])) { + return new \RangePrice($deliveryPriceByRange['id_range_price']); + } + + return false; + } + /** * @param \Carrier $carrier * @param \RangeWeight|\RangePrice $range @@ -169,7 +241,7 @@ public function buildCarrier(\Carrier $carrier, $currencyIsoCode, $weightUnit) * * @return false|CarrierDetail * - * @@throws \PrestaShopDatabaseException + * @@throws PrestaShopDatabaseException */ private function buildCarrierDetails(\Carrier $carrier, $range, $zone) { @@ -206,7 +278,7 @@ private function buildCarrierDetails(\Carrier $carrier, $range, $zone) * * @return CarrierTax|null * - * @@throws \PrestaShopDatabaseException + * @@throws PrestaShopDatabaseException */ private function buildCarrierTaxes(\Carrier $carrier, $zoneId, $rangeId) { diff --git a/src/Config/Config.php b/src/Config/Config.php index 39ad3e95..8ddb35a8 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -34,6 +34,7 @@ class Config const COLLECTION_CARRIERS = 'carriers'; const COLLECTION_CARRIER_DETAILS = 'carrier_details'; + const COLLECTION_CARRIER_TAXES = 'carrier_taxes'; const COLLECTION_CARTS = 'carts'; const COLLECTION_CART_PRODUCTS = 'cart_products'; const COLLECTION_CART_RULES = 'cart_rules'; @@ -73,6 +74,7 @@ class Config const SHOP_CONTENTS = [ self::COLLECTION_CARRIERS, self::COLLECTION_CARRIER_DETAILS, + self::COLLECTION_CARRIER_TAXES, self::COLLECTION_CARTS, self::COLLECTION_CART_PRODUCTS, self::COLLECTION_CART_RULES, diff --git a/src/DTO/Carrier.php b/src/DTO/Carrier.php index 7f8e5976..8d86b337 100644 --- a/src/DTO/Carrier.php +++ b/src/DTO/Carrier.php @@ -4,11 +4,6 @@ class Carrier implements \JsonSerializable { - /** - * @var string - */ - private $collection = 'carriers'; - /** * @var int */ @@ -139,14 +134,6 @@ class Carrier implements \JsonSerializable */ private $carrierTaxes = []; - /** - * @return string - */ - public function getCollection() - { - return $this->collection; - } - /** * @return int */ @@ -683,49 +670,31 @@ public function jsonSerialize() */ error_reporting(E_ALL ^ E_WARNING); - $return = []; - - $return[] = [ - 'collection' => $this->getCollection(), - 'id' => (string) $this->getIdReference(), - 'properties' => [ - 'id_carrier' => (string) $this->getIdCarrier(), - 'id_reference' => (string) $this->getIdReference(), - 'name' => (string) $this->getName(), - 'carrier_taxes_rates_group_id' => (string) $this->getTaxesRatesGroupId(), - 'url' => (string) $this->getUrl(), - 'active' => (bool) $this->isActive(), - 'deleted' => (bool) $this->isDeleted(), - 'shipping_handling' => (float) $this->getShippingHandling(), - 'free_shipping_starts_at_price' => (float) $this->getFreeShippingStartsAtPrice(), - 'free_shipping_starts_at_weight' => (float) $this->getFreeShippingStartsAtWeight(), - 'disable_carrier_when_out_of_range' => (bool) $this->isDisableCarrierWhenOutOfRange(), - 'is_module' => (bool) $this->isModule(), - 'is_free' => (bool) $this->isFree(), - 'shipping_external' => (bool) $this->isShippingExternal(), - 'need_range' => (bool) $this->isNeedRange(), - 'external_module_name' => (string) $this->getExternalModuleName(), - 'max_width' => (float) $this->getMaxWidth(), - 'max_height' => (float) $this->getMaxHeight(), - 'max_depth' => (float) $this->getMaxDepth(), - 'max_weight' => (float) $this->getMaxWeight(), - 'grade' => (int) $this->getGrade(), - 'delay' => (string) $this->getDelay(), - 'currency' => (string) $this->getCurrency(), - 'weight_unit' => (string) $this->getWeightUnit(), - ], + return [ + 'id_carrier' => (string) $this->getIdCarrier(), + 'id_reference' => (string) $this->getIdReference(), + 'name' => (string) $this->getName(), + 'carrier_taxes_rates_group_id' => (string) $this->getTaxesRatesGroupId(), + 'url' => (string) $this->getUrl(), + 'active' => (bool) $this->isActive(), + 'deleted' => (bool) $this->isDeleted(), + 'shipping_handling' => (float) $this->getShippingHandling(), + 'free_shipping_starts_at_price' => (float) $this->getFreeShippingStartsAtPrice(), + 'free_shipping_starts_at_weight' => (float) $this->getFreeShippingStartsAtWeight(), + 'disable_carrier_when_out_of_range' => (bool) $this->isDisableCarrierWhenOutOfRange(), + 'is_module' => (bool) $this->isModule(), + 'is_free' => (bool) $this->isFree(), + 'shipping_external' => (bool) $this->isShippingExternal(), + 'need_range' => (bool) $this->isNeedRange(), + 'external_module_name' => (string) $this->getExternalModuleName(), + 'max_width' => (float) $this->getMaxWidth(), + 'max_height' => (float) $this->getMaxHeight(), + 'max_depth' => (float) $this->getMaxDepth(), + 'max_weight' => (float) $this->getMaxWeight(), + 'grade' => (int) $this->getGrade(), + 'delay' => (string) $this->getDelay(), + 'currency' => (string) $this->getCurrency(), + 'weight_unit' => (string) $this->getWeightUnit(), ]; - - $carrierDetails = []; - foreach ($this->getCarrierDetails() as $carrierDetail) { - $carrierDetails[] = $carrierDetail->jsonSerialize(); - } - - $carrierTaxRates = []; - foreach ($this->getCarrierTaxes() as $carrierTax) { - $carrierTaxRates[] = $carrierTax->jsonSerialize(); - } - - return array_merge($return, $carrierDetails, $carrierTaxRates); } } diff --git a/src/Repository/CarrierRepository.php b/src/Repository/CarrierRepository.php deleted file mode 100644 index f36d8c89..00000000 --- a/src/Repository/CarrierRepository.php +++ /dev/null @@ -1,235 +0,0 @@ -db = \Db::getInstance(); - $this->context = $context; - - if ($this->context->shop === null) { - throw new \PrestaShopException('No shop context'); - } - - $this->shopId = (int) $this->context->shop->id; - } - - /** - * @param \Carrier $carrierObj - * - * @return array|false - */ - public function getDeliveryPriceByRange(\Carrier $carrierObj) - { - $rangeTable = $carrierObj->getRangeTable(); - switch ($rangeTable) { - case 'range_weight': - return $this->getCarrierByWeightRange($carrierObj, 'range_weight'); - case 'range_price': - return $this->getCarrierByPriceRange($carrierObj, 'range_price'); - default: - return false; - } - } - - /** - * @param \Carrier $carrierObj - * @param string $rangeTable - * - * @return array - */ - private function getCarrierByPriceRange( - \Carrier $carrierObj, - $rangeTable - ) { - $deliveryPriceByRange = \Carrier::getDeliveryPriceByRanges($rangeTable, (int) $carrierObj->id); - - $filteredRanges = []; - foreach ($deliveryPriceByRange as $range) { - $filteredRanges[$range['id_range_price']]['id_range_price'] = $range['id_range_price']; - $filteredRanges[$range['id_range_price']]['id_carrier'] = $range['id_carrier']; - $filteredRanges[$range['id_range_price']]['zones'][$range['id_zone']]['id_zone'] = $range['id_zone']; - $filteredRanges[$range['id_range_price']]['zones'][$range['id_zone']]['price'] = $range['price']; - } - - return $filteredRanges; - } - - /** - * @param \Carrier $carrierObj - * @param string $rangeTable - * - * @return array - */ - private function getCarrierByWeightRange( - \Carrier $carrierObj, - $rangeTable - ) { - $deliveryPriceByRange = \Carrier::getDeliveryPriceByRanges($rangeTable, (int) $carrierObj->id); - - $filteredRanges = []; - foreach ($deliveryPriceByRange as $range) { - $filteredRanges[$range['id_range_weight']]['id_range_weight'] = $range['id_range_weight']; - $filteredRanges[$range['id_range_weight']]['id_carrier'] = $range['id_carrier']; - $filteredRanges[$range['id_range_weight']]['zones'][$range['id_zone']]['id_zone'] = $range['id_zone']; - $filteredRanges[$range['id_range_weight']]['zones'][$range['id_zone']]['price'] = $range['price']; - } - - return $filteredRanges; - } - - /** - * @param int $offset - * @param int $limit - * @param int $langId - * - * @return \DbQuery - */ - private function getAllCarriersQuery($offset, $limit, $langId) - { - $query = new \DbQuery(); - $query->from('carrier', 'c'); - $query->select('c.id_carrier'); - $query->leftJoin('carrier_lang', 'cl', 'cl.id_carrier = c.id_carrier AND cl.id_lang = ' . (int) $langId); - $query->leftJoin('carrier_shop', 'cs', 'cs.id_carrier = c.id_carrier'); - $query->where('cs.id_shop = ' . $this->shopId); - $query->where('deleted=0'); - $query->limit($limit, $offset); - - return $query; - } - - /** - * @param string $type - * @param string $langIso - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getShippingIncremental($type, $langIso) - { - $query = new \DbQuery(); - $query->from(IncrementalSyncRepository::INCREMENTAL_SYNC_TABLE, 'aic'); - $query->leftJoin(EventbusSyncRepository::TYPE_SYNC_TABLE_NAME, 'ts', 'ts.type = aic.type'); - $query->where('aic.type = "' . pSQL($type) . '"'); - $query->where('ts.id_shop = ' . $this->shopId); - $query->where('ts.lang_iso = "' . pSQL($langIso) . '"'); - - return $this->db->executeS($query); - } - - /** - * @param array $deliveryPriceByRange - * - * @return false|\RangeWeight|\RangePrice - * - * @throws \PrestaShopDatabaseException - * @throws \PrestaShopException - */ - public function getCarrierRange($deliveryPriceByRange) - { - if (isset($deliveryPriceByRange['id_range_weight'])) { - return new \RangeWeight($deliveryPriceByRange['id_range_weight']); - } - if (isset($deliveryPriceByRange['id_range_price'])) { - return new \RangePrice($deliveryPriceByRange['id_range_price']); - } - - return false; - } - - /** - * @param int[] $carrierIds - * @param int $langId - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getCarrierProperties($carrierIds, $langId) - { - if (!$carrierIds) { - return []; - } - $query = new \DbQuery(); - $query->from('carrier', 'c'); - $query->select('c.*, cl.delay'); - $query->leftJoin('carrier_lang', 'cl', 'cl.id_carrier = c.id_carrier AND cl.id_lang = ' . (int) $langId); - $query->leftJoin('carrier_shop', 'cs', 'cs.id_carrier = c.id_carrier'); - $query->where('c.id_carrier IN (' . implode(',', array_map('intval', $carrierIds)) . ')'); - $query->where('cs.id_shop = ' . $this->shopId); - - return $this->db->executeS($query); - } - - /** - * @param int $offset - * @param int $limit - * @param int $langId - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getAllCarrierProperties($offset, $limit, $langId) - { - return $this->db->executeS($this->getAllCarriersQuery($offset, $limit, $langId)); - } - - /** - * @param int $offset - * @param int $langId - * - * @return int - * - * @throws \PrestaShopDatabaseException - */ - public function getRemainingCarriersCount($offset, $langId) - { - $carriers = $this->getAllCarrierProperties($offset, 1, $langId); - - if (!is_array($carriers) || empty($carriers)) { - return 0; - } - - return count($carriers); - } - - /** - * @param int $offset - * @param int $limit - * @param int $langId - * - * @return array - * - * @throws \PrestaShopDatabaseException - */ - public function getQueryForDebug($offset, $limit, $langId) - { - $query = $this->getAllCarriersQuery($offset, $limit, $langId); - $queryStringified = preg_replace('/\s+/', ' ', $query->build()); - - return array_merge( - (array) $query, - ['queryStringified' => $queryStringified] - ); - } -} diff --git a/src/Repository/NewRepository/CarrierRepository.php b/src/Repository/NewRepository/CarrierRepository.php new file mode 100644 index 00000000..27bd0d59 --- /dev/null +++ b/src/Repository/NewRepository/CarrierRepository.php @@ -0,0 +1,103 @@ +shop === null) { + throw new \PrestaShopException('No shop context'); + } + + $this->query = new \DbQuery(); + + $this->query->from('carrier', 'c') + ->leftJoin('carrier_lang', 'cl', 'cl.id_carrier = c.id_carrier AND cl.id_lang = ' . $langId) + ->leftJoin('carrier_shop', 'cs', 'cs.id_carrier = c.id_carrier') + ->where('cs.id_shop = ' . $context->shop->id) + ->where('deleted=0') + ; + + $this->query->select('c.*'); + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * @param bool $debug + * + * @return array + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function getContentsForFull($offset, $limit, $langIso, $debug) + { + $this->generateBaseQuery($langIso); + + $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 getContentsForIncremental($limit, $contentIds, $langIso, $debug) + { + $this->generateBaseQuery($langIso); + + $this->query + ->where('c.id_carrier IN(' . implode(',', array_map('intval', $contentIds)) . ')') + ->limit($limit); + + return $this->runQuery($debug); + } + + /** + * @param int $offset + * @param string $langIso + * @param bool $debug + * + * @return int + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function countFullSyncContentLeft($offset, $langIso, $debug) + { + $result = $this->getContentsForFull($offset, 1, $langIso, $debug); + + if (!is_array($result) || empty($result)) { + return 0; + } + + return count($result); + } +} diff --git a/src/Service/ShopContent/CarriersService.php b/src/Service/ShopContent/CarriersService.php new file mode 100644 index 00000000..da6a8225 --- /dev/null +++ b/src/Service/ShopContent/CarriersService.php @@ -0,0 +1,113 @@ +carrierRepository = $carrierRepository; + $this->configurationRepository = $configurationRepository; + $this->carrierBuilder = $carrierBuilder; + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForFull($offset, $limit, $langIso, $debug) + { + $currency = new \Currency((int) $this->configurationRepository->get('PS_CURRENCY_DEFAULT')); + + /** @var array $carriers */ + $carriers = $this->carrierRepository->getContentsForFull($offset, $limit, $langIso, $debug); + + /** @var string $psWeightUnit */ + $psWeightUnit = $this->configurationRepository->get('PS_WEIGHT_UNIT'); + + $eventBusCarriers = $this->carrierBuilder->buildCarriers( + $carriers, + $langIso, + $currency, + $psWeightUnit + ); + + return array_map(function ($item) { + return [ + 'id' => $item['id_reference'], + 'collection' => Config::COLLECTION_CARRIERS, + 'properties' => $item, + ]; + }, $eventBusCarriers); + } + + /** + * @param int $limit + * @param array $contentIds + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForIncremental($limit, $contentIds, $langIso, $debug) + { + $result = $this->carrierRepository->getContentsForIncremental($limit, $contentIds, $langIso, $debug); + + if (empty($result)) { + return []; + } + + $currency = new \Currency((int) $this->configurationRepository->get('PS_CURRENCY_DEFAULT')); + + /** @var string $psWeightUnit */ + $psWeightUnit = $this->configurationRepository->get('PS_WEIGHT_UNIT'); + + $eventBusCarriers = $this->carrierBuilder->buildCarriers( + $result, + $langIso, + $currency, + $psWeightUnit + ); + + return array_map(function ($item) { + return [ + 'id' => $item['id_reference'], + 'collection' => Config::COLLECTION_CARRIERS, + 'properties' => $item, + ]; + }, $eventBusCarriers); + } + + /** + * @param int $offset + * @param string $langIso + * @param bool $debug + * + * @return int + */ + public function countFullSyncContentLeft($offset, $langIso, $debug) + { + return (int) $this->carrierRepository->countFullSyncContentLeft($offset, $langIso, $debug); + } +} diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 6024d5d9..a7daf7af 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -72,7 +72,7 @@ public function __construct( * * @return array * - * @@throws \PrestaShopDatabaseException|EnvVarException|ApiException + * @@throws PrestaShopDatabaseException|EnvVarException|ApiException */ public function sendFullSync( string $shopContent, @@ -139,7 +139,7 @@ public function sendFullSync( * * @return array * - * @@throws \PrestaShopDatabaseException|EnvVarException + * @@throws PrestaShopDatabaseException|EnvVarException */ public function sendIncrementalSync( string $shopContent, @@ -301,7 +301,7 @@ public function insertContentIntoIncremental($contentTypesWithIds, $actionType, * * @return bool * - * @@throws \PrestaShopDatabaseException + * @@throws PrestaShopDatabaseException */ private function debounceLiveSync($shopContentName) // @phpstan-ignore method.unused { diff --git a/src/Traits/UseHooks.php b/src/Traits/UseHooks.php index a465a352..db0d0572 100644 --- a/src/Traits/UseHooks.php +++ b/src/Traits/UseHooks.php @@ -1128,6 +1128,9 @@ public function hookActionObjectCarrierAddAfter($parameters) $this->shopId, false ); + + // TODO INSERT INCREMENTAL SYNC AND LIVE SYNC OF CARRIER DETAILS + // TODO INSERT INCREMENTAL SYNC AND LIVE SYNC OF CARRIER TAXES } } @@ -1153,6 +1156,9 @@ public function hookActionObjectCarrierUpdateAfter($parameters) $this->shopId, false ); + + // TODO INSERT INCREMENTAL SYNC AND LIVE SYNC OF CARRIER DETAILS + // TODO INSERT INCREMENTAL SYNC AND LIVE SYNC OF CARRIER TAXES } } @@ -1177,6 +1183,9 @@ public function hookActionObjectCarrierDeleteAfter($parameters) $this->shopId, false ); + + // TODO INSERT INCREMENTAL SYNC AND LIVE SYNC OF CARRIER DETAILS + // TODO INSERT INCREMENTAL SYNC AND LIVE SYNC OF CARRIER TAXES } }