Files
kupshop/bundles/External/ZNZBundle/Util/ZNZFeedUtil.php
2025-08-02 16:30:27 +02:00

84 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace External\ZNZBundle\Util;
use External\ZNZBundle\Dto\B2BFeedDefinition;
use KupShop\FeedsBundle\Exceptions\UnknownFeedTypeException;
use KupShop\FeedsBundle\FeedLocator;
use KupShop\KupShopBundle\Context\CountryContext;
use KupShop\KupShopBundle\Query\JsonOperator;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\XMLFeedBundle\Controller\B2BController;
use Query\Operator;
use Symfony\Component\HttpFoundation\Response;
/**
* Pomocna servisa pro praci s B2B feedama na ZNZ.
*/
class ZNZFeedUtil
{
public function __construct(
private FeedLocator $feedLocator,
private ?B2BController $B2BController = null,
) {
}
/**
* Outputs B2B user feed by `B2BFeedDefinition`.
*
* Feed is found by `name` which is build as `<category>_<type>` in administration.
*/
public function getB2BFeed(B2BFeedDefinition $feedDefinition, bool $download = false): ?Response
{
if (!$this->B2BController) {
return null;
}
if (!($user = \User::createFromFeedToken($feedDefinition->userHash))) {
return null;
}
$feedName = "{$feedDefinition->category}_{$feedDefinition->type}";
$feedRow = sqlQueryBuilder()
->select('*')
->from('feeds')
->where(Operator::equals([JsonOperator::value('data', 'note') => $feedName]))
->execute()->fetchAssociative();
if (!$feedRow) {
return null;
}
$feedRow['data'] = json_decode($feedRow['data'] ?? '', true) ?: [];
try {
$feed = $this->feedLocator->getServiceByType($feedRow['type']);
} catch (UnknownFeedTypeException) {
return null;
}
$feedRow['id_language'] = $feedDefinition->language;
$feedRow['id_currency'] = $feedDefinition->currency;
$feedRow['id_country'] = Contexts::get(CountryContext::class)->getActiveId();
return $this->B2BController->outputFeed($user, $feed, $feedRow, $feedDefinition->format, null, $download);
}
/**
* Returns ids of default feeds for users.
*
* @return int[]
*/
public function getDefaultUserFeeds(): array
{
return sqlQueryBuilder()
->select('id as i, id')
->from('feeds')
->where(Operator::like([JsonOperator::value('data', 'note') => '%_default']))
->execute()->fetchFirstColumn();
}
}