src/Twig/PressSite/MultimediaExtension.php line 211

Open in your IDE?
  1. <?php
  2. namespace App\Twig\PressSite;
  3. use App\Contract\Media\InternalFileProviderInterface;
  4. use App\Contract\PressSite\Multimedia\MultimediaCollectorInterface;
  5. use App\Entity\AssetOnline;
  6. use App\Entity\AssetOnlineAudioLanguage;
  7. use App\Entity\AssetOnlineSubtitleLanguage;
  8. use App\Entity\Product;
  9. use App\Entity\ProductTVSeries;
  10. use App\Enum\Common\LanguageEnum;
  11. use App\Service\Helper\PressSite\MultimediaHelper;
  12. use App\Service\PressSite\Multimedia\MultimediaFactory;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Twig\Extension\AbstractExtension;
  15. use Twig\TwigFunction;
  16. /**
  17.  * Class MultimediaExtension.
  18.  *
  19.  * This extension provides helper methods that are only used
  20.  * in the PressSites in order to manage all asset and media
  21.  * related entries.
  22.  */
  23. class MultimediaExtension extends AbstractExtension
  24. {
  25.     /**
  26.      * The factory service responsible for creating a multimedia helper service.
  27.      *
  28.      * @var \App\Service\PressSite\Multimedia\MultimediaFactory
  29.      */
  30.     private $multimediaFactory;
  31.     /**
  32.      * The custom file provider.
  33.      *
  34.      * @var \App\Media\Provider\AssetFileProvider
  35.      */
  36.     private $assetFileProvider;
  37.     /**
  38.      * The instance of the collector service based on the site type.
  39.      *
  40.      * @var \App\Contract\PressSite\Multimedia\MultimediaCollectorInterface
  41.      */
  42.     private static $multimediaCollector;
  43.     public function __construct(MultimediaFactory $multimediaFactoryInternalFileProviderInterface $assetFileProvider)
  44.     {
  45.         $this->multimediaFactory $multimediaFactory;
  46.         $this->assetFileProvider $assetFileProvider;
  47.     }
  48.     public function getFunctions(): array
  49.     {
  50.         return [
  51.             new TwigFunction('getAssetResolution', [$this'getAssetResolution']),
  52.             new TwigFunction('getProductAssets', [$this'getProductAssets']),
  53.             new TwigFunction('getAssetPoster', [$this'getAssetPoster']),
  54.             new TwigFunction('getAssetFilesize', [$this'getAssetFilesize']),
  55.             new TwigFunction('getAssetOriginalUrl', [$this'getAssetOriginalUrl']),
  56.             new TwigFunction('isAssetSelected', [$this'isAssetSelected']),
  57.             new TwigFunction('getAssetAudioLanguages', [$this'getAssetAudioLanguages']),
  58.             new TwigFunction('getAssetSubtitleLanguages', [$this'getAssetSubtitleLanguages']),
  59.         ];
  60.     }
  61.     /**
  62.      * Returns the collection of product assets grouped by tab name.
  63.      *
  64.      * @param \App\Entity\Product $entity
  65.      *   The instance of the loaded product
  66.      *
  67.      * @return array
  68.      *   The collection of assets
  69.      */
  70.     public function getProductAssets(Product $entity): array
  71.     {
  72.         $seriesId = ($entity instanceof ProductTVSeries && !empty($entity->getSeries()))
  73.             ? $entity->getSeries()->getSeriesID()
  74.             : null;
  75.         return $this->getMultimediaCollector()->collectAssets(
  76.             $entity->getProductID(),
  77.             $seriesId
  78.         );
  79.     }
  80.     /**
  81.      * Responsible for returning the correct poster for the fancybox modal.
  82.      *
  83.      * We have a lot of asset content types with no generated thumbnail,
  84.      * so this helper method will take into consideration whether we operate
  85.      * on an asset with specific extension (one that have generated thumbnail)
  86.      * or not and return a default photo instead.
  87.      *
  88.      * @param \App\Entity\AssetOnline $entity
  89.      *   The instance of the loaded asset
  90.      *
  91.      * @return string
  92.      *   The url of the poster
  93.      */
  94.     public function getAssetPoster(AssetOnline $entity): string
  95.     {
  96.         if (!in_array($entity->getExtension(), ['jpg''jpeg''png'])) {
  97.             return '/assets/press_site/images/default-cover-image.jpg';
  98.         }
  99.         return $this->assetFileProvider->generatePublicUrl($entity->getFile(), 'default_pr_poster');
  100.     }
  101.     /**
  102.      * Returnn the url to the original asset.
  103.      *
  104.      * @param \App\Entity\AssetOnline $entity
  105.      *   The instance of the loaded asset
  106.      *
  107.      * @return string
  108.      *   The full url for the originally uploaded asset
  109.      */
  110.     public function getAssetOriginalUrl(AssetOnline $entity): string
  111.     {
  112.         return $this->assetFileProvider->generatePublicUrl($entity->getFile(), 'reference');
  113.     }
  114.     /**
  115.      * Returns the human readable formatted file size.
  116.      *
  117.      * @param \App\Entity\AssetOnline $entity
  118.      *   The instance of the loaded asset
  119.      *
  120.      * @return string|null
  121.      *   The formatted filesize
  122.      */
  123.     public function getAssetFilesize(AssetOnline $entity): ?string
  124.     {
  125.         $bytes $entity->getFile()->getSize();
  126.         if (>= $bytes) {
  127.             return null;
  128.         }
  129.         $i floor(log($bytes1024));
  130.         $units = ['B''kB''MB''GB''TB'];
  131.         $steps = [00223];
  132.         return round($bytes 1024 ** $i$steps[$i]) . $units[$i];
  133.     }
  134.     /**
  135.      * Returns comma delimited list of audio languages.
  136.      *
  137.      * @param \App\Entity\AssetOnline $entity
  138.      *   The instance of the loaded asset
  139.      *
  140.      * @return string|null
  141.      *   comma delimited string
  142.      */
  143.     public function getAssetAudioLanguages(AssetOnline $entity): ?string
  144.     {
  145.         $languages $entity->getAudioLanguages()->map(fn (AssetOnlineAudioLanguage $audioLanguage) => LanguageEnum::readableFor($audioLanguage->getLanguage()))->toArray();
  146.         return implode(', '$languages);
  147.     }
  148.     /**
  149.      * Returns comma delimited list of subtitle languages.
  150.      *
  151.      * @param \App\Entity\AssetOnline $entity
  152.      *   The instance of the loaded asset
  153.      *
  154.      * @return string|null
  155.      *   comma delimited string
  156.      */
  157.     public function getAssetSubtitleLanguages(AssetOnline $entity): ?string
  158.     {
  159.         $languages $entity->getSubtitleLanguages()->map(fn (AssetOnlineSubtitleLanguage $subLanguage) => LanguageEnum::readableFor($subLanguage->getLanguage()));
  160.         $languages $languages->toArray();
  161.         return implode(', '$languages);
  162.     }
  163.     /**
  164.      * Returns Asset Resolution Aspect.
  165.      *
  166.      * @return string|null
  167.      */
  168.     public function getAssetResolution(AssetOnline $entity): string
  169.     {
  170.         return $entity->getAssetResolutionAspect()->getName();
  171.     }
  172.     /**
  173.      * Indicates whether or not given asset is added to session storage for download.
  174.      *
  175.      * @param \Symfony\Component\HttpFoundation\Request $request
  176.      *   The current request object
  177.      * @param int $productId
  178.      *   The ID of the currently loaded product
  179.      * @param int $assetId
  180.      *   The ID of the currently processed asset
  181.      *
  182.      * @return bool
  183.      *   The check result
  184.      */
  185.     public function isAssetSelected(Request $requestint $productIdint $assetId): bool
  186.     {
  187.         $request->attributes->set('_product_id'$productId);
  188.         $data $request->getSession()->get(MultimediaHelper::createStorageKey($request));
  189.         return !empty($data) && !empty($data['items']) && isset($data['items'][$assetId]);
  190.     }
  191.     /**
  192.      * Returns an instance of the multimedia collector service based on the current site typ.
  193.      *
  194.      * @return \App\Contract\PressSite\Multimedia\MultimediaCollectorInterface
  195.      *   The instance of the class implementing this interface
  196.      */
  197.     protected function getMultimediaCollector(): MultimediaCollectorInterface
  198.     {
  199.         if (!static::$multimediaCollector instanceof MultimediaCollectorInterface) {
  200.             static::$multimediaCollector $this->multimediaFactory->createCollector();
  201.         }
  202.         return static::$multimediaCollector;
  203.     }
  204. }