vendor/sulu/sulu/src/Sulu/Bundle/MediaBundle/Api/Media.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\MediaBundle\Api;
  11. use JMS\Serializer\Annotation\ExclusionPolicy;
  12. use JMS\Serializer\Annotation\Groups;
  13. use JMS\Serializer\Annotation\SerializedName;
  14. use JMS\Serializer\Annotation\VirtualProperty;
  15. use Sulu\Bundle\AudienceTargetingBundle\Entity\TargetGroupInterface;
  16. use Sulu\Bundle\CategoryBundle\Api\Category;
  17. use Sulu\Bundle\CategoryBundle\Entity\CategoryInterface as CategoryEntity;
  18. use Sulu\Bundle\MediaBundle\Entity\File;
  19. use Sulu\Bundle\MediaBundle\Entity\FileVersion;
  20. use Sulu\Bundle\MediaBundle\Entity\FileVersionContentLanguage;
  21. use Sulu\Bundle\MediaBundle\Entity\FileVersionMeta;
  22. use Sulu\Bundle\MediaBundle\Entity\FileVersionPublishLanguage;
  23. use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
  24. use Sulu\Bundle\MediaBundle\Entity\MediaType;
  25. use Sulu\Bundle\MediaBundle\Media\Exception\FileNotFoundException;
  26. use Sulu\Bundle\MediaBundle\Media\Exception\FileVersionNotFoundException;
  27. use Sulu\Bundle\TagBundle\Tag\TagInterface;
  28. use Sulu\Component\Rest\ApiWrapper;
  29. use Sulu\Component\Security\Authentication\UserInterface;
  30. use Webmozart\Assert\Assert;
  31. /**
  32.  * Class Media
  33.  * The Media RestObject is the api entity for the MediaController.
  34.  *
  35.  * @ExclusionPolicy("all")
  36.  */
  37. class Media extends ApiWrapper
  38. {
  39.     /**
  40.      * @var string
  41.      */
  42.     public const MEDIA_TYPE_IMAGE 'image';
  43.     /**
  44.      * @var string
  45.      */
  46.     public const MEDIA_TYPE_VIDEO 'video';
  47.     /**
  48.      * @var string
  49.      */
  50.     public const MEDIA_TYPE_AUDIO 'audio';
  51.     /**
  52.      * @var string
  53.      */
  54.     public const MEDIA_TYPE_DOCUMENT 'document';
  55.     /**
  56.      * @var string
  57.      */
  58.     protected $url;
  59.     /**
  60.      * @var string|null
  61.      */
  62.     protected $adminUrl;
  63.     /**
  64.      * @var array
  65.      */
  66.     protected $formats = [];
  67.     /**
  68.      * @var string
  69.      */
  70.     protected $locale;
  71.     /**
  72.      * @var int
  73.      */
  74.     protected $version;
  75.     /**
  76.      * @var array
  77.      */
  78.     protected $additionalVersionData = [];
  79.     /**
  80.      * @var FileVersion
  81.      */
  82.     protected $fileVersion null;
  83.     /**
  84.      * @var FileVersionMeta
  85.      */
  86.     protected $localizedMeta null;
  87.     /**
  88.      * @var File
  89.      */
  90.     protected $file null;
  91.     public function __construct(MediaInterface $media$locale$version null)
  92.     {
  93.         $this->entity $media;
  94.         $this->locale $locale;
  95.         $this->version $version;
  96.     }
  97.     /**
  98.      * @VirtualProperty
  99.      * @SerializedName("id")
  100.      * @Groups({"partialMedia", "Default"})
  101.      *
  102.      * @return int
  103.      */
  104.     public function getId()
  105.     {
  106.         return $this->entity->getId();
  107.     }
  108.     /**
  109.      * @VirtualProperty
  110.      * @SerializedName("locale")
  111.      *
  112.      * @return string
  113.      */
  114.     public function getLocale()
  115.     {
  116.         return $this->locale;
  117.     }
  118.     /**
  119.      * @VirtualProperty
  120.      *
  121.      * @return string
  122.      */
  123.     public function getFallbackLocale()
  124.     {
  125.         if (!$this->getLocalizedMeta()) {
  126.             return;
  127.         }
  128.         $fallbackLocale $this->getLocalizedMeta()->getLocale();
  129.         return $fallbackLocale !== $this->locale $fallbackLocale null;
  130.     }
  131.     /**
  132.      * @param Collection $collection
  133.      *
  134.      * @return $this
  135.      */
  136.     public function setCollection($collection)
  137.     {
  138.         $this->entity->setCollection($collection);
  139.         return $this;
  140.     }
  141.     /**
  142.      * @VirtualProperty
  143.      * @SerializedName("collection")
  144.      *
  145.      * @return int
  146.      */
  147.     public function getCollection()
  148.     {
  149.         $collection $this->entity->getCollection();
  150.         if ($collection) {
  151.             return $collection->getId();
  152.         }
  153.         return;
  154.     }
  155.     /**
  156.      * @param int $size
  157.      *
  158.      * @return $this
  159.      */
  160.     public function setSize($size)
  161.     {
  162.         $this->getFileVersion()->setSize($size);
  163.         return $this;
  164.     }
  165.     /**
  166.      * @VirtualProperty
  167.      * @SerializedName("size")
  168.      *
  169.      * @return int
  170.      */
  171.     public function getSize()
  172.     {
  173.         return $this->getFileVersion()->getSize();
  174.     }
  175.     /**
  176.      * @param string $mimeType
  177.      *
  178.      * @return $this
  179.      */
  180.     public function setMimeType($mimeType)
  181.     {
  182.         $this->getFileVersion()->setMimeType($mimeType);
  183.         return $this;
  184.     }
  185.     /**
  186.      * @VirtualProperty
  187.      * @SerializedName("mimeType")
  188.      *
  189.      * @return string|null
  190.      */
  191.     public function getMimeType()
  192.     {
  193.         return $this->getFileVersion()->getMimeType();
  194.     }
  195.     /**
  196.      * @return string|null
  197.      */
  198.     public function getExtension()
  199.     {
  200.         return $this->getFileVersion()->getExtension();
  201.     }
  202.     /**
  203.      * @param string $title
  204.      *
  205.      * @return $this
  206.      */
  207.     public function setTitle($title)
  208.     {
  209.         $this->getMeta(true)->setTitle($title);
  210.         return $this;
  211.     }
  212.     /**
  213.      * @VirtualProperty
  214.      * @SerializedName("title")
  215.      *
  216.      * @return string|null
  217.      */
  218.     public function getTitle()
  219.     {
  220.         if (!$this->getLocalizedMeta()) {
  221.             return null;
  222.         }
  223.         return $this->getLocalizedMeta()->getTitle();
  224.     }
  225.     /**
  226.      * @param string|null $description
  227.      *
  228.      * @return $this
  229.      */
  230.     public function setDescription($description)
  231.     {
  232.         $this->getMeta(true)->setDescription($description);
  233.         return $this;
  234.     }
  235.     /**
  236.      * @VirtualProperty
  237.      * @SerializedName("description")
  238.      *
  239.      * @return string|null
  240.      */
  241.     public function getDescription()
  242.     {
  243.         if (!$this->getLocalizedMeta()) {
  244.             return null;
  245.         }
  246.         return $this->getLocalizedMeta()->getDescription();
  247.     }
  248.     /**
  249.      * @param string|null $copyright
  250.      *
  251.      * @return $this
  252.      */
  253.     public function setCopyright($copyright)
  254.     {
  255.         $this->getMeta(true)->setCopyright($copyright);
  256.         return $this;
  257.     }
  258.     /**
  259.      * Returns copyright for media.
  260.      *
  261.      * @VirtualProperty
  262.      * @SerializedName("copyright")
  263.      *
  264.      * @return string|null
  265.      *
  266.      * @throws FileVersionNotFoundException
  267.      */
  268.     public function getCopyright()
  269.     {
  270.         if (!$this->getLocalizedMeta()) {
  271.             return null;
  272.         }
  273.         return $this->getLocalizedMeta()->getCopyright();
  274.     }
  275.     /**
  276.      * @param string|null $credits
  277.      *
  278.      * @return $this
  279.      */
  280.     public function setCredits($credits)
  281.     {
  282.         $this->getMeta(true)->setCredits($credits);
  283.         return $this;
  284.     }
  285.     /**
  286.      * Returns copyright for media.
  287.      *
  288.      * @VirtualProperty
  289.      * @SerializedName("credits")
  290.      *
  291.      * @return string|null
  292.      *
  293.      * @throws FileVersionNotFoundException
  294.      */
  295.     public function getCredits()
  296.     {
  297.         if (!$this->getLocalizedMeta()) {
  298.             return null;
  299.         }
  300.         return $this->getLocalizedMeta()->getCredits();
  301.     }
  302.     /**
  303.      * @param int $version
  304.      *
  305.      * @return $this
  306.      */
  307.     public function setVersion($version)
  308.     {
  309.         $this->version $version;
  310.         return $this;
  311.     }
  312.     /**
  313.      * @VirtualProperty
  314.      * @SerializedName("version")
  315.      *
  316.      * @return int
  317.      */
  318.     public function getVersion()
  319.     {
  320.         return $this->getFileVersion()->getVersion();
  321.     }
  322.     /**
  323.      * @VirtualProperty
  324.      * @SerializedName("subVersion")
  325.      *
  326.      * @return int
  327.      */
  328.     public function getSubVersion()
  329.     {
  330.         return $this->getFileVersion()->getSubVersion();
  331.     }
  332.     /**
  333.      * @return array
  334.      */
  335.     public function getAdditionalVersionData()
  336.     {
  337.         return $this->additionalVersionData;
  338.     }
  339.     /**
  340.      * @param array $additionalVersionData
  341.      *
  342.      * @return $this
  343.      */
  344.     public function setAdditionalVersionData($additionalVersionData)
  345.     {
  346.         $this->additionalVersionData $additionalVersionData;
  347.         return $this;
  348.     }
  349.     /**
  350.      * @VirtualProperty
  351.      * @SerializedName("versions")
  352.      *
  353.      * @return array
  354.      */
  355.     public function getVersions()
  356.     {
  357.         $versions = [];
  358.         $file $this->getFile();
  359.         /** @var FileVersion $fileVersion */
  360.         foreach ($file->getFileVersions() as $fileVersion) {
  361.             $versionData = [];
  362.             if (isset($this->additionalVersionData[$fileVersion->getVersion()])) {
  363.                 $versionData $this->additionalVersionData[$fileVersion->getVersion()];
  364.             }
  365.             $versionData['version'] = $fileVersion->getVersion();
  366.             $versionData['name'] = $fileVersion->getName();
  367.             $versionData['created'] = $fileVersion->getCreated();
  368.             $versionData['changed'] = $fileVersion->getChanged();
  369.             $versionData['active'] = $fileVersion->isActive();
  370.             $versions[$fileVersion->getVersion()] = $versionData;
  371.         }
  372.         return $versions;
  373.     }
  374.     /**
  375.      * @param string $name
  376.      *
  377.      * @return $this
  378.      */
  379.     public function setName($name)
  380.     {
  381.         $this->getFileVersion()->setName($name);
  382.         return $this;
  383.     }
  384.     /**
  385.      * @VirtualProperty
  386.      * @SerializedName("name")
  387.      *
  388.      * @return string
  389.      */
  390.     public function getName()
  391.     {
  392.         return $this->getFileVersion()->getName();
  393.     }
  394.     /**
  395.      * @VirtualProperty
  396.      * @SerializedName("type")
  397.      *
  398.      * @return MediaType
  399.      */
  400.     public function getType()
  401.     {
  402.         return $this->entity->getType();
  403.     }
  404.     /**
  405.      * @param MediaType $type
  406.      *
  407.      * @return $this
  408.      */
  409.     public function setType($type)
  410.     {
  411.         $this->entity->setType($type);
  412.         return $this;
  413.     }
  414.     /**
  415.      * @param string $type
  416.      *
  417.      * @return bool
  418.      */
  419.     public function isTypeOf($type)
  420.     {
  421.         return $this->getType()->getName() == $type;
  422.     }
  423.     /**
  424.      * @VirtualProperty
  425.      * @SerializedName("isImage")
  426.      *
  427.      * @return bool
  428.      */
  429.     public function isImage()
  430.     {
  431.         return $this->isTypeOf(self::MEDIA_TYPE_IMAGE);
  432.     }
  433.     /**
  434.      * @VirtualProperty
  435.      * @SerializedName("isVideo")
  436.      *
  437.      * @return bool
  438.      */
  439.     public function isVideo()
  440.     {
  441.         return $this->isTypeOf(self::MEDIA_TYPE_VIDEO);
  442.     }
  443.     /**
  444.      * @VirtualProperty
  445.      * @SerializedName("isAudio")
  446.      *
  447.      * @return bool
  448.      */
  449.     public function isAudio()
  450.     {
  451.         return $this->isTypeOf(self::MEDIA_TYPE_AUDIO);
  452.     }
  453.     /**
  454.      * @VirtualProperty
  455.      * @SerializedName("isDocument")
  456.      *
  457.      * @return bool
  458.      */
  459.     public function isDocument()
  460.     {
  461.         return $this->isTypeOf(self::MEDIA_TYPE_DOCUMENT);
  462.     }
  463.     /**
  464.      * @VirtualProperty
  465.      * @SerializedName("storageOptions")
  466.      *
  467.      * @return array
  468.      */
  469.     public function getStorageOptions()
  470.     {
  471.         return $this->getFileVersion()->getStorageOptions();
  472.     }
  473.     /**
  474.      * @param array $storageOptions
  475.      *
  476.      * @return $this
  477.      */
  478.     public function setStorageOptions($storageOptions)
  479.     {
  480.         $this->getFileVersion()->setStorageOptions($storageOptions);
  481.         return $this;
  482.     }
  483.     /**
  484.      * @param array $publishLanguages
  485.      *
  486.      * @return $this
  487.      */
  488.     public function setPublishLanguages($publishLanguages)
  489.     {
  490.         $fileVersion $this->getFileVersion();
  491.         foreach ($publishLanguages as $locale) {
  492.             $publishLanguage = new FileVersionPublishLanguage();
  493.             $publishLanguage->setFileVersion($fileVersion);
  494.             $publishLanguage->setLocale($locale);
  495.             if (!$fileVersion->getPublishLanguages()->contains($publishLanguage)) {
  496.                 $fileVersion->addPublishLanguage($publishLanguage);
  497.             }
  498.         }
  499.         return $this;
  500.     }
  501.     /**
  502.      * @VirtualProperty
  503.      * @SerializedName("publishLanguages")
  504.      *
  505.      * @return array
  506.      */
  507.     public function getPublishLanguages()
  508.     {
  509.         $publishLanguages = [];
  510.         /** @var FileVersionPublishLanguage $publishLanguage */
  511.         foreach ($this->getFileVersion()->getPublishLanguages() as $publishLanguage) {
  512.             $publishLanguages[] = $publishLanguage->getLocale();
  513.         }
  514.         return $publishLanguages;
  515.     }
  516.     /**
  517.      * @param array $contentLanguages
  518.      *
  519.      * @return $this
  520.      */
  521.     public function setContentLanguages($contentLanguages)
  522.     {
  523.         $fileVersion $this->getFileVersion();
  524.         foreach ($contentLanguages as $locale) {
  525.             $contentLanguage = new FileVersionContentLanguage();
  526.             $contentLanguage->setFileVersion($fileVersion);
  527.             $contentLanguage->setLocale($locale);
  528.             if (!$fileVersion->getContentLanguages()->contains($contentLanguage)) {
  529.                 $fileVersion->addContentLanguage($contentLanguage);
  530.             }
  531.         }
  532.         return $this;
  533.     }
  534.     /**
  535.      * @VirtualProperty
  536.      * @SerializedName("contentLanguages")
  537.      *
  538.      * @return array
  539.      */
  540.     public function getContentLanguages()
  541.     {
  542.         $contentLanguages = [];
  543.         /** @var FileVersionContentLanguage $contentLanguage */
  544.         foreach ($this->getFileVersion()->getContentLanguages() as $contentLanguage) {
  545.             $contentLanguages[] = $contentLanguage->getLocale();
  546.         }
  547.         return $contentLanguages;
  548.     }
  549.     /**
  550.      * @return $this
  551.      */
  552.     public function removeTags()
  553.     {
  554.         $fileVersion $this->getFileVersion();
  555.         $fileVersion->removeTags();
  556.         return $this;
  557.     }
  558.     /**
  559.      * @return $this
  560.      */
  561.     public function addTag(TagInterface $tagEntity)
  562.     {
  563.         $fileVersion $this->getFileVersion();
  564.         if (!$fileVersion->getTags()->contains($tagEntity)) {
  565.             $fileVersion->addTag($tagEntity);
  566.         }
  567.         return $this;
  568.     }
  569.     /**
  570.      * @VirtualProperty
  571.      * @SerializedName("tags")
  572.      *
  573.      * @return string[]
  574.      */
  575.     public function getTags()
  576.     {
  577.         $tags = [];
  578.         foreach ($this->getFileVersion()->getTags() as $tag) {
  579.             /* @var TagInterface $tag */
  580.             \array_push($tags$tag->getName());
  581.         }
  582.         return $tags;
  583.     }
  584.     /**
  585.      * @SerializedName("thumbnails")
  586.      *
  587.      * @return array
  588.      */
  589.     public function getFormats()
  590.     {
  591.         return $this->formats;
  592.     }
  593.     /**
  594.      * @VirtualProperty
  595.      * @SerializedName("thumbnails")
  596.      *
  597.      * @return array
  598.      */
  599.     public function getThumbnails() // FIXME change to getPreviews when SerializedName working
  600.     {
  601.         return $this->formats;
  602.     }
  603.     /**
  604.      * @param array $formats
  605.      */
  606.     public function setFormats($formats)
  607.     {
  608.         $this->formats $formats;
  609.     }
  610.     /**
  611.      * @VirtualProperty
  612.      * @SerializedName("url")
  613.      *
  614.      * @return string
  615.      */
  616.     public function getUrl()
  617.     {
  618.         return $this->url;
  619.     }
  620.     /**
  621.      * @param string $url
  622.      */
  623.     public function setUrl($url)
  624.     {
  625.         $this->url $url;
  626.     }
  627.     /**
  628.      * @VirtualProperty
  629.      * @SerializedName("adminUrl")
  630.      *
  631.      * @return string
  632.      */
  633.     public function getAdminUrl()
  634.     {
  635.         // if the admin url is not set fallback to the website url for backward compatibility
  636.         if (!$this->adminUrl) {
  637.             return $this->url;
  638.         }
  639.         return $this->adminUrl;
  640.     }
  641.     /**
  642.      * @param string $adminUrl
  643.      */
  644.     public function setAdminUrl($adminUrl)
  645.     {
  646.         $this->adminUrl $adminUrl;
  647.     }
  648.     /**
  649.      * @param \DateTime|string $changed
  650.      *
  651.      * @return $this
  652.      */
  653.     public function setChanged($changed)
  654.     {
  655.         if (\is_string($changed)) {
  656.             $changed = new \DateTime($changed);
  657.         }
  658.         $this->getFileVersion()->setChanged($changed);
  659.         return $this;
  660.     }
  661.     /**
  662.      * @VirtualProperty
  663.      * @SerializedName("changed")
  664.      *
  665.      * @return \DateTime
  666.      */
  667.     public function getChanged()
  668.     {
  669.         return $this->getFileVersion()->getChanged();
  670.     }
  671.     /**
  672.      * @param UserInterface|null $changer
  673.      *
  674.      * @return $this
  675.      */
  676.     public function setChanger($changer)
  677.     {
  678.         $this->entity->setChanger($changer);
  679.         return $this;
  680.     }
  681.     /**
  682.      * @VirtualProperty
  683.      * @SerializedName("changer")
  684.      *
  685.      * @return string|null
  686.      */
  687.     public function getChanger()
  688.     {
  689.         $user $this->getFileVersion()->getChanger();
  690.         if ($user) {
  691.             return $user->getFullName();
  692.         }
  693.         return null;
  694.     }
  695.     /**
  696.      * @VirtualProperty
  697.      * @SerializedName("created")
  698.      *
  699.      * @return \DateTime
  700.      */
  701.     public function getCreated()
  702.     {
  703.         return $this->entity->getCreated();
  704.     }
  705.     /**
  706.      * @param UserInterface|null $creator
  707.      *
  708.      * @return $this
  709.      */
  710.     public function setCreator($creator)
  711.     {
  712.         $this->entity->setCreator($creator);
  713.         return $this;
  714.     }
  715.     /**
  716.      * @VirtualProperty
  717.      * @SerializedName("creator")
  718.      *
  719.      * @return string|null
  720.      */
  721.     public function getCreator()
  722.     {
  723.         $user $this->getFileVersion()->getCreator();
  724.         if ($user) {
  725.             return $user->getFullName();
  726.         }
  727.         return null;
  728.     }
  729.     /**
  730.      * @param array $properties
  731.      *
  732.      * @return $this
  733.      */
  734.     public function setProperties($properties)
  735.     {
  736.         $this->getFileVersion()->setProperties($properties);
  737.         return $this;
  738.     }
  739.     /**
  740.      * @VirtualProperty
  741.      * @SerializedName("properties")
  742.      *
  743.      * @return array|null
  744.      */
  745.     public function getProperties()
  746.     {
  747.         $properties $this->getFileVersion()->getProperties();
  748.         if (null === $properties) {
  749.             return null;
  750.         }
  751.         Assert::isArray($properties);
  752.         return $properties;
  753.     }
  754.     /**
  755.      * @VirtualProperty
  756.      * @SerializedName("downloadCounter")
  757.      *
  758.      * @return int
  759.      */
  760.     public function getDownloadCounter()
  761.     {
  762.         $downloadCounter 0;
  763.         foreach ($this->getEntity()->getFiles() as $file) {
  764.             /** @var FileVersion $fileVersion */
  765.             foreach ($file->getFileVersions() as $fileVersion) {
  766.                 $downloadCounter += \intval($fileVersion->getDownloadCounter());
  767.             }
  768.         }
  769.         return $downloadCounter;
  770.     }
  771.     /**
  772.      * @return FileVersion
  773.      *
  774.      * @throws FileVersionNotFoundException
  775.      */
  776.     public function getFileVersion()
  777.     {
  778.         if (null !== $this->fileVersion) {
  779.             return $this->fileVersion;
  780.         }
  781.         /** @var File $file */
  782.         foreach ($this->entity->getFiles() as $file) {
  783.             if (null !== $this->version) {
  784.                 $version $this->version;
  785.             } else {
  786.                 $version $file->getVersion();
  787.             }
  788.             if ($fileVersion $file->getFileVersion($version)) {
  789.                 $this->fileVersion $fileVersion;
  790.                 return $fileVersion;
  791.             }
  792.             break; // currently only one file per media exists
  793.         }
  794.         throw new FileVersionNotFoundException($this->entity->getId(), $this->version);
  795.     }
  796.     /**
  797.      * @return File
  798.      *
  799.      * @throws FileNotFoundException
  800.      */
  801.     public function getFile()
  802.     {
  803.         if (null !== $this->file) {
  804.             return $this->file;
  805.         }
  806.         /** @var File $file */
  807.         foreach ($this->entity->getFiles() as $file) {
  808.             // currently only one file per media exists
  809.             $this->file $file;
  810.             return $this->file;
  811.         }
  812.         throw new FileNotFoundException($this->entity->getId());
  813.     }
  814.     /**
  815.      * @param bool $create
  816.      *
  817.      * @return FileVersionMeta
  818.      */
  819.     private function getMeta($create false)
  820.     {
  821.         $locale $this->locale;
  822.         $metaCollection $this->getFileVersion()->getMeta();
  823.         // get meta only with this locale
  824.         $metaCollectionFiltered $metaCollection->filter(function($meta) use ($locale) {
  825.             /** @var FileVersionMeta $meta */
  826.             if ($meta->getLocale() == $locale) {
  827.                 return true;
  828.             }
  829.             return false;
  830.         });
  831.         // check if meta was found
  832.         if ($metaCollectionFiltered->isEmpty()) {
  833.             if ($create) {
  834.                 // create when not found
  835.                 $meta = new FileVersionMeta();
  836.                 $meta->setLocale($this->locale);
  837.                 $meta->setFileVersion($this->getFileVersion());
  838.                 $this->getFileVersion()->addMeta($meta);
  839.                 return $meta;
  840.             }
  841.             // return first when create false
  842.             return $this->getFileVersion()->getDefaultMeta();
  843.         }
  844.         // return exists
  845.         return $metaCollectionFiltered->first();
  846.     }
  847.     /**
  848.      * Searches the meta for the file version in the media locale. Might also return a fallback.
  849.      *
  850.      * @return FileVersionMeta
  851.      *
  852.      * @throws FileVersionNotFoundException
  853.      */
  854.     private function getLocalizedMeta()
  855.     {
  856.         if ($this->localizedMeta) {
  857.             return $this->localizedMeta;
  858.         }
  859.         $this->localizedMeta $this->getFileVersion()->getDefaultMeta();
  860.         foreach ($this->getFileVersion()->getMeta() as $meta) {
  861.             if ($meta->getLocale() == $this->locale) {
  862.                 $this->localizedMeta $meta;
  863.                 break;
  864.             }
  865.         }
  866.         return $this->localizedMeta;
  867.     }
  868.     /**
  869.      * Adds a category to the entity.
  870.      */
  871.     public function addCategory(CategoryEntity $category)
  872.     {
  873.         $fileVersion $this->getFileVersion();
  874.         $fileVersion->addCategory($category);
  875.         return $this;
  876.     }
  877.     /**
  878.      * Removes all category from the entity.
  879.      *
  880.      * @return self
  881.      */
  882.     public function removeCategories()
  883.     {
  884.         $fileVersion $this->getFileVersion();
  885.         $fileVersion->removeCategories();
  886.         return $this;
  887.     }
  888.     /**
  889.      * Returns the categories of the media.
  890.      *
  891.      * @VirtualProperty
  892.      * @SerializedName("categories")
  893.      *
  894.      * @return int[]
  895.      */
  896.     public function getCategories()
  897.     {
  898.         $apiCategories = [];
  899.         $fileVersion $this->getFileVersion();
  900.         $categories $fileVersion->getCategories();
  901.         return \array_map(function(CategoryEntity $category) {
  902.             return $category->getId();
  903.         }, $categories->toArray());
  904.     }
  905.     /**
  906.      * Adds a target group to the entity.
  907.      *
  908.      * @return self
  909.      */
  910.     public function addTargetGroup(TargetGroupInterface $targetGroup)
  911.     {
  912.         $fileVersion $this->getFileVersion();
  913.         $fileVersion->addTargetGroup($targetGroup);
  914.         return $this;
  915.     }
  916.     /**
  917.      * Removes all target groups from the entities.
  918.      *
  919.      * @return self
  920.      */
  921.     public function removeTargetGroups()
  922.     {
  923.         $fileVersion $this->getFileVersion();
  924.         $fileVersion->removeTargetGroups();
  925.         return $this;
  926.     }
  927.     /**
  928.      * Returns the target groups of the media.
  929.      *
  930.      * @VirtualProperty
  931.      * @SerializedName("targetGroups")
  932.      * @Groups({"fullMediaAudienceTargeting"})
  933.      *
  934.      * @return int[]
  935.      */
  936.     public function getTargetGroups()
  937.     {
  938.         if (!$this->getFileVersion()->getTargetGroups()) {
  939.             return [];
  940.         }
  941.         return \array_map(function(TargetGroupInterface $targetGroup) {
  942.             return $targetGroup->getId();
  943.         }, $this->getFileVersion()->getTargetGroups()->toArray());
  944.     }
  945.     /**
  946.      * Returns the x coordinate of the focus point.
  947.      *
  948.      * @VirtualProperty
  949.      * @SerializedName("focusPointX")
  950.      *
  951.      * @return int|null
  952.      */
  953.     public function getFocusPointX()
  954.     {
  955.         return $this->getFileVersion()->getFocusPointX();
  956.     }
  957.     /**
  958.      * Sets the x coordinate of the focus point.
  959.      *
  960.      * @param int $focusPointX
  961.      */
  962.     public function setFocusPointX($focusPointX)
  963.     {
  964.         $this->getFileVersion()->setFocusPointX($focusPointX);
  965.     }
  966.     /**
  967.      * Returns the y coordinate of the focus point.
  968.      *
  969.      * @VirtualProperty
  970.      * @SerializedName("focusPointY")
  971.      *
  972.      * @return int|null
  973.      */
  974.     public function getFocusPointY()
  975.     {
  976.         return $this->getFileVersion()->getFocusPointY();
  977.     }
  978.     /**
  979.      * Sets the y coordinate of the focus point.
  980.      *
  981.      * @param int $focusPointY
  982.      */
  983.     public function setFocusPointY($focusPointY)
  984.     {
  985.         $this->getFileVersion()->setFocusPointY($focusPointY);
  986.     }
  987.     /**
  988.      * @VirtualProperty
  989.      * @SerializedName("previewImageId")
  990.      *
  991.      * @return ?int
  992.      */
  993.     public function getPreviewImageId()
  994.     {
  995.         $previewImage $this->entity->getPreviewImage();
  996.         if (!$previewImage) {
  997.             return null;
  998.         }
  999.         return $previewImage->getId();
  1000.     }
  1001. }