From 226d177babbd8eef5b3881dce501c2841e629382 Mon Sep 17 00:00:00 2001 From: Jonathan Renard <1273438+fox-john@users.noreply.github.com> Date: Wed, 11 Sep 2024 14:41:18 +0200 Subject: [PATCH] feat: pseventbus v4 carts (#355) --- config/common/new-repository.yml | 4 + config/front/services.yml | 6 + e2e/src/helpers/shop-contents.ts | 1 + src/Repository/CartRepository.php | 129 ------------------ .../NewRepository/CarrierRepository.php | 2 +- .../NewRepository/CartRepository.php | 100 ++++++++++++++ .../NewRepository/OrderDetailRepository.php | 2 +- src/Service/ShopContent/CarriersService.php | 2 +- src/Service/ShopContent/CartsService.php | 95 +++++++++++++ 9 files changed, 209 insertions(+), 132 deletions(-) delete mode 100644 src/Repository/CartRepository.php create mode 100644 src/Repository/NewRepository/CartRepository.php create mode 100644 src/Service/ShopContent/CartsService.php diff --git a/config/common/new-repository.yml b/config/common/new-repository.yml index fc336efc..7583357d 100644 --- a/config/common/new-repository.yml +++ b/config/common/new-repository.yml @@ -18,3 +18,7 @@ services: PrestaShop\Module\PsEventbus\Repository\NewRepository\CarrierRepository: class: PrestaShop\Module\PsEventbus\Repository\NewRepository\CarrierRepository public: true + + PrestaShop\Module\PsEventbus\Repository\NewRepository\CartRepository: + class: PrestaShop\Module\PsEventbus\Repository\NewRepository\CartRepository + public: true diff --git a/config/front/services.yml b/config/front/services.yml index 2f0b31bc..cf2dce09 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -96,3 +96,9 @@ services: public: true arguments: - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\CarrierRepository' + + PrestaShop\Module\PsEventbus\Service\ShopContent\CartsService: + class: PrestaShop\Module\PsEventbus\Service\ShopContent\CartsService + public: true + arguments: + - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\CartRepository' diff --git a/e2e/src/helpers/shop-contents.ts b/e2e/src/helpers/shop-contents.ts index 687187c6..deca7881 100644 --- a/e2e/src/helpers/shop-contents.ts +++ b/e2e/src/helpers/shop-contents.ts @@ -37,6 +37,7 @@ export const shopContentMapping = { 'carriers': 'carriers', 'carrier_details': 'carrier-details', 'carrier_taxes': 'carrier-taxes', + 'carts': 'carts', 'orders': 'orders', 'order_cart_rules': 'order-cart-rules', 'order_details': 'order-details', diff --git a/src/Repository/CartRepository.php b/src/Repository/CartRepository.php deleted file mode 100644 index 77df6184..00000000 --- a/src/Repository/CartRepository.php +++ /dev/null @@ -1,129 +0,0 @@ -db = \Db::getInstance(); - $this->context = $context; - } - - /** - * @return \DbQuery - */ - private function getBaseQuery() - { - if ($this->context->shop === null) { - throw new \PrestaShopException('No shop context'); - } - - $shopId = (int) $this->context->shop->id; - - $query = new \DbQuery(); - - $query->from('cart', 'c') - ->where('c.id_shop = ' . $shopId); - - return $query; - } - - /** - * @param int $offset - * @param int $limit - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getCarts($offset, $limit) - { - $query = $this->getBaseQuery(); - - $this->addSelectParameters($query); - - $query->limit($limit, $offset); - - return $this->db->executeS($query); - } - - /** - * @param int $offset - * - * @return int - */ - public function getRemainingCartsCount($offset) - { - $query = $this->getBaseQuery(); - - $query->select('(COUNT(c.id_cart) - ' . (int) $offset . ') as count'); - - return (int) $this->db->getValue($query); - } - - /** - * @param int $limit - * @param array $cartIds - * - * @return array - * - * @throws \PrestaShopDatabaseException - */ - public function getCartsIncremental($limit, $cartIds) - { - $query = $this->getBaseQuery(); - - $this->addSelectParameters($query); - - $query->where('c.id_cart IN(' . implode(',', array_map('intval', $cartIds)) . ')') - ->limit($limit); - - $result = $this->db->executeS($query); - - return is_array($result) ? $result : []; - } - - /** - * @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) - { - $query->select('c.id_cart, date_add as created_at, date_upd as updated_at'); - } -} diff --git a/src/Repository/NewRepository/CarrierRepository.php b/src/Repository/NewRepository/CarrierRepository.php index ea8c5d74..41f30bc6 100644 --- a/src/Repository/NewRepository/CarrierRepository.php +++ b/src/Repository/NewRepository/CarrierRepository.php @@ -28,7 +28,7 @@ public function generateFullQuery($langIso) $langId = (int) \Language::getIdByIso($langIso); $context = \Context::getContext(); - if ($context === null) { + if ($context == null) { throw new \PrestaShopException('Context is null'); } diff --git a/src/Repository/NewRepository/CartRepository.php b/src/Repository/NewRepository/CartRepository.php new file mode 100644 index 00000000..f72bc3ec --- /dev/null +++ b/src/Repository/NewRepository/CartRepository.php @@ -0,0 +1,100 @@ +query = new \DbQuery(); + + $this->query->from(self::TABLE_NAME, 'c'); + } + + /** + * @param string $langIso + * + * @return mixed + * + * @throws \PrestaShopException + */ + public function generateFullQuery($langIso) + { + $this->generateMinimalQuery(); + + $this->query->where('c.id_shop = ' . (int) parent::getShopId()); + + $this->query + ->select('c.id_cart') + ->select('date_add as created_at') + ->select('date_upd as updated_at') + ; + } + + /** + * @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->generateFullQuery($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->generateFullQuery($langIso); + + $this->query + ->where('c.id_cart IN(' . implode(',', array_map('intval', $contentIds)) . ')') + ->limit($limit) + ; + + return $this->runQuery($debug); + } + + /** + * @param int $offset + * + * @return int + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function countFullSyncContentLeft($offset) + { + $this->generateMinimalQuery(); + + $this->query->select('(COUNT(c.id_cart) - ' . (int) $offset . ') as count'); + + $result = $this->runQuery(false); + + return $result[0]['count']; + } +} diff --git a/src/Repository/NewRepository/OrderDetailRepository.php b/src/Repository/NewRepository/OrderDetailRepository.php index 477deda2..ef5b5a6b 100644 --- a/src/Repository/NewRepository/OrderDetailRepository.php +++ b/src/Repository/NewRepository/OrderDetailRepository.php @@ -27,7 +27,7 @@ public function generateFullQuery($langIso) { $context = \Context::getContext(); - if ($context === null) { + if ($context == null) { throw new \PrestaShopException('Context is null'); } diff --git a/src/Service/ShopContent/CarriersService.php b/src/Service/ShopContent/CarriersService.php index be82418e..52bf6944 100644 --- a/src/Service/ShopContent/CarriersService.php +++ b/src/Service/ShopContent/CarriersService.php @@ -95,7 +95,7 @@ private function castCarriers(&$carriers) { $context = \Context::getContext(); - if ($context === null) { + if ($context == null) { throw new \PrestaShopException('Context is null'); } diff --git a/src/Service/ShopContent/CartsService.php b/src/Service/ShopContent/CartsService.php new file mode 100644 index 00000000..ff7cf82c --- /dev/null +++ b/src/Service/ShopContent/CartsService.php @@ -0,0 +1,95 @@ +cartRepository = $cartRepository; + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForFull($offset, $limit, $langIso, $debug) + { + $result = $this->cartRepository->getContentsForFull($offset, $limit, $langIso, $debug); + + if (empty($result)) { + return []; + } + + $this->castCarts($result); + + return array_map(function ($item) { + return [ + 'id' => (string) $item['id_cart'], + 'collection' => Config::COLLECTION_CARTS, + '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->cartRepository->getContentsForIncremental($limit, $contentIds, $langIso, $debug); + + if (empty($result)) { + return []; + } + + $this->castCarts($result); + + return array_map(function ($item) { + return [ + 'id' => (string) $item['id_cart'], + 'collection' => Config::COLLECTION_CARTS, + 'properties' => $item, + ]; + }, $result); + } + + /** + * @param int $offset + * + * @return int + */ + public function countFullSyncContentLeft($offset) + { + return $this->cartRepository->countFullSyncContentLeft($offset); + } + + /** + * @param array $carts + * + * @return void + */ + private function castCarts(&$carts) + { + foreach ($carts as &$cart) { + $cart['id_cart'] = (string) $cart['id_cart']; + $cart['created_at'] = (string) $cart['created_at']; + $cart['updated_at'] = (string) $cart['updated_at']; + } + } +}