<?php
namespace App\Application\Service\Taxonomy;
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\Application\Service\Helper\LoggerService;
use App\Application\Service\Helper\PathService;
use App\Entity\System\Taxonomy;
use App\Entity\System\TaxonomyLanguage;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Application\Service\Cache\CacheAdapterService;
use Monolog\Logger;
class TaxonomyService
{
public const CACHE_IMAGES_FIRST_LEVEL_MENU = 'images_first_level_menu';
public const CACHE_TAXONOMY_FIRST_LEVEL_CHILDREN = 'taxonomy_first_level_children';
public const CACHE_TAXONOMY_CHILDS = 'Taxonomy_childs';
public const CACHE_TAXONOMY_LANGUAGE_INDEXED = 'taxonomy_language_indexed';
public const ORDER_BY_KEY_NAME = 'name';
public const NUMBER_PRODUCTS_TAXONOMY = 1000;
/** @var TaxonomyManager */
protected $taxonomyManager;
/** @var TaxonomyLanguageManager */
protected $taxonomyLanguageManager;
/** @var RequestStack */
protected $requestStack;
/** @var ConfigurationManager */
protected $configurationManager;
/** @var ProductManager */
protected $productManager;
/** @var CacheAdapterService */
protected $cacheAdapterService;
/** @var ArrayHelper */
private $arrayHelper;
/** @var TaxonomyInfoManager */
private $taxonomyInfoManager;
/** @var LoggerService */
private $loggerService;
/** @var PathService */
private $pathService;
public function __construct(
TaxonomyManager $taxonomyManager,
TaxonomyLanguageManager $taxonomyLanguageManager,
RequestStack $requestStack,
ConfigurationManager $configurationManager,
ProductManager $productManager,
CacheAdapterService $cacheAdapterService,
ArrayHelper $arrayHelper,
TaxonomyInfoManager $taxonomyInfoManager,
LoggerService $loggerService,
PathService $pathService
) {
$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->loggerService = $loggerService;
$this->pathService = $pathService;
}
/**
* @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
{
$langId = $this->requestStack->getSession()->get('id_lang');
$taxonomyInfo = $this->taxonomyManager->getTaxonomyInfo($taxonomyId, $langId);
if (!$taxonomyInfo) {
return null;
}
/** TODO force taxonomies */
$taxonomyLink = $taxonomyInfo['tl_link_rewrite'];
if (empty($taxonomyLink)) {
$taxonomyLink = 'force-link-taxonomy';
}
/** end TODO force taxonomies */
return [
'id' => $taxonomyInfo['t_reference'],
'name' => $taxonomyInfo['tl_name'],
// 'link' => $this->linkGenerator->getFrontUrl($taxonomyLink),
'link' => BASE_URL.$this->requestStack->getSession()->get('lang').'/'.$taxonomyLink.'.html',
'childs' => $this->getTaxonomyChilds($taxonomyInfo['t_id'], $depth),
'n_products' => $taxonomyInfo['ts_count'],
];
}
/**
* @param int $taxonomyId
* @param int $langId
*
* @return array
*
* @throws InvalidArgumentException
*/
public function getTaxonomyFirstLevelChildren(int $taxonomyId, int $langId): array
{
$logger = $this->loggerService->getLogger(
'SYNC',
"[%datetime%] %level_name%: %message%\n",
LOGS_PATH.'taxanomy_first_level_children',
Logger::INFO
);
if ($data = $this->cacheAdapterService->existCache($langId.'_'.$taxonomyId.'_'.self::CACHE_TAXONOMY_FIRST_LEVEL_CHILDREN)) {
return $data;
}
$isoCode = $this->requestStack->getSession()->get('lang');
$baseHost = $this->pathService->getBaseHost();
$childrenTaxonomies = $this->taxonomyManager->findBy(['parent' => $taxonomyId]);
$childrenTaxonomiesAsArray = [];
foreach ($childrenTaxonomies as $childTaxonomy) {
try {
/** @var TaxonomyLanguage $childTaxonomyLanguage */
$childTaxonomyLanguage = $childTaxonomy->getLangs()->get($langId);
$childrenTaxonomiesAsArray[] = [
'id_category' => $childTaxonomy->getId(),
'name' => $childTaxonomyLanguage->getName(),
'link_rewrite' => $baseHost.$isoCode.DIRECTORY_SEPARATOR.$childTaxonomyLanguage->getLinkRewrite().'.html',
'childs' => ($childTaxonomy->getChildren()->count() > 0),
'parent' => $taxonomyId,
];
} catch (\Throwable $exception) {
$logger->error('Error with taxonomy: '.$childTaxonomy->getId().' Exception: '.$exception->getMessage());
continue;
}
}
return $this->cacheAdapterService->getCache($langId.'_'.$taxonomyId.'_'.self::CACHE_TAXONOMY_FIRST_LEVEL_CHILDREN, $childrenTaxonomiesAsArray);
}
/**
* @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);
}
}