<?php
namespace App\Application\Service\Taxonomy;
use App\Application\Service\Session\SessionService;
use App\Manager\System\ConfigurationManager;
use App\Manager\System\ProductManager;
use App\Manager\System\TaxonomyInfoManager;
use App\Manager\System\TaxonomyLanguageManager;
use App\Manager\System\TaxonomyManager;
use App\Application\Service\Helper\ArrayHelper;
use App\Entity\System\Taxonomy;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Application\Service\Cache\CacheAdapterService;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class TaxonomyService
{
public const CACHE_TAXONOMY_CHILDS = 'Taxonomy_childs';
public const CACHE_TAXONOMY_LANGUAGE_INDEXED = 'taxonomy_language_indexed';
public const ORDER_BY_KEY_NAME = 'name';
protected TaxonomyManager $taxonomyManager;
protected TaxonomyLanguageManager $taxonomyLanguageManager;
protected RequestStack $requestStack;
protected ConfigurationManager $configurationManager;
protected ProductManager $productManager;
protected CacheAdapterService $cacheAdapterService;
private ArrayHelper $arrayHelper;
private TaxonomyInfoManager $taxonomyInfoManager;
private RouterInterface $router;
private SessionService $sessionService;
public function __construct(
TaxonomyManager $taxonomyManager,
TaxonomyLanguageManager $taxonomyLanguageManager,
RequestStack $requestStack,
ConfigurationManager $configurationManager,
ProductManager $productManager,
CacheAdapterService $cacheAdapterService,
ArrayHelper $arrayHelper,
TaxonomyInfoManager $taxonomyInfoManager,
RouterInterface $router,
SessionService $sessionService
) {
$this->taxonomyManager = $taxonomyManager;
$this->taxonomyLanguageManager = $taxonomyLanguageManager;
$this->requestStack = $requestStack;
$this->configurationManager = $configurationManager;
$this->productManager = $productManager;
$this->cacheAdapterService = $cacheAdapterService;
$this->arrayHelper = $arrayHelper;
$this->taxonomyInfoManager = $taxonomyInfoManager;
$this->router = $router;
$this->sessionService = $sessionService;
}
/**
* @param int|null $taxonomyParentId
* @param int|null $depth
*
* @return array
*
* @throws InvalidArgumentException
*/
public function getTaxonomyChilds(?int $taxonomyParentId = null, ?int $depth = 0): ?array
{
$automaticCacheKey = $this->requestStack->getSession()->get('lang').'_'.$taxonomyParentId.'_'.$depth.'_';
if ($depth >= Taxonomy::MAX_DEPTH_LEVEL) {
return null;
}
if ($data = $this->cacheAdapterService->existCache($automaticCacheKey.self::CACHE_TAXONOMY_CHILDS)) {
return $data;
}
$taxonomyInfo = [];
$depth++;
if (null === $taxonomyParentId) {
$taxonomyIds = $this->taxonomyManager->getFirstLevelTaxonomiesIds();
} else {
$taxonomyIds = $this->taxonomyManager->getChildrenTaxonomiesIds([$taxonomyParentId]);
}
foreach ($taxonomyIds as $taxonomyId) {
$taxonomyInfoData = $this->getTaxonomyInfo($taxonomyId['id'], $depth);
if ($taxonomyInfoData && $taxonomyInfoData['n_products'] > 0) {
$taxonomyInfo[] = $taxonomyInfoData;
}
}
$taxonomyInfo = $this->arrayHelper->orderArrayMultidimensionalByKey($taxonomyInfo, self::ORDER_BY_KEY_NAME);
return $this->cacheAdapterService->getCache($automaticCacheKey.self::CACHE_TAXONOMY_CHILDS, $taxonomyInfo);
}
/**
* @param int|null $taxonomyParentId
*
* @return array
*/
protected function getTaxonomiesQueryCriteria(?int $taxonomyParentId = null): array
{
$criteria = [];
$criteria['parent'] = $taxonomyParentId;
return $criteria;
}
protected function getTaxonomyInfo(int $taxonomyId, int $depth): ?array
{
$taxonomyInfo = $this->taxonomyManager->getTaxonomyInfo($taxonomyId, $this->sessionService->getLocaleId());
if (!$taxonomyInfo) {
return null;
}
return [
'id' => $taxonomyInfo['t_reference'],
'name' => $taxonomyInfo['tl_name'],
'link' => $this->router->generate(
'shop_category',
['url' => $taxonomyInfo['tl_link_rewrite'], 'lang' => $this->sessionService->getLocale()],
UrlGeneratorInterface::ABSOLUTE_URL
),
'childs' => $this->getTaxonomyChilds($taxonomyInfo['t_id'], $depth),
'n_products' => $taxonomyInfo['ts_count'],
];
}
/**
* @throws InvalidArgumentException
*/
public function getAllCachedTaxonomiesForLanguage(int $languageId)
{
if ($data = $this->cacheAdapterService->existCache($languageId.'_'.self::CACHE_TAXONOMY_LANGUAGE_INDEXED)) {
return $data;
}
$allTaxonomiesByLanguage = $this->taxonomyInfoManager->getTaxonomyInfoByLanguage($languageId);
return $this->cacheAdapterService->getCache($languageId.'_'.self::CACHE_TAXONOMY_LANGUAGE_INDEXED, $allTaxonomiesByLanguage);
}
}