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

Open in your IDE?
  1. <?php
  2. namespace App\Application\Service\Taxonomy;
  3. use App\Application\Service\Session\SessionService;
  4. use App\Manager\System\ConfigurationManager;
  5. use App\Manager\System\ProductManager;
  6. use App\Manager\System\TaxonomyInfoManager;
  7. use App\Manager\System\TaxonomyLanguageManager;
  8. use App\Manager\System\TaxonomyManager;
  9. use App\Application\Service\Helper\ArrayHelper;
  10. use App\Entity\System\Taxonomy;
  11. use Psr\Cache\InvalidArgumentException;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use App\Application\Service\Cache\CacheAdapterService;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  16. class TaxonomyService
  17. {
  18.     public const CACHE_TAXONOMY_CHILDS 'Taxonomy_childs';
  19.     public const CACHE_TAXONOMY_LANGUAGE_INDEXED 'taxonomy_language_indexed';
  20.     public const ORDER_BY_KEY_NAME 'name';
  21.     protected TaxonomyManager $taxonomyManager;
  22.     protected TaxonomyLanguageManager $taxonomyLanguageManager;
  23.     protected RequestStack $requestStack;
  24.     protected ConfigurationManager $configurationManager;
  25.     protected ProductManager $productManager;
  26.     protected CacheAdapterService $cacheAdapterService;
  27.     private ArrayHelper $arrayHelper;
  28.     private TaxonomyInfoManager $taxonomyInfoManager;
  29.     private RouterInterface $router;
  30.     private SessionService $sessionService;
  31.     public function __construct(
  32.         TaxonomyManager $taxonomyManager,
  33.         TaxonomyLanguageManager $taxonomyLanguageManager,
  34.         RequestStack $requestStack,
  35.         ConfigurationManager $configurationManager,
  36.         ProductManager $productManager,
  37.         CacheAdapterService $cacheAdapterService,
  38.         ArrayHelper $arrayHelper,
  39.         TaxonomyInfoManager $taxonomyInfoManager,
  40.         RouterInterface $router,
  41.         SessionService $sessionService
  42.     ) {
  43.         $this->taxonomyManager $taxonomyManager;
  44.         $this->taxonomyLanguageManager $taxonomyLanguageManager;
  45.         $this->requestStack $requestStack;
  46.         $this->configurationManager $configurationManager;
  47.         $this->productManager $productManager;
  48.         $this->cacheAdapterService $cacheAdapterService;
  49.         $this->arrayHelper $arrayHelper;
  50.         $this->taxonomyInfoManager $taxonomyInfoManager;
  51.         $this->router $router;
  52.         $this->sessionService $sessionService;
  53.     }
  54.     /**
  55.      * @param int|null $taxonomyParentId
  56.      * @param int|null $depth
  57.      *
  58.      * @return array
  59.      *
  60.      * @throws InvalidArgumentException
  61.      */
  62.     public function getTaxonomyChilds(?int $taxonomyParentId null, ?int $depth 0): ?array
  63.     {
  64.         $automaticCacheKey $this->requestStack->getSession()->get('lang').'_'.$taxonomyParentId.'_'.$depth.'_';
  65.         if ($depth >= Taxonomy::MAX_DEPTH_LEVEL) {
  66.             return null;
  67.         }
  68.         if ($data $this->cacheAdapterService->existCache($automaticCacheKey.self::CACHE_TAXONOMY_CHILDS)) {
  69.             return $data;
  70.         }
  71.         $taxonomyInfo = [];
  72.         $depth++;
  73.         if (null === $taxonomyParentId) {
  74.             $taxonomyIds $this->taxonomyManager->getFirstLevelTaxonomiesIds();
  75.         } else {
  76.             $taxonomyIds $this->taxonomyManager->getChildrenTaxonomiesIds([$taxonomyParentId]);
  77.         }
  78.         foreach ($taxonomyIds as $taxonomyId) {
  79.             $taxonomyInfoData $this->getTaxonomyInfo($taxonomyId['id'], $depth);
  80.             if ($taxonomyInfoData && $taxonomyInfoData['n_products'] > 0) {
  81.                 $taxonomyInfo[] = $taxonomyInfoData;
  82.             }
  83.         }
  84.         $taxonomyInfo $this->arrayHelper->orderArrayMultidimensionalByKey($taxonomyInfoself::ORDER_BY_KEY_NAME);
  85.         return $this->cacheAdapterService->getCache($automaticCacheKey.self::CACHE_TAXONOMY_CHILDS$taxonomyInfo);
  86.     }
  87.     /**
  88.      * @param int|null $taxonomyParentId
  89.      *
  90.      * @return array
  91.      */
  92.     protected function getTaxonomiesQueryCriteria(?int $taxonomyParentId null): array
  93.     {
  94.         $criteria = [];
  95.         $criteria['parent'] = $taxonomyParentId;
  96.         return $criteria;
  97.     }
  98.     protected function getTaxonomyInfo(int $taxonomyIdint $depth): ?array
  99.     {
  100.         $taxonomyInfo $this->taxonomyManager->getTaxonomyInfo($taxonomyId$this->sessionService->getLocaleId());
  101.         if (!$taxonomyInfo) {
  102.             return null;
  103.         }
  104.         return [
  105.             'id' => $taxonomyInfo['t_reference'],
  106.             'name' => $taxonomyInfo['tl_name'],
  107.             'link' => $this->router->generate(
  108.                 'shop_category',
  109.                 ['url' => $taxonomyInfo['tl_link_rewrite'], 'lang' => $this->sessionService->getLocale()],
  110.                 UrlGeneratorInterface::ABSOLUTE_URL
  111.             ),
  112.             'childs' => $this->getTaxonomyChilds($taxonomyInfo['t_id'], $depth),
  113.             'n_products' => $taxonomyInfo['ts_count'],
  114.         ];
  115.     }
  116.     /**
  117.      * @throws InvalidArgumentException
  118.      */
  119.     public function getAllCachedTaxonomiesForLanguage(int $languageId)
  120.     {
  121.         if ($data $this->cacheAdapterService->existCache($languageId.'_'.self::CACHE_TAXONOMY_LANGUAGE_INDEXED)) {
  122.             return $data;
  123.         }
  124.         $allTaxonomiesByLanguage $this->taxonomyInfoManager->getTaxonomyInfoByLanguage($languageId);
  125.         return $this->cacheAdapterService->getCache($languageId.'_'.self::CACHE_TAXONOMY_LANGUAGE_INDEXED$allTaxonomiesByLanguage);
  126.     }
  127. }