src/Hitso/Bundle/ContentBundle/Twig/Extension/ContentExtension.php line 127

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\ContentBundle\Twig\Extension;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\Common\Collections\Criteria;
  7. use Hitso\Bundle\CommonBundle\Doctrine\Paginator\DoctrineORMAdapter;
  8. use Hitso\Bundle\ContentBundle\Entity\Article;
  9. use Hitso\Bundle\ContentBundle\Entity\ArticleCategory;
  10. use Hitso\Bundle\ContentBundle\Entity\Banner;
  11. use Hitso\Bundle\ContentBundle\Entity\Category;
  12. use Hitso\Bundle\ContentBundle\Entity\Content;
  13. use Hitso\Bundle\ContentBundle\Entity\Elements\Element;
  14. use Hitso\Bundle\ContentBundle\Entity\Slide;
  15. use Hitso\Bundle\ContentBundle\Services\Video\VideoHelper;
  16. use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
  17. use Pagerfanta\Pagerfanta;
  18. use Symfony\Bridge\Doctrine\RegistryInterface;
  19. use Symfony\Component\Cache\Adapter\AdapterInterface;
  20. use Twig\Extension\AbstractExtension;
  21. use Twig\TwigFilter;
  22. use Twig\TwigFunction;
  23. class ContentExtension extends AbstractExtension
  24. {
  25.     /**
  26.      * @var AdapterInterface
  27.      */
  28.     private $cache;
  29.     /**
  30.      * @var RegistryInterface
  31.      */
  32.     protected $doctrine;
  33.     /**
  34.      * @var SiteContext
  35.      */
  36.     protected $context;
  37.     /**
  38.      * @var VideoHelper
  39.      */
  40.     protected $videoHelper;
  41.     /**
  42.      * @var SiteContext
  43.      */
  44.     protected $siteContext;
  45.     public function __construct(
  46.         RegistryInterface $doctrine,
  47.         AdapterInterface $cache,
  48.         SiteContext $context,
  49.         VideoHelper $videoHelper,
  50.         SiteContext $siteContext
  51.     ) {
  52.         $this->doctrine    $doctrine;
  53.         $this->cache       $cache;
  54.         $this->context     $context;
  55.         $this->videoHelper $videoHelper;
  56.         $this->siteContext $siteContext;
  57.     }
  58.     public function getFunctions()
  59.     {
  60.         return [
  61.             new TwigFunction('hitso_content_slide', [$this'getSlides'], ['is_safe' => ['html']]),
  62.             new TwigFunction('related_articles', [$this'getRelatedArticles'], ['is_safe' => ['html']]),
  63.             new TwigFunction('hitso_content_banner', [$this'getBanners'], ['is_safe' => ['html']]),
  64.             new TwigFunction('article_categories', [$this'getArticleCategories'], ['is_safe' => ['html']]),
  65.             new TwigFunction('category_articles', [$this'getArticlesByCategory'], ['is_safe' => ['html']]),
  66.             new TwigFunction('published_articles', [$this'getPublishedArticles'], ['is_safe' => ['html']]),
  67.             new TwigFunction('featured_articles', [$this'getFeaturedArticles'], ['is_safe' => ['html']]),
  68.             new TwigFunction('extract_content', [$this'extractContent'], ['is_safe' => ['html']]),
  69.             new TwigFunction('video_thumbnail_url', [$this'getVideoThumbnailUrl'], ['is_safe' => ['html']]),
  70.             new TwigFunction('getYoutubeMovieId', [$this'getYoutubeMovieId'], ['is_safe' => ['html']]),
  71.             new TwigFunction('getYoutubeMovieUrl', [$this'getYoutubeMovieUrl'], ['is_safe' => ['html']]),
  72.             new TwigFunction('formatBytes', [$this'formatBytes'], ['is_safe' => ['html']]),
  73.         ];
  74.     }
  75.     /**
  76.      * @return array
  77.      */
  78.     public function getFilters()
  79.     {
  80.         return [
  81.             new TwigFilter('file_size_format', [$this'fileSizeFormat']),
  82.         ];
  83.     }
  84.     public function getName()
  85.     {
  86.         return 'hitso_content_ext';
  87.     }
  88.     public function getVideoThumbnailUrl(?string $videoUrl): string
  89.     {
  90.         if (null !== $videoUrl) {
  91.             return $this->videoHelper->getThumbnailUrl($videoUrl);
  92.         }
  93.         return '';
  94.     }
  95.     public function extractContent(Content $content): string
  96.     {
  97.         $html = [];
  98.         $content->getElements()->map(function (Element $element) use (&$html) {
  99.             if (strlen($element->getSearchableContent())) {
  100.                 $html[] = $element->getSearchableContent();
  101.             }
  102.         });
  103.         return implode('.'$html);
  104.     }
  105.     public function getPublishedArticles(int $limit)
  106.     {
  107.         $repository   $this->doctrine->getRepository(Article::class);
  108.         $queryBuilder $repository->getPublishedContentsQuery();
  109.         $queryBuilder->setMaxResults($limit);
  110.         return $queryBuilder->getQuery()->getResult();
  111.     }
  112.     public function getFeaturedArticles(int $limit 4)
  113.     {
  114.         $repository   $this->doctrine->getRepository(Article::class);
  115.         $queryBuilder $repository->getPublishedContentsQuery();
  116.         $queryBuilder->setMaxResults($limit);
  117.         return $queryBuilder->getQuery()->getResult();
  118.     }
  119.     public function getArticlesByCategory(ArticleCategory $articleCategoryint $page 1int $limit 12)
  120.     {
  121.         $qb      $this->doctrine->getRepository(Article::class)->getCategoryQuery($articleCategory);
  122.         $cacheId 'article_category_' $articleCategory->getId();
  123.         $adapter = new DoctrineORMAdapter($qb$cacheId);
  124.         return (new Pagerfanta($adapter))
  125.             ->setMaxPerPage($limit)
  126.             ->setCurrentPage($page);
  127.     }
  128.     public function getRelatedArticles(Article $articleint $limit 4)
  129.     {
  130.         if (!== (int) $article->getId()) {
  131.             $qb      $this->doctrine->getRepository(Article::class)->getRelatedQuery($article);
  132.             $cacheId 'article_related_' $article->getId() . '_' $this->siteContext->getContentSite()->getId();
  133.             $adapter = new DoctrineORMAdapter($qb$cacheId);
  134.             return (new Pagerfanta($adapter))
  135.                 ->setMaxPerPage($limit)
  136.                 ->setCurrentPage(1);
  137.         }
  138.         return new ArrayCollection();
  139.     }
  140.     public function getArticleCategories(): Collection
  141.     {
  142.         $repository $this->doctrine->getRepository(Category::class);
  143.         $criteria   = new Criteria();
  144.         return $repository->matching($criteria);
  145.     }
  146.     public function getSlides($ident$baseUrl '')
  147.     {
  148.         $cacheKey  'content_slides_' $this->context->getContentSite()->getId() . sha1($ident);
  149.         $cacheItem $this->cache->getItem($cacheKey);
  150.         if ($cacheItem->isHit()) {
  151.             return $cacheItem->get();
  152.         }
  153.         $repository $this->doctrine->getRepository(Slide::class);
  154.         $slides $repository->getActiveSlides($ident);
  155.         $cacheItem->set($slides);
  156.         $this->cache->save($cacheItem);
  157.         return $slides;
  158.     }
  159.     public function getBanners(string $target)
  160.     {
  161.         $cacheKey  'content_banners_' $this->context->getContentSite()->getId() . sha1($target);
  162.         $cacheItem $this->cache->getItem($cacheKey);
  163.         if ($cacheItem->isHit()) {
  164.             return $cacheItem->get();
  165.         }
  166.         $repository $this->doctrine->getRepository(Banner::class);
  167.         $banners    $repository->getActiveBanners($target);
  168.         $cacheItem->set($banners);
  169.         $this->cache->save($cacheItem);
  170.         return $banners;
  171.     }
  172.     /**
  173.      * @param string $videoUrl
  174.      *
  175.      * @return string
  176.      * @throws \Hitso\Bundle\CommonBundle\Exception\Exception
  177.      */
  178.     public function getYoutubeMovieId(string $videoUrl): string
  179.     {
  180.         return $this->videoHelper->getYoutubeMovieId($videoUrl);
  181.     }
  182.     /**
  183.      * @param string $videoUrl
  184.      *
  185.      * @return string
  186.      */
  187.     public function getYoutubeMovieUrl(string $videoUrl): string
  188.     {
  189.         return $this->videoHelper->getYoutubeMovieUrl($videoUrl);
  190.     }
  191.     /**
  192.      * @param        $size
  193.      * @param int    $precision
  194.      * @param string $space
  195.      *
  196.      * @return string
  197.      */
  198.     public function fileSizeFormat($size$precision 2$space ' ')
  199.     {
  200.         if ($size <= 0) {
  201.             return '0' $space 'KB';
  202.         }
  203.         if ($size === 1) {
  204.             return '1' $space 'byte';
  205.         }
  206.         $mod   1024;
  207.         $units = ['b''KB''MB''GB''TB''PB'];
  208.         for ($i 0$size $mod && $i count($units) - 1; ++$i) {
  209.             $size /= $mod;
  210.         }
  211.         return round($size$precision) . $space $units[$i];
  212.     }
  213. }