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.      * @param File $file
  392.      *
  393.      * @return FileVersion
  394.      */
  395.     public function setFile(File $file null)
  396.     {
  397.         $this->file $file;
  398.         return $this;
  399.     }
  400.     /**
  401.      * Get file.
  402.      *
  403.      * @return File
  404.      */
  405.     public function getFile()
  406.     {
  407.         return $this->file;
  408.     }
  409.     /**
  410.      * Add tags.
  411.      *
  412.      * @return FileVersion
  413.      */
  414.     public function addTag(TagInterface $tags)
  415.     {
  416.         $this->tags[] = $tags;
  417.         return $this;
  418.     }
  419.     /**
  420.      * Remove tags.
  421.      */
  422.     public function removeTag(TagInterface $tags)
  423.     {
  424.         $this->tags->removeElement($tags);
  425.     }
  426.     /**
  427.      * Remove all tags.
  428.      */
  429.     public function removeTags()
  430.     {
  431.         $this->tags->clear();
  432.     }
  433.     /**
  434.      * Get tags.
  435.      *
  436.      * @return DoctrineCollection<int, TagInterface>
  437.      */
  438.     public function getTags()
  439.     {
  440.         return $this->tags;
  441.     }
  442.     /**
  443.      * Set defaultMeta.
  444.      *
  445.      * @param FileVersionMeta $defaultMeta
  446.      *
  447.      * @return FileVersion
  448.      */
  449.     public function setDefaultMeta(FileVersionMeta $defaultMeta null)
  450.     {
  451.         $this->defaultMeta $defaultMeta;
  452.         return $this;
  453.     }
  454.     /**
  455.      * Get defaultMeta.
  456.      *
  457.      * @return FileVersionMeta
  458.      */
  459.     public function getDefaultMeta()
  460.     {
  461.         return $this->defaultMeta;
  462.     }
  463.     /**
  464.      * don't clone id to create a new entities.
  465.      */
  466.     public function __clone()
  467.     {
  468.         if ($this->id) {
  469.             $this->id null;
  470.             /** @var FileVersionMeta[] $newMetaList */
  471.             $newMetaList = [];
  472.             $defaultMetaLocale $this->getDefaultMeta()->getLocale();
  473.             /** @var FileVersionContentLanguage[] $newContentLanguageList */
  474.             $newContentLanguageList = [];
  475.             /** @var FileVersionPublishLanguage[] $newPublishLanguageList */
  476.             $newPublishLanguageList = [];
  477.             /** @var FormatOptions[] $newFormatOptionsArray */
  478.             $newFormatOptionsArray = [];
  479.             foreach ($this->meta as $meta) {
  480.                 /* @var FileVersionMeta $meta */
  481.                 $newMetaList[] = clone $meta;
  482.             }
  483.             $this->meta->clear();
  484.             foreach ($newMetaList as $newMeta) {
  485.                 $newMeta->setFileVersion($this);
  486.                 $this->addMeta($newMeta);
  487.                 if ($newMeta->getLocale() === $defaultMetaLocale) {
  488.                     $this->setDefaultMeta($newMeta);
  489.                 }
  490.             }
  491.             foreach ($this->contentLanguages as $contentLanguage) {
  492.                 /* @var FileVersionContentLanguage $contentLanguage */
  493.                 $newContentLanguageList[] = clone $contentLanguage;
  494.             }
  495.             $this->contentLanguages->clear();
  496.             foreach ($newContentLanguageList as $newContentLanguage) {
  497.                 $newContentLanguage->setFileVersion($this);
  498.                 $this->addContentLanguage($newContentLanguage);
  499.             }
  500.             foreach ($this->publishLanguages as $publishLanguage) {
  501.                 /* @var FileVersionPublishLanguage $publishLanguage */
  502.                 $newPublishLanguageList[] = clone $publishLanguage;
  503.             }
  504.             $this->publishLanguages->clear();
  505.             foreach ($newPublishLanguageList as $newPublishLanguage) {
  506.                 $newPublishLanguage->setFileVersion($this);
  507.                 $this->addPublishLanguage($newPublishLanguage);
  508.             }
  509.             foreach ($this->formatOptions as $formatOptions) {
  510.                 /* @var FormatOptions $formatOptions */
  511.                 $newFormatOptionsArray[] = clone $formatOptions;
  512.             }
  513.             $this->formatOptions->clear();
  514.             foreach ($newFormatOptionsArray as $newFormatOptions) {
  515.                 /* @var FormatOptions $newFormatOptions */
  516.                 $newFormatOptions->setFileVersion($this);
  517.                 $this->addFormatOptions($newFormatOptions);
  518.             }
  519.         }
  520.     }
  521.     /**
  522.      * Is active.
  523.      *
  524.      * @return bool
  525.      */
  526.     public function isActive()
  527.     {
  528.         return $this->version === $this->file->getVersion();
  529.     }
  530.     /**
  531.      * @return mixed
  532.      */
  533.     public function getProperties()
  534.     {
  535.         return \json_decode($this->properties ?? ''true);
  536.     }
  537.     /**
  538.      * @return self
  539.      */
  540.     public function setProperties(array $properties)
  541.     {
  542.         $serializedText \json_encode($properties);
  543.         if (false === $serializedText) {
  544.             return $this;
  545.         }
  546.         $this->properties $serializedText;
  547.         return $this;
  548.     }
  549.     /**
  550.      * Add categories.
  551.      *
  552.      * @return self
  553.      */
  554.     public function addCategory(CategoryInterface $categories)
  555.     {
  556.         $this->categories[] = $categories;
  557.         return $this;
  558.     }
  559.     /**
  560.      * Remove categories.
  561.      */
  562.     public function removeCategories()
  563.     {
  564.         $this->categories->clear();
  565.     }
  566.     /**
  567.      * Get categories.
  568.      *
  569.      * @return DoctrineCollection<int, CategoryInterface>
  570.      */
  571.     public function getCategories()
  572.     {
  573.         return $this->categories;
  574.     }
  575.     /**
  576.      * Add a target group.
  577.      */
  578.     public function addTargetGroup(TargetGroupInterface $targetGroup)
  579.     {
  580.         $this->targetGroups[] = $targetGroup;
  581.     }
  582.     /**
  583.      * Remove all target groups.
  584.      */
  585.     public function removeTargetGroups()
  586.     {
  587.         if ($this->targetGroups) {
  588.             $this->targetGroups->clear();
  589.         }
  590.     }
  591.     /**
  592.      * @return DoctrineCollection<int, TargetGroupInterface>
  593.      */
  594.     public function getTargetGroups()
  595.     {
  596.         return $this->targetGroups;
  597.     }
  598.     /**
  599.      * Returns the x coordinate of the focus point.
  600.      *
  601.      * @return int|null
  602.      */
  603.     public function getFocusPointX()
  604.     {
  605.         return $this->focusPointX;
  606.     }
  607.     /**
  608.      * Sets the x coordinate of the focus point.
  609.      *
  610.      * @param int $focusPointX
  611.      */
  612.     public function setFocusPointX($focusPointX)
  613.     {
  614.         $this->focusPointX $focusPointX;
  615.     }
  616.     /**
  617.      * Returns the y coordinate of the focus point.
  618.      *
  619.      * @return int|null
  620.      */
  621.     public function getFocusPointY()
  622.     {
  623.         return $this->focusPointY;
  624.     }
  625.     /**
  626.      * Sets the y coordinate of the focus point.
  627.      *
  628.      * @param int $focusPointY
  629.      */
  630.     public function setFocusPointY($focusPointY)
  631.     {
  632.         $this->focusPointY $focusPointY;
  633.     }
  634. }