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.      * @param File $file
  388.      *
  389.      * @return FileVersion
  390.      */
  391.     public function setFile(?File $file null)
  392.     {
  393.         $this->file $file;
  394.         return $this;
  395.     }
  396.     /**
  397.      * Get file.
  398.      *
  399.      * @return File
  400.      */
  401.     public function getFile()
  402.     {
  403.         return $this->file;
  404.     }
  405.     /**
  406.      * Add tags.
  407.      *
  408.      * @return FileVersion
  409.      */
  410.     public function addTag(TagInterface $tags)
  411.     {
  412.         $this->tags[] = $tags;
  413.         return $this;
  414.     }
  415.     /**
  416.      * Remove tags.
  417.      */
  418.     public function removeTag(TagInterface $tags)
  419.     {
  420.         $this->tags->removeElement($tags);
  421.     }
  422.     /**
  423.      * Remove all tags.
  424.      */
  425.     public function removeTags()
  426.     {
  427.         $this->tags->clear();
  428.     }
  429.     /**
  430.      * Get tags.
  431.      *
  432.      * @return DoctrineCollection<int, TagInterface>
  433.      */
  434.     public function getTags()
  435.     {
  436.         return $this->tags;
  437.     }
  438.     /**
  439.      * Set defaultMeta.
  440.      *
  441.      * @param FileVersionMeta $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->propertiestrue);
  532.     }
  533.     /**
  534.      * @return self
  535.      */
  536.     public function setProperties(array $properties)
  537.     {
  538.         $this->properties \json_encode($properties);
  539.         return $this;
  540.     }
  541.     /**
  542.      * Add categories.
  543.      *
  544.      * @return self
  545.      */
  546.     public function addCategory(CategoryInterface $categories)
  547.     {
  548.         $this->categories[] = $categories;
  549.         return $this;
  550.     }
  551.     /**
  552.      * Remove categories.
  553.      */
  554.     public function removeCategories()
  555.     {
  556.         $this->categories->clear();
  557.     }
  558.     /**
  559.      * Get categories.
  560.      *
  561.      * @return DoctrineCollection<int, CategoryInterface>
  562.      */
  563.     public function getCategories()
  564.     {
  565.         return $this->categories;
  566.     }
  567.     /**
  568.      * Add a target group.
  569.      */
  570.     public function addTargetGroup(TargetGroupInterface $targetGroup)
  571.     {
  572.         $this->targetGroups[] = $targetGroup;
  573.     }
  574.     /**
  575.      * Remove all target groups.
  576.      */
  577.     public function removeTargetGroups()
  578.     {
  579.         if ($this->targetGroups) {
  580.             $this->targetGroups->clear();
  581.         }
  582.     }
  583.     /**
  584.      * @return DoctrineCollection<int, TargetGroupInterface>
  585.      */
  586.     public function getTargetGroups()
  587.     {
  588.         return $this->targetGroups;
  589.     }
  590.     /**
  591.      * Returns the x coordinate of the focus point.
  592.      *
  593.      * @return int
  594.      */
  595.     public function getFocusPointX()
  596.     {
  597.         return $this->focusPointX;
  598.     }
  599.     /**
  600.      * Sets the x coordinate of the focus point.
  601.      *
  602.      * @param int $focusPointX
  603.      */
  604.     public function setFocusPointX($focusPointX)
  605.     {
  606.         $this->focusPointX $focusPointX;
  607.     }
  608.     /**
  609.      * Returns the y coordinate of the focus point.
  610.      *
  611.      * @return int
  612.      */
  613.     public function getFocusPointY()
  614.     {
  615.         return $this->focusPointY;
  616.     }
  617.     /**
  618.      * Sets the y coordinate of the focus point.
  619.      *
  620.      * @param int $focusPointY
  621.      */
  622.     public function setFocusPointY($focusPointY)
  623.     {
  624.         $this->focusPointY $focusPointY;
  625.     }
  626. }