85 lines
2.4 KiB
PHP
85 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\SalesBundle\Feed;
|
|
|
|
use KupShop\FeedGeneratorBundle\Feed\ConfigurableFeedTrait;
|
|
use KupShop\FeedGeneratorBundle\Feed\IConfigurableFeed;
|
|
use KupShop\FeedsBundle\Feed\IFeed;
|
|
use KupShop\SalesBundle\Feed\Wrapper\SaleWrapper;
|
|
use KupShop\SalesBundle\SaleList\SaleList;
|
|
use Query\Operator;
|
|
use Query\QueryBuilder;
|
|
|
|
class SaleConfigurableFeed implements IFeed, IConfigurableFeed
|
|
{
|
|
use ConfigurableFeedTrait;
|
|
|
|
public static string $type = 'saleConfigurable';
|
|
|
|
public static string $alias = 'Prodejky';
|
|
|
|
public static string $objectType = 'sale';
|
|
|
|
private ?SaleList $saleListCache = null;
|
|
|
|
public function __construct(
|
|
protected SaleWrapper $saleWrapper,
|
|
protected readonly SaleList $saleList,
|
|
) {
|
|
$this->objectWrapper = $saleWrapper;
|
|
}
|
|
|
|
public static function isAllowed(): bool
|
|
{
|
|
return findModule(\Modules::SALES);
|
|
}
|
|
|
|
public function filterByObjectID($objectID): void
|
|
{
|
|
$this->specs[] = Operator::equals(['s.id' => $objectID]);
|
|
}
|
|
|
|
public function getData(array $feedRow, ?int $limit = null): \Generator
|
|
{
|
|
$saleList = $this->saleListCache ?? $this->getSaleList($feedRow);
|
|
|
|
$totalCount = (int) $saleList
|
|
->getQueryBuilder()
|
|
->select('count(s.id) AS c')
|
|
->execute()->fetchOne();
|
|
|
|
$iterationLimit = isset($limit) ? 1 : ($totalCount / (float) static::$batchSize);
|
|
for ($i = 0; $i < $iterationLimit; $i++) {
|
|
$saleList->limit($limit ?? static::$batchSize, $i * static::$batchSize);
|
|
foreach ($saleList->getSales() as $sale) {
|
|
yield $this->prepareSingleObject($sale);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function getSaleList(array $feedRow): SaleList
|
|
{
|
|
$saleList = clone $this->saleList;
|
|
|
|
$settings = json_decode($feedRow['settings'] ?? '', true) ?? [];
|
|
$filter = $settings['filter'] ?? [];
|
|
|
|
$days = $filter['sale_days'] ?? 1;
|
|
|
|
$saleList->fetchItems();
|
|
$saleList->andSpec(Operator::andX($this->specs));
|
|
|
|
// hard limit
|
|
$saleList->andSpec(function (QueryBuilder $qb) use ($days) {
|
|
$qb->setParameter('days', $days);
|
|
$qb->setMaxResults(100000);
|
|
|
|
return 's.date_created >= (DATE(NOW()) - INTERVAL :days DAY)';
|
|
});
|
|
|
|
return $saleList;
|
|
}
|
|
}
|