Current File : /var/www/prestashop/modules/ps_eventbus/src/Repository/WishlistRepository.php |
<?php
namespace PrestaShop\Module\PsEventbus\Repository;
class WishlistRepository
{
/**
* @var \Db
*/
private $db;
/**
* @var \Context
*/
private $context;
public function __construct(\Context $context)
{
$this->db = \Db::getInstance();
$this->context = $context;
}
/**
* @return \DbQuery
*/
public function getBaseQuery()
{
if ($this->context->shop === null) {
throw new \PrestaShopException('No shop context');
}
$shopId = (int) $this->context->shop->id;
$query = new \DbQuery();
$query->from('wishlist', 'w')
->where('w.id_shop = ' . $shopId);
return $query;
}
/**
* @param int $offset
* @param int $limit
*
* @return array<mixed>|bool|\mysqli_result|\PDOStatement|resource|null
*
* @throws \PrestaShopDatabaseException
*/
public function getWishlists($offset, $limit)
{
// need this module for this table : https://addons.prestashop.com/en/undownloadable/9131-wishlist-block.html
if (empty($this->checkIfPsWishlistIsInstalled())) {
return [];
}
$query = $this->getBaseQuery();
$this->addSelectParameters($query);
$query->limit($limit, $offset);
return $this->db->executeS($query);
}
/**
* @param int $offset
*
* @return int
*/
public function getRemainingWishlistsCount($offset)
{
// need this module for this table : https://addons.prestashop.com/en/undownloadable/9131-wishlist-block.html
if (empty($this->checkIfPsWishlistIsInstalled())) {
return 0;
}
$query = $this->getBaseQuery()
->select('(COUNT(w.id_wishlist) - ' . (int) $offset . ') as count');
return (int) $this->db->getValue($query);
}
/**
* @param int $limit
* @param array<mixed> $wishlistIds
*
* @return array<mixed>|bool|\mysqli_result|\PDOStatement|resource|null
*
* @throws \PrestaShopDatabaseException
*/
public function getWishlistsIncremental($limit, $wishlistIds)
{
$query = $this->getBaseQuery();
$this->addSelectParameters($query);
$query->where('w.id_wishlist IN(' . implode(',', array_map('intval', $wishlistIds)) . ')')
->limit($limit);
return $this->db->executeS($query);
}
/**
* @param int $offset
* @param int $limit
*
* @return array<mixed>
*
* @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('w.id_wishlist, w.id_customer, w.id_shop, w.id_shop_group, w.token, w.name, w.counter');
$query->select('w.date_add AS created_at, w.date_upd as updated_at, w.default');
}
/**
* @return array<mixed>|bool|\mysqli_result|\PDOStatement|resource|null
*
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
*/
private function checkIfPsWishlistIsInstalled()
{
$moduleisInstalledQuery = 'SELECT * FROM information_schema.tables WHERE table_name LIKE \'%wishlist\' LIMIT 1;';
return $this->db->executeS($moduleisInstalledQuery);
}
}