Skip to content

Commit

Permalink
feat: pseventbus v4 carts (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john authored Sep 11, 2024
1 parent e99ffb6 commit 226d177
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 132 deletions.
4 changes: 4 additions & 0 deletions config/common/new-repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions config/front/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions e2e/src/helpers/shop-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
129 changes: 0 additions & 129 deletions src/Repository/CartRepository.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Repository/NewRepository/CarrierRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
100 changes: 100 additions & 0 deletions src/Repository/NewRepository/CartRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace PrestaShop\Module\PsEventbus\Repository\NewRepository;

class CartRepository extends AbstractRepository implements RepositoryInterface
{
const TABLE_NAME = 'cart';

/**
* @return void
*/
public function generateMinimalQuery()
{
$this->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<mixed>
*
* @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<mixed> $contentIds
* @param string $langIso
* @param bool $debug
*
* @return array<mixed>
*
* @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'];
}
}
2 changes: 1 addition & 1 deletion src/Repository/NewRepository/OrderDetailRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function generateFullQuery($langIso)
{
$context = \Context::getContext();

if ($context === null) {
if ($context == null) {
throw new \PrestaShopException('Context is null');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Service/ShopContent/CarriersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function castCarriers(&$carriers)
{
$context = \Context::getContext();

if ($context === null) {
if ($context == null) {
throw new \PrestaShopException('Context is null');
}

Expand Down
95 changes: 95 additions & 0 deletions src/Service/ShopContent/CartsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace PrestaShop\Module\PsEventbus\Service\ShopContent;

use PrestaShop\Module\PsEventbus\Config\Config;
use PrestaShop\Module\PsEventbus\Repository\NewRepository\CartRepository;

class CartsService implements ShopContentServiceInterface
{
/** @var CartRepository */
private $cartRepository;

public function __construct(CartRepository $cartRepository)
{
$this->cartRepository = $cartRepository;
}

/**
* @param int $offset
* @param int $limit
* @param string $langIso
* @param bool $debug
*
* @return array<mixed>
*/
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<string, int> $contentIds
* @param string $langIso
* @param bool $debug
*
* @return array<mixed>
*/
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<mixed> $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'];
}
}
}

0 comments on commit 226d177

Please sign in to comment.