src/Controller/Tag/IndexController.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: ThanhDT
  5.  * Date: 12/4/2018
  6.  * Time: 9:18 AM
  7.  */
  8. namespace App\Controller\Tag;
  9. use App\Controller\BaseController;
  10. use App\Entity\PostPublishes;
  11. use App\Entity\GroupBoxItems;
  12. use App\Entity\Tags;
  13. use App\Service\DataExchange;
  14. use App\Service\Category;
  15. use App\Service\CryptUtils;
  16. use App\Service\ElasticSearch;
  17. use App\Utils\Constants;
  18. use App\Utils\Lib;
  19. use Symfony\Component\HttpFoundation\Request;
  20. class IndexController extends BaseController
  21. {
  22.     /**
  23.      * tag detail action
  24.      *
  25.      * @param string $slug The slug
  26.      * @param DataExchange $exchangeService The exchange service
  27.      * @param Category $category The category
  28.      * @param CryptUtils $cryptUtils The crypt utilities
  29.      * @param Elasticsearch $elasticSearch The elastic search
  30.      *
  31.      * @return     \Symfony\Component\HttpFoundation\Response
  32.      */
  33.     public function detailAction(
  34.         $slug,
  35.         Request $request,
  36.         DataExchange $exchangeService,
  37.         CryptUtils $cryptUtils,
  38.         Elasticsearch $elasticSearch
  39.     )
  40.     {
  41.         $cacheService $this->getCacheProvider(Constants::SERVER_CACHE_ARTICLE);
  42.         $tagData $this->getTagDataBySlug($cacheService$exchangeService$slug);
  43.         if (!$tagData) {
  44.             throw $this->createNotFoundException('The requested tag could not be found');
  45. //            return $this->createNotFoundWithContent($slug, $elasticSearch, $exchangeService, $cryptUtils);
  46.         }
  47.         $ignoreIds = [];
  48.         $limit Constants::SEARCH_POST_LIMIT;
  49.         list($posts$loadMoreParams) = $this->getTagPosts(
  50.             $cacheService,
  51.             $exchangeService,
  52.             $cryptUtils,
  53.             $tagData['tagId'],
  54.             0,
  55.             ($limit 1),
  56.             0,
  57.             0,
  58.             $ignoreIds
  59.         );
  60.         $mostViewPosts $this->getMostViewPosts($cacheService$exchangeService);
  61.         $seo $tagData['seo'];
  62.         $seo['title'] = "Tag - " $slug;
  63.         $viewParams = [
  64.             'tag' => $tagData,
  65.             'seo' => $seo,
  66.             'posts' => $posts,
  67.             'loadMoreParams' => $loadMoreParams,
  68.             'cateId' => 0,
  69.             'cateSlug' => 'blog',
  70.             'mostViewPosts' => $mostViewPosts,
  71.             'totalPosts' => $tagData['postCount'],
  72.             'keyword' => $slug,
  73.             'searchType' => 'Tag',
  74.         ];
  75.     
  76.         return $this->renderHtml('search/tag.html.twig'$viewParams);
  77.     }//end detailAction()
  78.     /**
  79.      * Gets the tag posts.
  80.      *
  81.      * @param mixed $cacheService The cache service
  82.      * @param DataExchange $exchangeService The exchange service
  83.      * @param int $tagId The tag identifier
  84.      * @param int $limit The limit
  85.      * @param int $timeStamp The time stamp
  86.      * @param int $lastId The last identifier
  87.      * @param array $ignoreIds The ignore identifiers
  88.      *
  89.      * @return     array         The tag posts.
  90.      */
  91.     protected function getTagPosts(
  92.         $cacheService,
  93.         $exchangeService,
  94.         CryptUtils $cryptUtils,
  95.         $tagId,
  96.         $page,
  97.         $limit,
  98.         $timeStamp,
  99.         $lastId,
  100.         $ignoreIds
  101.     )
  102.     {
  103.         $cacheKey sprintf(Constants::CACHE_TAG_POST$tagId$timeStamp$lastId$limit);
  104.         if (($cacheData $cacheService->get($cacheKey)) === false) {
  105.             $em $this->getDoctrine()->getManager();
  106.             $posts $em->getRepository(PostPublishes::class)->getPostsByTagId($tagId$limit$timeStamp$lastId$ignoreIds);
  107.             $postsData = [];
  108.             if ($posts) {
  109.                 foreach ($posts as $post) {
  110.                     $postsData[] = $exchangeService->exchangeRowPost(
  111.                         $post,
  112.                         Constants::LIST_RESULT_POSTS_IMAGE_SIZE,
  113.                         null,
  114.                         null,
  115.                         Constants::LIST_RESULT_POSTS_IMAGE_MOBILE_SIZE
  116.                     );
  117.                     array_push($ignoreIds$post['postId']);
  118.                 }
  119.                 $loadMoreParams $this->getLoadMoreParams($cryptUtils$tagId$postsData$page, ($limit 1), $ignoreIds);
  120.                 $postsData Lib::groupItemByLength($postsDataConstants::FIRST_LIMIT_POST_LIST);
  121.                 $cacheData = [$postsData$loadMoreParams];
  122.             } else {
  123.                 $cacheData = [falsefalse];
  124.             }//end if
  125.             $cacheService->set($cacheKey$cacheData$this->getParameter('cache_time')['hour']);
  126.         }//end if
  127.         return $cacheData;
  128.     }//end getTagPosts()
  129.     /**
  130.      * Gets the load more parameters.
  131.      *
  132.      * @param CryptUtils $cryptUtils The crypt utilities
  133.      * @param int $tagId The tag identifier
  134.      * @param array $posts The posts
  135.      * @param int $page The page offset
  136.      * @param int $limit The limit
  137.      * @param array $ignoreIds The ignore identifiers
  138.      *
  139.      * @return     array|bool  The load more parameters.
  140.      */
  141.     protected function getLoadMoreParams(CryptUtils $cryptUtils$tagId, &$posts$page$limit$ignoreIds)
  142.     {
  143.         $loadMoreParams false;
  144.         if ($posts && count($posts) > $limit) {
  145.             array_pop($posts);
  146.             array_pop($ignoreIds);
  147.             $lastPost end($posts);
  148.             $loadMoreToken = [
  149.                 'tagId' => $tagId,
  150.                 'lastId' => $lastPost['id'],
  151.                 'lastTimestamp' => $lastPost['publishedTimestamp'],
  152.                 'page' => $page 1,
  153.                 'ignoreIds' => $ignoreIds
  154.             ];
  155.             $loadMoreToken $cryptUtils->encrypt(json_encode($loadMoreToken));
  156.             $loadMoreParams = [
  157.                 'url' => $this->generateUrl('ajax_get_tag_posts'),
  158.                 'token' => $loadMoreToken,
  159.                 'fullUrl' => $this->getParameter('domain') . $this->generateUrl(
  160.                         "ajax_get_tag_posts",
  161.                         [
  162.                             'token' => $loadMoreToken,
  163.                             'is_amp' => 1
  164.                         ]
  165.                     ),
  166.             ];
  167.             reset($posts);
  168.         }//end if
  169.         return $loadMoreParams;
  170.     }//end getLoadMoreParams()
  171.     /**
  172.      * Gets the tag data by slug.
  173.      *
  174.      * @param mixed $cacheService The cache service
  175.      * @param DataExchange $exchangeService The exchange service
  176.      * @param string $slug The tag slug
  177.      *
  178.      * @return     array|bool    The tag data by slug.
  179.      */
  180.     public function getTagDataBySlug($cacheService$exchangeService$slug)
  181.     {
  182.         $cacheKey sprintf(Constants::CACHE_TAG_DETAIL$slug);
  183.         if (($tagData $cacheService->get($cacheKey)) === false) {
  184.             $em $this->getDoctrine()->getManager();
  185.             $tag $em->getRepository(Tags::class)->getTagBySlug($slug);
  186.             if ($tag) {
  187.                 $tagData $exchangeService->exchangeTagDetail($tag);
  188.                 $cacheService->set($cacheKey$tagData$this->getParameter('cache_time')['hour']);
  189.             }
  190.         }
  191.         return $tagData;
  192.     }//end getTagDataBySlug()
  193. }//end class