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

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\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
  44.      */
  45.     private $mimeType;
  46.     /**
  47.      * @var string
  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
  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
  102.      */
  103.     private $focusPointX;
  104.     /**
  105.      * @var int
  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
  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.         $this->storageOptions \json_encode($storageOptions);
  244.         return $this;
  245.     }
  246.     /**
  247.      * @return mixed[]
  248.      */
  249.     public function getStorageOptions(): array
  250.     {
  251.         $storageOptions \json_decode($this->storageOptionstrue);
  252.         if (!$storageOptions) {
  253.             return [];
  254.         }
  255.         return $storageOptions;
  256.     }
  257.     /**
  258.      * Set downloadCounter.
  259.      *
  260.      * @param int $downloadCounter
  261.      *
  262.      * @return FileVersion
  263.      */
  264.     public function setDownloadCounter($downloadCounter)
  265.     {
  266.         $this->downloadCounter $downloadCounter;
  267.         return $this;
  268.     }
  269.     /**
  270.      * Get downloadCounter.
  271.      *
  272.      * @return int
  273.      */
  274.     public function getDownloadCounter()
  275.     {
  276.         return $this->downloadCounter;
  277.     }
  278.     /**
  279.      * Get id.
  280.      *
  281.      * @return int
  282.      */
  283.     public function getId()
  284.     {
  285.         return $this->id;
  286.     }
  287.     /**
  288.      * Add contentLanguages.
  289.      *
  290.      * @return FileVersion
  291.      */
  292.     public function addContentLanguage(FileVersionContentLanguage $contentLanguages)
  293.     {
  294.         $this->contentLanguages[] = $contentLanguages;
  295.         return $this;
  296.     }
  297.     /**
  298.      * Remove contentLanguages.
  299.      */
  300.     public function removeContentLanguage(FileVersionContentLanguage $contentLanguages)
  301.     {
  302.         $this->contentLanguages->removeElement($contentLanguages);
  303.     }
  304.     /**
  305.      * Get contentLanguages.
  306.      *
  307.      * @return DoctrineCollection<int, FileVersionContentLanguage>
  308.      */
  309.     public function getContentLanguages()
  310.     {
  311.         return $this->contentLanguages;
  312.     }
  313.     /**
  314.      * Add publishLanguages.
  315.      *
  316.      * @return FileVersion
  317.      */
  318.     public function addPublishLanguage(FileVersionPublishLanguage $publishLanguages)
  319.     {
  320.         $this->publishLanguages[] = $publishLanguages;
  321.         return $this;
  322.     }
  323.     /**
  324.      * Remove publishLanguages.
  325.      */
  326.     public function removePublishLanguage(FileVersionPublishLanguage $publishLanguages)
  327.     {
  328.         $this->publishLanguages->removeElement($publishLanguages);
  329.     }
  330.     /**
  331.      * Get publishLanguages.
  332.      *
  333.      * @return DoctrineCollection<int, FileVersionPublishLanguage>
  334.      */
  335.     public function getPublishLanguages()
  336.     {
  337.         return $this->publishLanguages;
  338.     }
  339.     /**
  340.      * Add meta.
  341.      *
  342.      * @return FileVersion
  343.      */
  344.     public function addMeta(FileVersionMeta $meta)
  345.     {
  346.         $this->meta[] = $meta;
  347.         return $this;
  348.     }
  349.     /**
  350.      * Remove meta.
  351.      */
  352.     public function removeMeta(FileVersionMeta $meta)
  353.     {
  354.         $this->meta->removeElement($meta);
  355.     }
  356.     /**
  357.      * Get meta.
  358.      *
  359.      * @return DoctrineCollection<int, FileVersionMeta>
  360.      */
  361.     public function getMeta()
  362.     {
  363.         return $this->meta;
  364.     }
  365.     /**
  366.      * Adds a format-options entity to the file-version.
  367.      *
  368.      * @return FileVersion
  369.      */
  370.     public function addFormatOptions(FormatOptions $formatOptions)
  371.     {
  372.         $this->formatOptions[$formatOptions->getFormatKey()] = $formatOptions;
  373.         return $this;
  374.     }
  375.     /**
  376.      * Get formatOptions.
  377.      *
  378.      * @return DoctrineCollection<string, FormatOptions>
  379.      */
  380.     public function getFormatOptions()
  381.     {
  382.         return $this->formatOptions;
  383.     }
  384.     /**
  385.      * Set file.
  386.      *
  387.      * @return FileVersion
  388.      */
  389.     public function setFile(?File $file null)
  390.     {
  391.         $this->file $file;
  392.         return $this;
  393.     }
  394.     /**
  395.      * Get file.
  396.      *
  397.      * @return File
  398.      */
  399.     public function getFile()
  400.     {
  401.         return $this->file;
  402.     }
  403.     /**
  404.      * Add tags.
  405.      *
  406.      * @return FileVersion
  407.      */
  408.     public function addTag(TagInterface $tags)
  409.     {
  410.         $this->tags[] = $tags;
  411.         return $this;
  412.     }
  413.     /**
  414.      * Remove tags.
  415.      */
  416.     public function removeTag(TagInterface $tags)
  417.     {
  418.         $this->tags->removeElement($tags);
  419.     }
  420.     /**
  421.      * Remove all tags.
  422.      */
  423.     public function removeTags()
  424.     {
  425.         $this->tags->clear();
  426.     }
  427.     /**
  428.      * Get tags.
  429.      *
  430.      * @return DoctrineCollection<int, TagInterface>
  431.      */
  432.     public function getTags()
  433.     {
  434.         return $this->tags;
  435.     }
  436.     /**
  437.      * Set defaultMeta.
  438.      *
  439.      * @return FileVersion
  440.      */
  441.     public function setDefaultMeta(?FileVersionMeta $defaultMeta null)
  442.     {
  443.         $this->defaultMeta $defaultMeta;
  444.         return $this;
  445.     }
  446.     /**
  447.      * Get defaultMeta.
  448.      *
  449.      * @return FileVersionMeta
  450.      */
  451.     public function getDefaultMeta()
  452.     {
  453.         return $this->defaultMeta;
  454.     }
  455.     /**
  456.      * don't clone id to create a new entities.
  457.      */
  458.     public function __clone()
  459.     {
  460.         if ($this->id) {
  461.             $this->id null;
  462.             /** @var FileVersionMeta[] $newMetaList */
  463.             $newMetaList = [];
  464.             $defaultMetaLocale $this->getDefaultMeta()->getLocale();
  465.             /** @var FileVersionContentLanguage[] $newContentLanguageList */
  466.             $newContentLanguageList = [];
  467.             /** @var FileVersionPublishLanguage[] $newPublishLanguageList */
  468.             $newPublishLanguageList = [];
  469.             /** @var FormatOptions[] $newFormatOptionsArray */
  470.             $newFormatOptionsArray = [];
  471.             foreach ($this->meta as $meta) {
  472.                 /* @var FileVersionMeta $meta */
  473.                 $newMetaList[] = clone $meta;
  474.             }
  475.             $this->meta->clear();
  476.             foreach ($newMetaList as $newMeta) {
  477.                 $newMeta->setFileVersion($this);
  478.                 $this->addMeta($newMeta);
  479.                 if ($newMeta->getLocale() === $defaultMetaLocale) {
  480.                     $this->setDefaultMeta($newMeta);
  481.                 }
  482.             }
  483.             foreach ($this->contentLanguages as $contentLanguage) {
  484.                 /* @var FileVersionContentLanguage $contentLanguage */
  485.                 $newContentLanguageList[] = clone $contentLanguage;
  486.             }
  487.             $this->contentLanguages->clear();
  488.             foreach ($newContentLanguageList as $newContentLanguage) {
  489.                 $newContentLanguage->setFileVersion($this);
  490.                 $this->addContentLanguage($newContentLanguage);
  491.             }
  492.             foreach ($this->publishLanguages as $publishLanguage) {
  493.                 /* @var FileVersionPublishLanguage $publishLanguage */
  494.                 $newPublishLanguageList[] = clone $publishLanguage;
  495.             }
  496.             $this->publishLanguages->clear();
  497.             foreach ($newPublishLanguageList as $newPublishLanguage) {
  498.                 $newPublishLanguage->setFileVersion($this);
  499.                 $this->addPublishLanguage($newPublishLanguage);
  500.             }
  501.             foreach ($this->formatOptions as $formatOptions) {
  502.                 /* @var FormatOptions $formatOptions */
  503.                 $newFormatOptionsArray[] = clone $formatOptions;
  504.             }
  505.             $this->formatOptions->clear();
  506.             foreach ($newFormatOptionsArray as $newFormatOptions) {
  507.                 /* @var FormatOptions $newFormatOptions */
  508.                 $newFormatOptions->setFileVersion($this);
  509.                 $this->addFormatOptions($newFormatOptions);
  510.             }
  511.         }
  512.     }
  513.     /**
  514.      * Is active.
  515.      *
  516.      * @return bool
  517.      */
  518.     public function isActive()
  519.     {
  520.         return $this->version === $this->file->getVersion();
  521.     }
  522.     /**
  523.      * @return mixed[]
  524.      */
  525.     public function getProperties()
  526.     {
  527.         return \json_decode($this->propertiestrue);
  528.     }
  529.     /**
  530.      * @return self
  531.      */
  532.     public function setProperties(array $properties)
  533.     {
  534.         $this->properties \json_encode($properties);
  535.         return $this;
  536.     }
  537.     /**
  538.      * Add categories.
  539.      *
  540.      * @return self
  541.      */
  542.     public function addCategory(CategoryInterface $categories)
  543.     {
  544.         $this->categories[] = $categories;
  545.         return $this;
  546.     }
  547.     /**
  548.      * Remove categories.
  549.      */
  550.     public function removeCategories()
  551.     {
  552.         $this->categories->clear();
  553.     }
  554.     /**
  555.      * Get categories.
  556.      *
  557.      * @return DoctrineCollection<int, CategoryInterface>
  558.      */
  559.     public function getCategories()
  560.     {
  561.         return $this->categories;
  562.     }
  563.     /**
  564.      * Add a target group.
  565.      */
  566.     public function addTargetGroup(TargetGroupInterface $targetGroup)
  567.     {
  568.         $this->targetGroups[] = $targetGroup;
  569.     }
  570.     /**
  571.      * Remove all target groups.
  572.      */
  573.     public function removeTargetGroups()
  574.     {
  575.         if ($this->targetGroups) {
  576.             $this->targetGroups->clear();
  577.         }
  578.     }
  579.     /**
  580.      * @return DoctrineCollection<int, TargetGroupInterface>
  581.      */
  582.     public function getTargetGroups()
  583.     {
  584.         return $this->targetGroups;
  585.     }
  586.     /**
  587.      * Returns the x coordinate of the focus point.
  588.      *
  589.      * @return int
  590.      */
  591.     public function getFocusPointX()
  592.     {
  593.         return $this->focusPointX;
  594.     }
  595.     /**
  596.      * Sets the x coordinate of the focus point.
  597.      *
  598.      * @param int $focusPointX
  599.      */
  600.     public function setFocusPointX($focusPointX)
  601.     {
  602.         $this->focusPointX $focusPointX;
  603.     }
  604.     /**
  605.      * Returns the y coordinate of the focus point.
  606.      *
  607.      * @return int
  608.      */
  609.     public function getFocusPointY()
  610.     {
  611.         return $this->focusPointY;
  612.     }
  613.     /**
  614.      * Sets the y coordinate of the focus point.
  615.      *
  616.      * @param int $focusPointY
  617.      */
  618.     public function setFocusPointY($focusPointY)
  619.     {
  620.         $this->focusPointY $focusPointY;
  621.     }
  622. }