src/Application/Service/Taxonomy/TaxonomyHomeService.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Application\Service\Taxonomy;
  3. use Psr\Cache\InvalidArgumentException;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Contracts\Translation\TranslatorInterface;
  6. class TaxonomyHomeService
  7. {
  8.     public const NUM_MAX_CHILDS 4;
  9.     public const KEY_TEXT_PLURAL 'taxonomy.num.categories.more';
  10.     public const KEY_TEXT_SINGULAR 'taxonomy.num.category.more';
  11.     public const ONLY_ONE_CHILDREN 1;
  12.     /** @var TaxonomyService */
  13.     private $taxonomyService;
  14.     /** @var TranslatorInterface */
  15.     private $translator;
  16.     /** @var RequestStack */
  17.     private $requestStack;
  18.     public function __construct(
  19.         TaxonomyService $taxonomyService,
  20.         TranslatorInterface $translator,
  21.         RequestStack $requestStack
  22.     ) {
  23.         $this->taxonomyService $taxonomyService;
  24.         $this->translator $translator;
  25.         $this->requestStack $requestStack;
  26.     }
  27.     /**
  28.      * @return array
  29.      *
  30.      * @throws InvalidArgumentException
  31.      */
  32.     public function getTaxonomyComponentLanding(string $isoCode null): array
  33.     {
  34.         if (empty($isoCode)) {
  35.             $isoCode $this->requestStack->getSession()->get('lang');
  36.         }
  37.         $taxonomies $this->taxonomyService->getTaxonomyChilds();
  38.         return $this->getTaxonomies($taxonomies$isoCode);
  39.     }
  40.     /**
  41.      * @param array $taxonomies
  42.      * @param string $isoCode
  43.      *
  44.      * @return array
  45.      */
  46.     private function getTaxonomies(array $taxonomiesstring $isoCode): array
  47.     {
  48.         $result = [];
  49.         $count 0;
  50.         $numChildren 0;
  51.         $numCategoriesMore = [];
  52.         foreach ($taxonomies as $taxonomy) {
  53.             $childs = [];
  54.             [$numChildren$childs] = $this->getChildren($taxonomy['childs'], $numChildren$count$childs);
  55.             $numCategoriesMore $this->getNumCategoriesMore($numChildren$taxonomy$isoCode);
  56.             $result $this->setResult($taxonomy$childs$numCategoriesMore$result);
  57.             $count 0;
  58.         }
  59.         return $result;
  60.     }
  61.     /**
  62.      * @param int $numChildren
  63.      * @param array $taxonomy
  64.      * @param string $isoCode
  65.      *
  66.      * @return array
  67.      */
  68.     private function getNumCategoriesMore(int $numChildren, array $taxonomystring $isoCode): array
  69.     {
  70.         $numElementsNotVisible = ($numChildren self::NUM_MAX_CHILDS) ? ($numChildren self::NUM_MAX_CHILDS) : 0;
  71.         if ($numElementsNotVisible === self::ONLY_ONE_CHILDREN) {
  72.             $numCategoriesMore['taxonomyMore'] = $this->getData($this->translator->trans(self::KEY_TEXT_SINGULAR, ['%num_categories%' => $numElementsNotVisible], null$isoCode), $taxonomy['link']);
  73.             return $numCategoriesMore;
  74.         }
  75.         if ($numElementsNotVisible self::ONLY_ONE_CHILDREN) {
  76.             $numCategoriesMore['taxonomyMore'] = $this->getData($this->translator->trans(self::KEY_TEXT_PLURAL, ['%num_categories%' => $numElementsNotVisible], null$isoCode), $taxonomy['link']);
  77.             return $numCategoriesMore;
  78.         }
  79.         $numCategoriesMore['taxonomyMore'] = $this->getData(nullnull);
  80.         return $numCategoriesMore;
  81.     }
  82.     /**
  83.      * @param string|null $text
  84.      * @param string|null $link
  85.      *
  86.      * @return null[]|string[]
  87.      */
  88.     private function getData(?string $text, ?string $link): array
  89.     {
  90.         return [
  91.             'text' => $text,
  92.             'link' => $link,
  93.         ];
  94.     }
  95.     /**
  96.      * @param $taxonomyChilds
  97.      * @param int $numChildren
  98.      * @param int $count
  99.      * @param array $childs
  100.      *
  101.      * @return array
  102.      */
  103.     private function getChildren(
  104.         $taxonomyChilds,
  105.         int $numChildren,
  106.         int $count,
  107.         array $childs
  108.     ): array {
  109.         if (is_array($taxonomyChilds)) {
  110.             $numChildren count($taxonomyChilds);
  111.             foreach ($taxonomyChilds as $child) {
  112.                 if ($count self::NUM_MAX_CHILDS) {
  113.                     $childs[] = $child;
  114.                     $count++;
  115.                 }
  116.             }
  117.         }
  118.         return [$numChildren$childs];
  119.     }
  120.     /**
  121.      * @param $taxonomy
  122.      * @param $childs
  123.      * @param array $numCategoriesMore
  124.      * @param array $result
  125.      *
  126.      * @return array
  127.      */
  128.     private function setResult(
  129.         $taxonomy,
  130.         $childs,
  131.         array $numCategoriesMore,
  132.         array $result
  133.     ): array {
  134.         $result[] = [
  135.             'taxonomy' => $taxonomy['name'],
  136.             'taxonomyId' => $taxonomy['id'],
  137.             'link' => $taxonomy['link'],
  138.             'childs' => $childs,
  139.             'numCategoriesMore' => $numCategoriesMore,
  140.         ];
  141.         return $result;
  142.     }
  143. }