vendor/sulu/sulu/src/Sulu/Bundle/MediaBundle/Entity/FileVersion.php line 27

  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\Entity;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection as DoctrineCollection;
  13. use JMS\Serializer\Annotation\Exclude;
  14. use Sulu\Bundle\AudienceTargetingBundle\Entity\TargetGroupInterface;
  15. use Sulu\Bundle\CategoryBundle\Entity\CategoryInterface;
  16. use Sulu\Bundle\TagBundle\Tag\TagInterface;
  17. use Sulu\Component\Persistence\Model\AuditableInterface;
  18. use Sulu\Component\Persistence\Model\AuditableTrait;
  19. use Symfony\Component\Mime\MimeTypes;
  20. /**
  21.  * FileVersion.
  22.  */
  23. class FileVersion implements AuditableInterface
  24. {
  25.     use AuditableTrait;
  26.     /**
  27.      * @var string
  28.      */
  29.     private $name;
  30.     /**
  31.      * @var int
  32.      */
  33.     private $version;
  34.     /**
  35.      * @var int
  36.      */
  37.     private $subVersion 0;
  38.     /**
  39.      * @var int
  40.      */
  41.     private $size;
  42.     /**
  43.      * @var string|null
  44.      */
  45.     private $mimeType;
  46.     /**
  47.      * @var string|null
  48.      */
  49.     private $storageOptions;
  50.     /**
  51.      * @var int
  52.      */
  53.     private $downloadCounter 0;
  54.     /**
  55.      * @var int
  56.      */
  57.     private $id;
  58.     /**
  59.      * @var DoctrineCollection<int, FileVersionContentLanguage>
  60.      */
  61.     private $contentLanguages;
  62.     /**
  63.      * @var DoctrineCollection<int, FileVersionPublishLanguage>
  64.      */
  65.     private $publishLanguages;
  66.     /**
  67.      * @var DoctrineCollection<int, FileVersionMeta>
  68.      */
  69.     private $meta;
  70.     /**
  71.      * @var DoctrineCollection<string, FormatOptions>
  72.      */
  73.     private $formatOptions;
  74.     /**
  75.      * @var File
  76.      *
  77.      * @Exclude
  78.      */
  79.     private $file;
  80.     /**
  81.      * @var DoctrineCollection<int, TagInterface>
  82.      */
  83.     private $tags;
  84.     /**
  85.      * @var FileVersionMeta
  86.      */
  87.     private $defaultMeta;
  88.     /**
  89.      * @var string|null
  90.      */
  91.     private $properties '{}';
  92.     /**
  93.      * @var DoctrineCollection<int, CategoryInterface>
  94.      */
  95.     private $categories;
  96.     /**
  97.      * @var DoctrineCollection<int, TargetGroupInterface>
  98.      */
  99.     private $targetGroups;
  100.     /**
  101.      * @var int|null
  102.      */
  103.     private $focusPointX;
  104.     /**
  105.      * @var int|null
  106.      */
  107.     private $focusPointY;
  108.     /**
  109.      * Constructor.
  110.      */
  111.     public function __construct()
  112.     {
  113.         $this->contentLanguages = new ArrayCollection();
  114.         $this->publishLanguages = new ArrayCollection();
  115.         $this->meta = new ArrayCollection();
  116.         $this->formatOptions = new ArrayCollection();
  117.         $this->tags = new ArrayCollection();
  118.         $this->categories = new ArrayCollection();
  119.         $this->targetGroups = new ArrayCollection();
  120.     }
  121.     /**
  122.      * Set name.
  123.      *
  124.      * @param string $name
  125.      *
  126.      * @return FileVersion
  127.      */
  128.     public function setName($name)
  129.     {
  130.         $this->name $name;
  131.         return $this;
  132.     }
  133.     /**
  134.      * Get name.
  135.      *
  136.      * @return string
  137.      */
  138.     public function getName()
  139.     {
  140.         return $this->name;
  141.     }
  142.     /**
  143.      * Set version.
  144.      *
  145.      * @param int $version
  146.      *
  147.      * @return FileVersion
  148.      */
  149.     public function setVersion($version)
  150.     {
  151.         $this->version $version;
  152.         return $this;
  153.     }
  154.     /**
  155.      * Get version.
  156.      *
  157.      * @return int
  158.      */
  159.     public function getVersion()
  160.     {
  161.         return $this->version;
  162.     }
  163.     /**
  164.      * Increases the subversion. Required for cache busting on certain operations which change the image without
  165.      * creating a new file version.
  166.      *
  167.      * @return FileVersion
  168.      */
  169.     public function increaseSubVersion()
  170.     {
  171.         ++$this->subVersion;
  172.         return $this;
  173.     }
  174.     /**
  175.      * Get subVersion.
  176.      *
  177.      * @return int
  178.      */
  179.     public function getSubVersion()
  180.     {
  181.         return $this->subVersion;
  182.     }
  183.     /**
  184.      * Set size.
  185.      *
  186.      * @param int $size
  187.      *
  188.      * @return FileVersion
  189.      */
  190.     public function setSize($size)
  191.     {
  192.         $this->size $size;
  193.         return $this;
  194.     }
  195.     /**
  196.      * Get size.
  197.      *
  198.      * @return int
  199.      */
  200.     public function getSize()
  201.     {
  202.         return $this->size;
  203.     }
  204.     /**
  205.      * Set mimeType.
  206.      *
  207.      * @param string $mimeType
  208.      *
  209.      * @return FileVersion
  210.      */
  211.     public function setMimeType($mimeType)
  212.     {
  213.         $this->mimeType $mimeType;
  214.         return $this;
  215.     }
  216.     /**
  217.      * Get mimeType.
  218.      *
  219.      * @return string|null
  220.      */
  221.     public function getMimeType()
  222.     {
  223.         return $this->mimeType;
  224.     }
  225.     /**
  226.      * Get extension.
  227.      *
  228.      * @return null|string
  229.      */
  230.     public function getExtension()
  231.     {
  232.         $pathInfo \pathinfo($this->getName());
  233.         $extension MimeTypes::getDefault()->getExtensions($this->getMimeType() ?? '')[0] ?? null;
  234.         if ($extension) {
  235.             return $extension;
  236.         } elseif (isset($pathInfo['extension'])) {
  237.             return $pathInfo['extension'];
  238.         }
  239.         return null;
  240.     }
  241.     public function setStorageOptions(array $storageOptions)
  242.     {
  243.         $serializedText \json_encode($storageOptions);
  244.         if (false === $serializedText) {
  245.             return;
  246.         }
  247.         $this->storageOptions $serializedText;
  248.         return $this;
  249.     }
  250.     /**
  251.      * @return mixed[]
  252.      */
  253.     public function getStorageOptions(): array
  254.     {
  255.         $storageOptions \json_decode($this->storageOptions ?? ''true);
  256.         if (!$storageOptions) {
  257.             return [];
  258.         }
  259.         return $storageOptions;
  260.     }
  261.     /**
  262.      * Set downloadCounter.
  263.      *
  264.      * @param int $downloadCounter
  265.      *
  266.      * @return FileVersion
  267.      */
  268.     public function setDownloadCounter($downloadCounter)
  269.     {
  270.         $this->downloadCounter $downloadCounter;
  271.         return $this;
  272.     }
  273.     /**
  274.      * Get downloadCounter.
  275.      *
  276.      * @return int
  277.      */
  278.     public function getDownloadCounter()
  279.     {
  280.         return $this->downloadCounter;
  281.     }
  282.     /**
  283.      * Get id.
  284.      *
  285.      * @return int
  286.      */
  287.     public function getId()
  288.     {
  289.         return $this->id;
  290.     }
  291.     /**
  292.      * Add contentLanguages.
  293.      *
  294.      * @return FileVersion
  295.      */
  296.     public function addContentLanguage(FileVersionContentLanguage $contentLanguages)
  297.     {
  298.         $this->contentLanguages[] = $contentLanguages;
  299.         return $this;
  300.     }
  301.     /**
  302.      * Remove contentLanguages.
  303.      */
  304.     public function removeContentLanguage(FileVersionContentLanguage $contentLanguages)
  305.     {
  306.         $this->contentLanguages->removeElement($contentLanguages);
  307.     }
  308.     /**
  309.      * Get contentLanguages.
  310.      *
  311.      * @return DoctrineCollection<int, FileVersionContentLanguage>
  312.      */
  313.     public function getContentLanguages()
  314.     {
  315.         return $this->contentLanguages;
  316.     }
  317.     /**
  318.      * Add publishLanguages.
  319.      *
  320.      * @return FileVersion
  321.      */
  322.     public function addPublishLanguage(FileVersionPublishLanguage $publishLanguages)
  323.     {
  324.         $this->publishLanguages[] = $publishLanguages;
  325.         return $this;
  326.     }
  327.     /**
  328.      * Remove publishLanguages.
  329.      */
  330.     public function removePublishLanguage(FileVersionPublishLanguage $publishLanguages)
  331.     {
  332.         $this->publishLanguages->removeElement($publishLanguages);
  333.     }
  334.     /**
  335.      * Get publishLanguages.
  336.      *
  337.      * @return DoctrineCollection<int, FileVersionPublishLanguage>
  338.      */
  339.     public function getPublishLanguages()
  340.     {
  341.         return $this->publishLanguages;
  342.     }
  343.     /**
  344.      * Add meta.
  345.      *
  346.      * @return FileVersion
  347.      */
  348.     public function addMeta(FileVersionMeta $meta)
  349.     {
  350.         $this->meta[] = $meta;
  351.         return $this;
  352.     }
  353.     /**
  354.      * Remove meta.
  355.      */
  356.     public function removeMeta(FileVersionMeta $meta)
  357.     {
  358.         $this->meta->removeElement($meta);
  359.     }
  360.     /**
  361.      * Get meta.
  362.      *
  363.      * @return DoctrineCollection<int, FileVersionMeta>
  364.      */
  365.     public function getMeta()
  366.     {
  367.         return $this->meta;
  368.     }
  369.     /**
  370.      * Adds a format-options entity to the file-version.
  371.      *
  372.      * @return FileVersion
  373.      */
  374.     public function addFormatOptions(FormatOptions $formatOptions)
  375.     {
  376.         $this->formatOptions[$formatOptions->getFormatKey()] = $formatOptions;
  377.         return $this;
  378.     }
  379.     /**
  380.      * Get formatOptions.
  381.      *
  382.      * @return DoctrineCollection<string, FormatOptions>
  383.      */
  384.     public function getFormatOptions()
  385.     {
  386.         return $this->formatOptions;
  387.     }
  388.     /**
  389.      * Set file.
  390.      *
  391.      * @return FileVersion
  392.      */
  393.     public function setFile(?File $file null)
  394.     {
  395.         $this->file $file;
  396.         return $this;
  397.     }
  398.     /**
  399.      * Get file.
  400.      *
  401.      * @return File
  402.      */
  403.     public function getFile()
  404.     {
  405.         return $this->file;
  406.     }
  407.     /**
  408.      * Add tags.
  409.      *
  410.      * @return FileVersion
  411.      */
  412.     public function addTag(TagInterface $tags)
  413.     {
  414.         $this->tags[] = $tags;
  415.         return $this;
  416.     }
  417.     /**
  418.      * Remove tags.
  419.      */
  420.     public function removeTag(TagInterface $tags)
  421.     {
  422.         $this->tags->removeElement($tags);
  423.     }
  424.     /**
  425.      * Remove all tags.
  426.      */
  427.     public function removeTags()
  428.     {
  429.         $this->tags->clear();
  430.     }
  431.     /**
  432.      * Get tags.
  433.      *
  434.      * @return DoctrineCollection<int, TagInterface>
  435.      */
  436.     public function getTags()
  437.     {
  438.         return $this->tags;
  439.     }
  440.     /**
  441.      * Set defaultMeta.
  442.      *
  443.      * @return FileVersion
  444.      */
  445.     public function setDefaultMeta(?FileVersionMeta $defaultMeta null)
  446.     {
  447.         $this->defaultMeta $defaultMeta;
  448.         return $this;
  449.     }
  450.     /**
  451.      * Get defaultMeta.
  452.      *
  453.      * @return FileVersionMeta
  454.      */
  455.     public function getDefaultMeta()
  456.     {
  457.         return $this->defaultMeta;
  458.     }
  459.     /**
  460.      * don't clone id to create a new entities.
  461.      */
  462.     public function __clone()
  463.     {
  464.         if ($this->id) {
  465.             $this->id null;
  466.             /** @var FileVersionMeta[] $newMetaList */
  467.             $newMetaList = [];
  468.             $defaultMetaLocale $this->getDefaultMeta()->getLocale();
  469.             /** @var FileVersionContentLanguage[] $newContentLanguageList */
  470.             $newContentLanguageList = [];
  471.             /** @var FileVersionPublishLanguage[] $newPublishLanguageList */
  472.             $newPublishLanguageList = [];
  473.             /** @var FormatOptions[] $newFormatOptionsArray */
  474.             $newFormatOptionsArray = [];
  475.             foreach ($this->meta as $meta) {
  476.                 /* @var FileVersionMeta $meta */
  477.                 $newMetaList[] = clone $meta;
  478.             }
  479.             $this->meta->clear();
  480.             foreach ($newMetaList as $newMeta) {
  481.                 $newMeta->setFileVersion($this);
  482.                 $this->addMeta($newMeta);
  483.                 if ($newMeta->getLocale() === $defaultMetaLocale) {
  484.                     $this->setDefaultMeta($newMeta);
  485.                 }
  486.             }
  487.             foreach ($this->contentLanguages as $contentLanguage) {
  488.                 /* @var FileVersionContentLanguage $contentLanguage */
  489.                 $newContentLanguageList[] = clone $contentLanguage;
  490.             }
  491.             $this->contentLanguages->clear();
  492.             foreach ($newContentLanguageList as $newContentLanguage) {
  493.                 $newContentLanguage->setFileVersion($this);
  494.                 $this->addContentLanguage($newContentLanguage);
  495.             }
  496.             foreach ($this->publishLanguages as $publishLanguage) {
  497.                 /* @var FileVersionPublishLanguage $publishLanguage */
  498.                 $newPublishLanguageList[] = clone $publishLanguage;
  499.             }
  500.             $this->publishLanguages->clear();
  501.             foreach ($newPublishLanguageList as $newPublishLanguage) {
  502.                 $newPublishLanguage->setFileVersion($this);
  503.                 $this->addPublishLanguage($newPublishLanguage);
  504.             }
  505.             foreach ($this->formatOptions as $formatOptions) {
  506.                 /* @var FormatOptions $formatOptions */
  507.                 $newFormatOptionsArray[] = clone $formatOptions;
  508.             }
  509.             $this->formatOptions->clear();
  510.             foreach ($newFormatOptionsArray as $newFormatOptions) {
  511.                 /* @var FormatOptions $newFormatOptions */
  512.                 $newFormatOptions->setFileVersion($this);
  513.                 $this->addFormatOptions($newFormatOptions);
  514.             }
  515.         }
  516.     }
  517.     /**
  518.      * Is active.
  519.      *
  520.      * @return bool
  521.      */
  522.     public function isActive()
  523.     {
  524.         return $this->version === $this->file->getVersion();
  525.     }
  526.     /**
  527.      * @return mixed
  528.      */
  529.     public function getProperties()
  530.     {
  531.         return \json_decode($this->properties ?? ''true);
  532.     }
  533.     /**
  534.      * @return self
  535.      */
  536.     public function setProperties(array $properties)
  537.     {
  538.         $serializedText \json_encode($properties);
  539.         if (false === $serializedText) {
  540.             return $this;
  541.         }
  542.         $this->properties $serializedText;
  543.         return $this;
  544.     }
  545.     /**
  546.      * Add categories.
  547.      *
  548.      * @return self
  549.      */
  550.     public function addCategory(CategoryInterface $categories)
  551.     {
  552.         $this->categories[] = $categories;
  553.         return $this;
  554.     }
  555.     /**
  556.      * Remove categories.
  557.      */
  558.     public function removeCategories()
  559.     {
  560.         $this->categories->clear();
  561.     }
  562.     /**
  563.      * Get categories.
  564.      *
  565.      * @return DoctrineCollection<int, CategoryInterface>
  566.      */
  567.     public function getCategories()
  568.     {
  569.         return $this->categories;
  570.     }
  571.     /**
  572.      * Add a target group.
  573.      */
  574.     public function addTargetGroup(TargetGroupInterface $targetGroup)
  575.     {
  576.         $this->targetGroups[] = $targetGroup;
  577.     }
  578.     /**
  579.      * Remove all target groups.
  580.      */
  581.     public function removeTargetGroups()
  582.     {
  583.         if ($this->targetGroups) {
  584.             $this->targetGroups->clear();
  585.         }
  586.     }
  587.     /**
  588.      * @return DoctrineCollection<int, TargetGroupInterface>
  589.      */
  590.     public function getTargetGroups()
  591.     {
  592.         return $this->targetGroups;
  593.     }
  594.     /**
  595.      * Returns the x coordinate of the focus point.
  596.      *
  597.      * @return int|null
  598.      */
  599.     public function getFocusPointX()
  600.     {
  601.         return $this->focusPointX;
  602.     }
  603.     /**
  604.      * Sets the x coordinate of the focus point.
  605.      *
  606.      * @param int $focusPointX
  607.      */
  608.     public function setFocusPointX($focusPointX)
  609.     {
  610.         $this->focusPointX $focusPointX;
  611.     }
  612.     /**
  613.      * Returns the y coordinate of the focus point.
  614.      *
  615.      * @return int|null
  616.      */
  617.     public function getFocusPointY()
  618.     {
  619.         return $this->focusPointY;
  620.     }
  621.     /**
  622.      * Sets the y coordinate of the focus point.
  623.      *
  624.      * @param int $focusPointY
  625.      */
  626.     public function setFocusPointY($focusPointY)
  627.     {
  628.         $this->focusPointY $focusPointY;
  629.     }
  630. }