first commit
This commit is contained in:
175
modules/ps_eventbus/src/Provider/CartDataProvider.php
Normal file
175
modules/ps_eventbus/src/Provider/CartDataProvider.php
Normal file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\Module\PsEventbus\Provider;
|
||||
|
||||
use PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter;
|
||||
use PrestaShop\Module\PsEventbus\Repository\CartProductRepository;
|
||||
use PrestaShop\Module\PsEventbus\Repository\CartRepository;
|
||||
|
||||
class CartDataProvider implements PaginatedApiDataProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var CartRepository
|
||||
*/
|
||||
private $cartRepository;
|
||||
/**
|
||||
* @var CartProductRepository
|
||||
*/
|
||||
private $cartProductRepository;
|
||||
/**
|
||||
* @var ArrayFormatter
|
||||
*/
|
||||
private $arrayFormatter;
|
||||
|
||||
/**
|
||||
* @param CartRepository $cartRepository
|
||||
* @param CartProductRepository $cartProductRepository
|
||||
* @param ArrayFormatter $arrayFormatter
|
||||
*/
|
||||
public function __construct(
|
||||
CartRepository $cartRepository,
|
||||
CartProductRepository $cartProductRepository,
|
||||
ArrayFormatter $arrayFormatter
|
||||
) {
|
||||
$this->cartRepository = $cartRepository;
|
||||
$this->cartProductRepository = $cartProductRepository;
|
||||
$this->arrayFormatter = $arrayFormatter;
|
||||
}
|
||||
|
||||
public function getFormattedData($offset, $limit, $langIso)
|
||||
{
|
||||
$carts = $this->cartRepository->getCarts($offset, $limit);
|
||||
|
||||
if (!is_array($carts)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$cartProducts = $this->getCartProducts($carts);
|
||||
|
||||
$this->castCartValues($carts);
|
||||
|
||||
$carts = array_map(function ($cart) {
|
||||
return [
|
||||
'id' => $cart['id_cart'],
|
||||
'collection' => 'carts',
|
||||
'properties' => $cart,
|
||||
];
|
||||
}, $carts);
|
||||
|
||||
return array_merge($carts, $cartProducts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRemainingObjectsCount($offset, $langIso)
|
||||
{
|
||||
return (int) $this->cartRepository->getRemainingCartsCount($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $carts
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function castCartValues(array &$carts)
|
||||
{
|
||||
foreach ($carts as &$cart) {
|
||||
$cart['id_cart'] = (string) $cart['id_cart'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $cartProducts
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function castCartProductValues(array &$cartProducts)
|
||||
{
|
||||
foreach ($cartProducts as &$cartProduct) {
|
||||
$cartProduct['id_cart_product'] = (string) "{$cartProduct['id_cart']}-{$cartProduct['id_product']}-{$cartProduct['id_product_attribute']}";
|
||||
$cartProduct['id_cart'] = (string) $cartProduct['id_cart'];
|
||||
$cartProduct['id_product'] = (string) $cartProduct['id_product'];
|
||||
$cartProduct['id_product_attribute'] = (string) $cartProduct['id_product_attribute'];
|
||||
$cartProduct['quantity'] = (int) $cartProduct['quantity'];
|
||||
}
|
||||
}
|
||||
|
||||
public function getFormattedDataIncremental($limit, $langIso)
|
||||
{
|
||||
$carts = $this->cartRepository->getCartsIncremental($limit);
|
||||
|
||||
if (!is_array($carts) || empty($carts)) {
|
||||
return [
|
||||
'ids' => [],
|
||||
'data' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$cartIds = $this->separateCartIds($carts);
|
||||
|
||||
$cartProducts = $this->getCartProducts($carts);
|
||||
|
||||
$this->castCartValues($carts);
|
||||
|
||||
$carts = array_map(function ($cart) {
|
||||
return [
|
||||
'id' => $cart['id_cart'],
|
||||
'collection' => 'carts',
|
||||
'properties' => $cart,
|
||||
];
|
||||
}, $carts);
|
||||
|
||||
return [
|
||||
'ids' => $cartIds,
|
||||
'data' => array_merge($carts, $cartProducts),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $carts
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \PrestaShopDatabaseException
|
||||
*/
|
||||
private function getCartProducts(array $carts)
|
||||
{
|
||||
$cartIds = array_map(function ($cart) {
|
||||
return (string) $cart['id_cart'];
|
||||
}, $carts);
|
||||
|
||||
$cartProducts = $this->cartProductRepository->getCartProducts($cartIds);
|
||||
|
||||
if (!is_array($cartProducts) || empty($cartProducts)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$this->castCartProductValues($cartProducts);
|
||||
|
||||
if (is_array($cartProducts)) {
|
||||
return array_map(function ($cartProduct) {
|
||||
return [
|
||||
'id' => "{$cartProduct['id_cart']}-{$cartProduct['id_product']}-{$cartProduct['id_product_attribute']}",
|
||||
'collection' => 'cart_products',
|
||||
'properties' => $cartProduct,
|
||||
];
|
||||
}, $cartProducts);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $carts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function separateCartIds(array $carts)
|
||||
{
|
||||
return $this->arrayFormatter->formatValueArray($carts, 'id_order', true);
|
||||
}
|
||||
}
|
||||
105
modules/ps_eventbus/src/Provider/CategoryDataProvider.php
Normal file
105
modules/ps_eventbus/src/Provider/CategoryDataProvider.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\Module\PsEventbus\Provider;
|
||||
|
||||
use PrestaShop\Module\PsEventbus\Decorator\CategoryDecorator;
|
||||
use PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter;
|
||||
use PrestaShop\Module\PsEventbus\Repository\CategoryRepository;
|
||||
|
||||
class CategoryDataProvider implements PaginatedApiDataProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var CategoryRepository
|
||||
*/
|
||||
private $categoryRepository;
|
||||
/**
|
||||
* @var CategoryDecorator
|
||||
*/
|
||||
private $categoryDecorator;
|
||||
/**
|
||||
* @var ArrayFormatter
|
||||
*/
|
||||
private $arrayFormatter;
|
||||
|
||||
public function __construct(CategoryRepository $categoryRepository, CategoryDecorator $categoryDecorator, ArrayFormatter $arrayFormatter)
|
||||
{
|
||||
$this->categoryRepository = $categoryRepository;
|
||||
$this->categoryDecorator = $categoryDecorator;
|
||||
$this->arrayFormatter = $arrayFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int $limit
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \PrestaShopDatabaseException
|
||||
*/
|
||||
public function getFormattedData($offset, $limit, $langIso)
|
||||
{
|
||||
$categories = $this->categoryRepository->getCategories($offset, $limit, $langIso);
|
||||
|
||||
if (!is_array($categories)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$this->categoryDecorator->decorateCategories($categories);
|
||||
|
||||
return array_map(function ($category) {
|
||||
return [
|
||||
'id' => "{$category['id_category']}-{$category['iso_code']}",
|
||||
'collection' => 'categories',
|
||||
'properties' => $category,
|
||||
];
|
||||
}, $categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRemainingObjectsCount($offset, $langIso)
|
||||
{
|
||||
return (int) $this->categoryRepository->getRemainingCategoriesCount($offset, $langIso);
|
||||
}
|
||||
|
||||
public function getFormattedDataIncremental($limit, $langIso)
|
||||
{
|
||||
$categories = $this->categoryRepository->getCategoriesIncremental($limit, $langIso);
|
||||
|
||||
if (!is_array($categories)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$categoryIds = $this->separateCategoryIds($categories);
|
||||
|
||||
$this->categoryDecorator->decorateCategories($categories);
|
||||
|
||||
$categories = array_map(function ($category) {
|
||||
return [
|
||||
'id' => "{$category['id_category']}-{$category['iso_code']}",
|
||||
'collection' => 'categories',
|
||||
'properties' => $category,
|
||||
];
|
||||
}, $categories);
|
||||
|
||||
return [
|
||||
'data' => $categories,
|
||||
'ids' => $categoryIds,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $categories
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function separateCategoryIds(array $categories)
|
||||
{
|
||||
return $this->arrayFormatter->formatValueArray($categories, 'id_category', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\Module\PsEventbus\Provider;
|
||||
|
||||
use Context;
|
||||
use PrestaShop\Module\PsEventbus\Repository\GoogleTaxonomyRepository;
|
||||
|
||||
class GoogleTaxonomyDataProvider implements PaginatedApiDataProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var GoogleTaxonomyRepository
|
||||
*/
|
||||
private $googleTaxonomyRepository;
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
private $context;
|
||||
|
||||
public function __construct(GoogleTaxonomyRepository $googleTaxonomyRepository, Context $context)
|
||||
{
|
||||
$this->googleTaxonomyRepository = $googleTaxonomyRepository;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
public function getFormattedData($offset, $limit, $langIso)
|
||||
{
|
||||
$data = $this->googleTaxonomyRepository->getTaxonomyCategories($offset, $limit, $this->context->shop->id);
|
||||
|
||||
if (!is_array($data)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(function ($googleTaxonomy) {
|
||||
$uniqueId = "{$googleTaxonomy['id_category']}-{$googleTaxonomy['id_category']}";
|
||||
$googleTaxonomy['taxonomy_id'] = $uniqueId;
|
||||
|
||||
return [
|
||||
'id' => $uniqueId,
|
||||
'collection' => 'taxonomies',
|
||||
'properties' => $googleTaxonomy,
|
||||
];
|
||||
}, $data);
|
||||
}
|
||||
|
||||
public function getRemainingObjectsCount($offset, $langIso)
|
||||
{
|
||||
return (int) $this->googleTaxonomyRepository->getRemainingTaxonomyRepositories($offset, $this->context->shop->id);
|
||||
}
|
||||
|
||||
public function getFormattedDataIncremental($limit, $langIso)
|
||||
{
|
||||
return [
|
||||
'ids' => [],
|
||||
'data' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
69
modules/ps_eventbus/src/Provider/ModuleDataProvider.php
Normal file
69
modules/ps_eventbus/src/Provider/ModuleDataProvider.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\Module\PsEventbus\Provider;
|
||||
|
||||
use PrestaShop\Module\PsEventbus\Repository\ModuleRepository;
|
||||
use PrestaShopDatabaseException;
|
||||
|
||||
class ModuleDataProvider implements PaginatedApiDataProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var ModuleRepository
|
||||
*/
|
||||
private $moduleRepository;
|
||||
|
||||
public function __construct(ModuleRepository $moduleRepository)
|
||||
{
|
||||
$this->moduleRepository = $moduleRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int $limit
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFormattedData($offset, $limit, $langIso)
|
||||
{
|
||||
try {
|
||||
$modules = $this->moduleRepository->getModules($offset, $limit);
|
||||
} catch (PrestaShopDatabaseException $e) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!is_array($modules)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(function ($module) {
|
||||
$moduleId = (string) $module['module_id'];
|
||||
$module['active'] = $module['active'] == '1';
|
||||
|
||||
return [
|
||||
'id' => $moduleId,
|
||||
'collection' => 'modules',
|
||||
'properties' => $module,
|
||||
];
|
||||
}, $modules);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRemainingObjectsCount($offset, $langIso)
|
||||
{
|
||||
return (int) $this->moduleRepository->getRemainingModules($offset);
|
||||
}
|
||||
|
||||
public function getFormattedDataIncremental($limit, $langIso = null)
|
||||
{
|
||||
return [
|
||||
'ids' => [],
|
||||
'data' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
214
modules/ps_eventbus/src/Provider/OrderDataProvider.php
Normal file
214
modules/ps_eventbus/src/Provider/OrderDataProvider.php
Normal file
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\Module\PsEventbus\Provider;
|
||||
|
||||
use Context;
|
||||
use PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter;
|
||||
use PrestaShop\Module\PsEventbus\Repository\OrderDetailsRepository;
|
||||
use PrestaShop\Module\PsEventbus\Repository\OrderRepository;
|
||||
use PrestaShopDatabaseException;
|
||||
|
||||
class OrderDataProvider implements PaginatedApiDataProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var OrderRepository
|
||||
*/
|
||||
private $orderRepository;
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
private $context;
|
||||
/**
|
||||
* @var ArrayFormatter
|
||||
*/
|
||||
private $arrayFormatter;
|
||||
/**
|
||||
* @var OrderDetailsRepository
|
||||
*/
|
||||
private $orderDetailsRepository;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
OrderRepository $orderRepository,
|
||||
OrderDetailsRepository $orderDetailsRepository,
|
||||
ArrayFormatter $arrayFormatter
|
||||
) {
|
||||
$this->orderRepository = $orderRepository;
|
||||
$this->context = $context;
|
||||
$this->arrayFormatter = $arrayFormatter;
|
||||
$this->orderDetailsRepository = $orderDetailsRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int $limit
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws PrestaShopDatabaseException
|
||||
*/
|
||||
public function getFormattedData($offset, $limit, $langIso)
|
||||
{
|
||||
$orders = $this->orderRepository->getOrders($offset, $limit, $this->context->shop->id);
|
||||
|
||||
if (!is_array($orders)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$this->castOrderValues($orders);
|
||||
|
||||
$orderDetails = $this->getOrderDetails($orders, $this->context->shop->id);
|
||||
|
||||
$orders = array_map(function ($order) {
|
||||
return [
|
||||
'id' => $order['id_order'],
|
||||
'collection' => 'orders',
|
||||
'properties' => $order,
|
||||
];
|
||||
}, $orders);
|
||||
|
||||
return array_merge($orders, $orderDetails);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRemainingObjectsCount($offset, $langIso)
|
||||
{
|
||||
return (int) $this->orderRepository->getRemainingOrderCount($offset, $this->context->shop->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $orders
|
||||
* @param int $shopId
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws PrestaShopDatabaseException
|
||||
*/
|
||||
private function getOrderDetails(array $orders, $shopId)
|
||||
{
|
||||
if (empty($orders)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$orderIds = $this->arrayFormatter->formatValueArray($orders, 'id_order');
|
||||
|
||||
$orderDetails = $this->orderDetailsRepository->getOrderDetails($orderIds, $shopId);
|
||||
|
||||
if (!is_array($orderDetails) || empty($orderDetails)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$this->castOrderDetailValues($orderDetails);
|
||||
|
||||
$orderDetails = array_map(function ($orderDetail) {
|
||||
return [
|
||||
'id' => $orderDetail['id_order_detail'],
|
||||
'collection' => 'order_details',
|
||||
'properties' => $orderDetail,
|
||||
];
|
||||
}, $orderDetails);
|
||||
|
||||
return $orderDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $orders
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function castOrderValues(array &$orders)
|
||||
{
|
||||
foreach ($orders as &$order) {
|
||||
$order['id_order'] = (int) $order['id_order'];
|
||||
$order['id_customer'] = (int) $order['id_customer'];
|
||||
$order['current_state'] = (int) $order['current_state'];
|
||||
$order['conversion_rate'] = (float) $order['conversion_rate'];
|
||||
$order['total_paid_tax_incl'] = (float) $order['total_paid_tax_incl'];
|
||||
$order['total_paid_tax_excl'] = (float) $order['total_paid_tax_excl'];
|
||||
$order['refund'] = (float) $order['refund'];
|
||||
$order['refund_tax_excl'] = (float) $order['refund_tax_excl'];
|
||||
$order['new_customer'] = $order['new_customer'] === '1';
|
||||
$order['is_paid'] = (float) $order['total_paid_real'] >= (float) $order['total_paid_tax_incl'];
|
||||
$order['shipping_cost'] = (float) $order['shipping_cost'];
|
||||
$order['total_paid_tax'] = $order['total_paid_tax_incl'] - $order['total_paid_tax_excl'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $orderDetails
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function castOrderDetailValues(array &$orderDetails)
|
||||
{
|
||||
foreach ($orderDetails as &$orderDetail) {
|
||||
$orderDetail['id_order_detail'] = (int) $orderDetail['id_order_detail'];
|
||||
$orderDetail['id_order'] = (int) $orderDetail['id_order'];
|
||||
$orderDetail['product_id'] = (int) $orderDetail['product_id'];
|
||||
$orderDetail['product_attribute_id'] = (int) $orderDetail['product_attribute_id'];
|
||||
$orderDetail['product_quantity'] = (int) $orderDetail['product_quantity'];
|
||||
$orderDetail['unit_price_tax_incl'] = (float) $orderDetail['unit_price_tax_incl'];
|
||||
$orderDetail['unit_price_tax_excl'] = (float) $orderDetail['unit_price_tax_excl'];
|
||||
$orderDetail['refund'] = (float) $orderDetail['refund'] > 0 ? -1 * (float) $orderDetail['refund'] : 0;
|
||||
$orderDetail['refund_tax_excl'] = (float) $orderDetail['refund_tax_excl'] > 0 ? -1 * (float) $orderDetail['refund_tax_excl'] : 0;
|
||||
$orderDetail['category'] = (int) $orderDetail['category'];
|
||||
$orderDetail['unique_product_id'] = "{$orderDetail['product_id']}-{$orderDetail['product_attribute_id']}-{$orderDetail['iso_code']}";
|
||||
$orderDetail['conversion_rate'] = (float) $orderDetail['conversion_rate'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws PrestaShopDatabaseException
|
||||
*/
|
||||
public function getFormattedDataIncremental($limit, $langIso)
|
||||
{
|
||||
$orders = $this->orderRepository->getOrdersIncremental($limit, $this->context->shop->id);
|
||||
|
||||
if (!is_array($orders) || empty($orders)) {
|
||||
return [
|
||||
'ids' => [],
|
||||
'data' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$orderIds = $this->separateOrderIds($orders);
|
||||
|
||||
$this->castOrderValues($orders);
|
||||
|
||||
$orderDetails = $this->getOrderDetails($orders, $this->context->shop->id);
|
||||
|
||||
$orders = array_map(function ($order) {
|
||||
return [
|
||||
'id' => $order['id_order'],
|
||||
'collection' => 'orders',
|
||||
'properties' => $order,
|
||||
];
|
||||
}, $orders);
|
||||
|
||||
return [
|
||||
'data' => array_merge($orders, $orderDetails),
|
||||
'ids' => $orderIds,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $orders
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function separateOrderIds(array $orders)
|
||||
{
|
||||
return $this->arrayFormatter->formatValueArray($orders, 'id_order', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\Module\PsEventbus\Provider;
|
||||
|
||||
use PrestaShopDatabaseException;
|
||||
|
||||
interface PaginatedApiDataProviderInterface
|
||||
{
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int $limit
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws PrestaShopDatabaseException
|
||||
*/
|
||||
public function getFormattedData($offset, $limit, $langIso);
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws PrestaShopDatabaseException
|
||||
*/
|
||||
public function getRemainingObjectsCount($offset, $langIso);
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws PrestaShopDatabaseException
|
||||
*/
|
||||
public function getFormattedDataIncremental($limit, $langIso);
|
||||
}
|
||||
132
modules/ps_eventbus/src/Provider/ProductDataProvider.php
Normal file
132
modules/ps_eventbus/src/Provider/ProductDataProvider.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\Module\PsEventbus\Provider;
|
||||
|
||||
use PrestaShop\Module\PsEventbus\Decorator\ProductDecorator;
|
||||
use PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter;
|
||||
use PrestaShop\Module\PsEventbus\Repository\LanguageRepository;
|
||||
use PrestaShop\Module\PsEventbus\Repository\ProductRepository;
|
||||
|
||||
class ProductDataProvider implements PaginatedApiDataProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var ProductRepository
|
||||
*/
|
||||
private $productRepository;
|
||||
/**
|
||||
* @var ProductDecorator
|
||||
*/
|
||||
private $productDecorator;
|
||||
/**
|
||||
* @var LanguageRepository
|
||||
*/
|
||||
private $languageRepository;
|
||||
/**
|
||||
* @var ArrayFormatter
|
||||
*/
|
||||
private $arrayFormatter;
|
||||
|
||||
public function __construct(
|
||||
ProductRepository $productRepository,
|
||||
ProductDecorator $productDecorator,
|
||||
LanguageRepository $languageRepository,
|
||||
ArrayFormatter $arrayFormatter
|
||||
) {
|
||||
$this->productRepository = $productRepository;
|
||||
$this->productDecorator = $productDecorator;
|
||||
$this->languageRepository = $languageRepository;
|
||||
$this->arrayFormatter = $arrayFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int $limit
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \PrestaShopDatabaseException
|
||||
*/
|
||||
public function getFormattedData($offset, $limit, $langIso)
|
||||
{
|
||||
$langId = $this->languageRepository->getLanguageIdByIsoCode($langIso);
|
||||
|
||||
$products = $this->productRepository->getProducts($offset, $limit, $langId);
|
||||
|
||||
$this->productDecorator->decorateProducts($products, $langIso, $langId);
|
||||
|
||||
return array_map(function ($product) {
|
||||
return [
|
||||
'id' => $product['unique_product_id'],
|
||||
'collection' => 'products',
|
||||
'properties' => $product,
|
||||
];
|
||||
}, $products);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \PrestaShopDatabaseException
|
||||
*/
|
||||
public function getRemainingObjectsCount($offset, $langIso)
|
||||
{
|
||||
$langId = $this->languageRepository->getLanguageIdByIsoCode($langIso);
|
||||
|
||||
return (int) $this->productRepository->getRemainingProductsCount($offset, $langId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
* @param string $langIso
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \PrestaShopDatabaseException
|
||||
*/
|
||||
public function getFormattedDataIncremental($limit, $langIso)
|
||||
{
|
||||
$langId = $this->languageRepository->getLanguageIdByIsoCode($langIso);
|
||||
|
||||
$products = $this->productRepository->getProductsIncremental($limit, $langIso, $langId);
|
||||
|
||||
$productIds = $this->separateProductIds($products, count($products) < $limit);
|
||||
|
||||
if (!empty($products)) {
|
||||
$this->productDecorator->decorateProducts($products, $langIso, $langId);
|
||||
}
|
||||
|
||||
$data = array_map(function ($product) {
|
||||
return [
|
||||
'id' => $product['unique_product_id'],
|
||||
'collection' => 'products',
|
||||
'properties' => $product,
|
||||
];
|
||||
}, $products);
|
||||
|
||||
return [
|
||||
'data' => $data,
|
||||
'ids' => $productIds,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $products
|
||||
* @param bool $includeLast
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function separateProductIds($products, $includeLast)
|
||||
{
|
||||
$productIds = $this->arrayFormatter->formatValueArray($products, 'id_product', true);
|
||||
|
||||
if (!$includeLast) {
|
||||
array_pop($productIds);
|
||||
}
|
||||
|
||||
return $productIds;
|
||||
}
|
||||
}
|
||||
34
modules/ps_eventbus/src/Provider/index.php
Normal file
34
modules/ps_eventbus/src/Provider/index.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License (AFL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/afl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA
|
||||
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
Reference in New Issue
Block a user