src/Application/Service/Taxonomy/TaxonomyService.php line 158

Open in your IDE?
  1. <?php
  2. namespace App\Application\Service\Taxonomy;
  3. use App\Manager\System\ConfigurationManager;
  4. use App\Manager\System\ProductManager;
  5. use App\Manager\System\TaxonomyInfoManager;
  6. use App\Manager\System\TaxonomyLanguageManager;
  7. use App\Manager\System\TaxonomyManager;
  8. use App\Application\Service\Helper\ArrayHelper;
  9. use App\Application\Service\Helper\LoggerService;
  10. use App\Application\Service\Helper\PathService;
  11. use App\Entity\System\Taxonomy;
  12. use App\Entity\System\TaxonomyLanguage;
  13. use Psr\Cache\InvalidArgumentException;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use App\Application\Service\Cache\CacheAdapterService;
  16. use Monolog\Logger;
  17. class TaxonomyService
  18. {
  19.     public const CACHE_IMAGES_FIRST_LEVEL_MENU 'images_first_level_menu';
  20.     public const CACHE_TAXONOMY_FIRST_LEVEL_CHILDREN 'taxonomy_first_level_children';
  21.     public const CACHE_TAXONOMY_CHILDS 'Taxonomy_childs';
  22.     public const CACHE_TAXONOMY_LANGUAGE_INDEXED 'taxonomy_language_indexed';
  23.     public const ORDER_BY_KEY_NAME 'name';
  24.     public const NUMBER_PRODUCTS_TAXONOMY 1000;
  25.     /** @var TaxonomyManager */
  26.     protected $taxonomyManager;
  27.     /** @var TaxonomyLanguageManager */
  28.     protected $taxonomyLanguageManager;
  29.     /** @var RequestStack */
  30.     protected $requestStack;
  31.     /** @var ConfigurationManager */
  32.     protected $configurationManager;
  33.     /** @var ProductManager */
  34.     protected $productManager;
  35.     /** @var CacheAdapterService */
  36.     protected $cacheAdapterService;
  37.     /** @var ArrayHelper */
  38.     private $arrayHelper;
  39.     /** @var TaxonomyInfoManager */
  40.     private $taxonomyInfoManager;
  41.     /** @var LoggerService */
  42.     private $loggerService;
  43.     /** @var PathService */
  44.     private $pathService;
  45.     public function __construct(
  46.         TaxonomyManager $taxonomyManager,
  47.         TaxonomyLanguageManager $taxonomyLanguageManager,
  48.         RequestStack $requestStack,
  49.         ConfigurationManager $configurationManager,
  50.         ProductManager $productManager,
  51.         CacheAdapterService $cacheAdapterService,
  52.         ArrayHelper $arrayHelper,
  53.         TaxonomyInfoManager $taxonomyInfoManager,
  54.         LoggerService $loggerService,
  55.         PathService $pathService
  56.     ) {
  57.         $this->taxonomyManager $taxonomyManager;
  58.         $this->taxonomyLanguageManager $taxonomyLanguageManager;
  59.         $this->requestStack $requestStack;
  60.         $this->configurationManager $configurationManager;
  61.         $this->productManager $productManager;
  62.         $this->cacheAdapterService $cacheAdapterService;
  63.         $this->arrayHelper $arrayHelper;
  64.         $this->taxonomyInfoManager $taxonomyInfoManager;
  65.         $this->loggerService $loggerService;
  66.         $this->pathService $pathService;
  67.     }
  68.     /**
  69.      * @param int|null $taxonomyParentId
  70.      * @param int|null $depth
  71.      *
  72.      * @return array
  73.      *
  74.      * @throws InvalidArgumentException
  75.      */
  76.     public function getTaxonomyChilds(?int $taxonomyParentId null, ?int $depth 0): ?array
  77.     {
  78.         $automaticCacheKey $this->requestStack->getSession()->get('lang').'_'.$taxonomyParentId.'_'.$depth.'_';
  79.         if ($depth >= Taxonomy::MAX_DEPTH_LEVEL) {
  80.             return null;
  81.         }
  82.         if ($data $this->cacheAdapterService->existCache($automaticCacheKey.self::CACHE_TAXONOMY_CHILDS)) {
  83.             return $data;
  84.         }
  85.         $taxonomyInfo = [];
  86.         $depth++;
  87.         if (null === $taxonomyParentId) {
  88.             $taxonomyIds $this->taxonomyManager->getFirstLevelTaxonomiesIds();
  89.         } else {
  90.             $taxonomyIds $this->taxonomyManager->getChildrenTaxonomiesIds([$taxonomyParentId]);
  91.         }
  92.         foreach ($taxonomyIds as $taxonomyId) {
  93.             $taxonomyInfoData $this->getTaxonomyInfo($taxonomyId['id'], $depth);
  94.             if ($taxonomyInfoData && $taxonomyInfoData['n_products'] > 0) {
  95.                 $taxonomyInfo[] = $taxonomyInfoData;
  96.             }
  97.         }
  98.         $taxonomyInfo $this->arrayHelper->orderArrayMultidimensionalByKey($taxonomyInfoself::ORDER_BY_KEY_NAME);
  99.         return $this->cacheAdapterService->getCache($automaticCacheKey.self::CACHE_TAXONOMY_CHILDS$taxonomyInfo);
  100.     }
  101.     /**
  102.      * @param int|null $taxonomyParentId
  103.      *
  104.      * @return array
  105.      */
  106.     protected function getTaxonomiesQueryCriteria(?int $taxonomyParentId null): array
  107.     {
  108.         $criteria = [];
  109.         $criteria['parent'] = $taxonomyParentId;
  110.         return $criteria;
  111.     }
  112.     protected function getTaxonomyInfo(int $taxonomyIdint $depth): ?array
  113.     {
  114.         $langId $this->requestStack->getSession()->get('id_lang');
  115.         $taxonomyInfo $this->taxonomyManager->getTaxonomyInfo($taxonomyId$langId);
  116.         if (!$taxonomyInfo) {
  117.             return null;
  118.         }
  119.         /**  TODO force taxonomies */
  120.         $taxonomyLink $taxonomyInfo['tl_link_rewrite'];
  121.         if (empty($taxonomyLink)) {
  122.             $taxonomyLink 'force-link-taxonomy';
  123.         }
  124.         /** end TODO force taxonomies */
  125.         return [
  126.             'id' => $taxonomyInfo['t_reference'],
  127.             'name' => $taxonomyInfo['tl_name'],
  128.             // 'link' => $this->linkGenerator->getFrontUrl($taxonomyLink),
  129.             'link' => BASE_URL.$this->requestStack->getSession()->get('lang').'/'.$taxonomyLink.'.html',
  130.             'childs' => $this->getTaxonomyChilds($taxonomyInfo['t_id'], $depth),
  131.             'n_products' => $taxonomyInfo['ts_count'],
  132.         ];
  133.     }
  134.     /**
  135.      * @param int $taxonomyId
  136.      * @param int $langId
  137.      *
  138.      * @return array
  139.      *
  140.      * @throws InvalidArgumentException
  141.      */
  142.     public function getTaxonomyFirstLevelChildren(int $taxonomyIdint $langId): array
  143.     {
  144.         $logger $this->loggerService->getLogger(
  145.             'SYNC',
  146.             "[%datetime%] %level_name%: %message%\n",
  147.             LOGS_PATH.'taxanomy_first_level_children',
  148.             Logger::INFO
  149.         );
  150.         if ($data $this->cacheAdapterService->existCache($langId.'_'.$taxonomyId.'_'.self::CACHE_TAXONOMY_FIRST_LEVEL_CHILDREN)) {
  151.             return $data;
  152.         }
  153.         $isoCode $this->requestStack->getSession()->get('lang');
  154.         $baseHost $this->pathService->getBaseHost();
  155.         $childrenTaxonomies $this->taxonomyManager->findBy(['parent' => $taxonomyId]);
  156.         $childrenTaxonomiesAsArray = [];
  157.         foreach ($childrenTaxonomies as $childTaxonomy) {
  158.             try {
  159.                 /** @var TaxonomyLanguage $childTaxonomyLanguage */
  160.                 $childTaxonomyLanguage $childTaxonomy->getLangs()->get($langId);
  161.                 $childrenTaxonomiesAsArray[] = [
  162.                     'id_category' => $childTaxonomy->getId(),
  163.                     'name' => $childTaxonomyLanguage->getName(),
  164.                     'link_rewrite' => $baseHost.$isoCode.DIRECTORY_SEPARATOR.$childTaxonomyLanguage->getLinkRewrite().'.html',
  165.                     'childs' => ($childTaxonomy->getChildren()->count() > 0),
  166.                     'parent' => $taxonomyId,
  167.                 ];
  168.             } catch (\Throwable $exception) {
  169.                 $logger->error('Error with taxonomy: '.$childTaxonomy->getId().' Exception: '.$exception->getMessage());
  170.                 continue;
  171.             }
  172.         }
  173.         return $this->cacheAdapterService->getCache($langId.'_'.$taxonomyId.'_'.self::CACHE_TAXONOMY_FIRST_LEVEL_CHILDREN$childrenTaxonomiesAsArray);
  174.     }
  175.     /**
  176.      * @throws InvalidArgumentException
  177.      */
  178.     public function getAllCachedTaxonomiesForLanguage(int $languageId)
  179.     {
  180.         if ($data $this->cacheAdapterService->existCache($languageId.'_'.self::CACHE_TAXONOMY_LANGUAGE_INDEXED)) {
  181.             return $data;
  182.         }
  183.         $allTaxonomiesByLanguage $this->taxonomyInfoManager->getTaxonomyInfoByLanguage($languageId);
  184.         return $this->cacheAdapterService->getCache($languageId.'_'.self::CACHE_TAXONOMY_LANGUAGE_INDEXED$allTaxonomiesByLanguage);
  185.     }
  186. }