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|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. private $file;
  79. /**
  80. * @var DoctrineCollection<int, TagInterface>
  81. */
  82. private $tags;
  83. /**
  84. * @var FileVersionMeta
  85. */
  86. private $defaultMeta;
  87. /**
  88. * @var string|null
  89. */
  90. private $properties = '{}';
  91. /**
  92. * @var DoctrineCollection<int, CategoryInterface>
  93. */
  94. private $categories;
  95. /**
  96. * @var DoctrineCollection<int, TargetGroupInterface>
  97. */
  98. private $targetGroups;
  99. /**
  100. * @var int|null
  101. */
  102. private $focusPointX;
  103. /**
  104. * @var int|null
  105. */
  106. private $focusPointY;
  107. /**
  108. * Constructor.
  109. */
  110. public function __construct()
  111. {
  112. $this->contentLanguages = new ArrayCollection();
  113. $this->publishLanguages = new ArrayCollection();
  114. $this->meta = new ArrayCollection();
  115. $this->formatOptions = new ArrayCollection();
  116. $this->tags = new ArrayCollection();
  117. $this->categories = new ArrayCollection();
  118. $this->targetGroups = new ArrayCollection();
  119. }
  120. /**
  121. * Set name.
  122. *
  123. * @param string $name
  124. *
  125. * @return FileVersion
  126. */
  127. public function setName($name)
  128. {
  129. $this->name = $name;
  130. return $this;
  131. }
  132. /**
  133. * Get name.
  134. *
  135. * @return string
  136. */
  137. public function getName()
  138. {
  139. return $this->name;
  140. }
  141. /**
  142. * Set version.
  143. *
  144. * @param int $version
  145. *
  146. * @return FileVersion
  147. */
  148. public function setVersion($version)
  149. {
  150. $this->version = $version;
  151. return $this;
  152. }
  153. /**
  154. * Get version.
  155. *
  156. * @return int
  157. */
  158. public function getVersion()
  159. {
  160. return $this->version;
  161. }
  162. /**
  163. * Increases the subversion. Required for cache busting on certain operations which change the image without
  164. * creating a new file version.
  165. *
  166. * @return FileVersion
  167. */
  168. public function increaseSubVersion()
  169. {
  170. ++$this->subVersion;
  171. return $this;
  172. }
  173. /**
  174. * Get subVersion.
  175. *
  176. * @return int
  177. */
  178. public function getSubVersion()
  179. {
  180. return $this->subVersion;
  181. }
  182. /**
  183. * Set size.
  184. *
  185. * @param int $size
  186. *
  187. * @return FileVersion
  188. */
  189. public function setSize($size)
  190. {
  191. $this->size = $size;
  192. return $this;
  193. }
  194. /**
  195. * Get size.
  196. *
  197. * @return int
  198. */
  199. public function getSize()
  200. {
  201. return $this->size;
  202. }
  203. /**
  204. * Set mimeType.
  205. *
  206. * @param string $mimeType
  207. *
  208. * @return FileVersion
  209. */
  210. public function setMimeType($mimeType)
  211. {
  212. $this->mimeType = $mimeType;
  213. return $this;
  214. }
  215. /**
  216. * Get mimeType.
  217. *
  218. * @return string|null
  219. */
  220. public function getMimeType()
  221. {
  222. return $this->mimeType;
  223. }
  224. /**
  225. * Get extension.
  226. *
  227. * @return null|string
  228. */
  229. public function getExtension()
  230. {
  231. $pathInfo = \pathinfo($this->getName());
  232. $extension = MimeTypes::getDefault()->getExtensions($this->getMimeType() ?? '')[0] ?? null;
  233. if ($extension) {
  234. return $extension;
  235. } elseif (isset($pathInfo['extension'])) {
  236. return $pathInfo['extension'];
  237. }
  238. return null;
  239. }
  240. /**
  241. * @param array<string, string|null> $storageOptions
  242. *
  243. * @return FileVersion
  244. */
  245. public function setStorageOptions(array $storageOptions)
  246. {
  247. $serializedText = \json_encode($storageOptions);
  248. if (false === $serializedText) {
  249. return $this;
  250. }
  251. $this->storageOptions = $serializedText;
  252. return $this;
  253. }
  254. /**
  255. * @return array<string, string|null>
  256. */
  257. public function getStorageOptions(): array
  258. {
  259. /** @var array<string, string|null>|false $storageOptions */
  260. $storageOptions = \json_decode($this->storageOptions ?? '', true);
  261. if (!$storageOptions) {
  262. return [];
  263. }
  264. return $storageOptions;
  265. }
  266. /**
  267. * Set downloadCounter.
  268. *
  269. * @param int $downloadCounter
  270. *
  271. * @return FileVersion
  272. */
  273. public function setDownloadCounter($downloadCounter)
  274. {
  275. $this->downloadCounter = $downloadCounter;
  276. return $this;
  277. }
  278. /**
  279. * Get downloadCounter.
  280. *
  281. * @return int
  282. */
  283. public function getDownloadCounter()
  284. {
  285. return $this->downloadCounter;
  286. }
  287. /**
  288. * Get id.
  289. *
  290. * @return int
  291. */
  292. public function getId()
  293. {
  294. return $this->id;
  295. }
  296. /**
  297. * Add contentLanguages.
  298. *
  299. * @return FileVersion
  300. */
  301. public function addContentLanguage(FileVersionContentLanguage $contentLanguages)
  302. {
  303. $this->contentLanguages[] = $contentLanguages;
  304. return $this;
  305. }
  306. /**
  307. * Remove contentLanguages.
  308. */
  309. public function removeContentLanguage(FileVersionContentLanguage $contentLanguages)
  310. {
  311. $this->contentLanguages->removeElement($contentLanguages);
  312. }
  313. /**
  314. * Get contentLanguages.
  315. *
  316. * @return DoctrineCollection<int, FileVersionContentLanguage>
  317. */
  318. public function getContentLanguages()
  319. {
  320. return $this->contentLanguages;
  321. }
  322. /**
  323. * Add publishLanguages.
  324. *
  325. * @return FileVersion
  326. */
  327. public function addPublishLanguage(FileVersionPublishLanguage $publishLanguages)
  328. {
  329. $this->publishLanguages[] = $publishLanguages;
  330. return $this;
  331. }
  332. /**
  333. * Remove publishLanguages.
  334. */
  335. public function removePublishLanguage(FileVersionPublishLanguage $publishLanguages)
  336. {
  337. $this->publishLanguages->removeElement($publishLanguages);
  338. }
  339. /**
  340. * Get publishLanguages.
  341. *
  342. * @return DoctrineCollection<int, FileVersionPublishLanguage>
  343. */
  344. public function getPublishLanguages()
  345. {
  346. return $this->publishLanguages;
  347. }
  348. /**
  349. * Add meta.
  350. *
  351. * @return FileVersion
  352. */
  353. public function addMeta(FileVersionMeta $meta)
  354. {
  355. $this->meta[] = $meta;
  356. return $this;
  357. }
  358. /**
  359. * Remove meta.
  360. */
  361. public function removeMeta(FileVersionMeta $meta)
  362. {
  363. $this->meta->removeElement($meta);
  364. }
  365. /**
  366. * Get meta.
  367. *
  368. * @return DoctrineCollection<int, FileVersionMeta>
  369. */
  370. public function getMeta()
  371. {
  372. return $this->meta;
  373. }
  374. /**
  375. * Adds a format-options entity to the file-version.
  376. *
  377. * @return FileVersion
  378. */
  379. public function addFormatOptions(FormatOptions $formatOptions)
  380. {
  381. $this->formatOptions[$formatOptions->getFormatKey()] = $formatOptions;
  382. return $this;
  383. }
  384. /**
  385. * Get formatOptions.
  386. *
  387. * @return DoctrineCollection<string, FormatOptions>
  388. */
  389. public function getFormatOptions()
  390. {
  391. return $this->formatOptions;
  392. }
  393. /**
  394. * Set file.
  395. *
  396. * @return FileVersion
  397. */
  398. public function setFile(?File $file = null)
  399. {
  400. $this->file = $file;
  401. return $this;
  402. }
  403. /**
  404. * Get file.
  405. *
  406. * @return File
  407. */
  408. public function getFile()
  409. {
  410. return $this->file;
  411. }
  412. /**
  413. * Add tags.
  414. *
  415. * @return FileVersion
  416. */
  417. public function addTag(TagInterface $tags)
  418. {
  419. $this->tags[] = $tags;
  420. return $this;
  421. }
  422. /**
  423. * Remove tags.
  424. */
  425. public function removeTag(TagInterface $tags)
  426. {
  427. $this->tags->removeElement($tags);
  428. }
  429. /**
  430. * Remove all tags.
  431. */
  432. public function removeTags()
  433. {
  434. $this->tags->clear();
  435. }
  436. /**
  437. * Get tags.
  438. *
  439. * @return DoctrineCollection<int, TagInterface>
  440. */
  441. public function getTags()
  442. {
  443. return $this->tags;
  444. }
  445. /**
  446. * Set defaultMeta.
  447. *
  448. * @return FileVersion
  449. */
  450. public function setDefaultMeta(?FileVersionMeta $defaultMeta = null)
  451. {
  452. $this->defaultMeta = $defaultMeta;
  453. return $this;
  454. }
  455. /**
  456. * Get defaultMeta.
  457. *
  458. * @return FileVersionMeta
  459. */
  460. public function getDefaultMeta()
  461. {
  462. return $this->defaultMeta;
  463. }
  464. /**
  465. * don't clone id to create a new entities.
  466. */
  467. public function __clone()
  468. {
  469. if ($this->id) {
  470. $this->id = null;
  471. /** @var FileVersionMeta[] $newMetaList */
  472. $newMetaList = [];
  473. $defaultMetaLocale = $this->getDefaultMeta()->getLocale();
  474. /** @var FileVersionContentLanguage[] $newContentLanguageList */
  475. $newContentLanguageList = [];
  476. /** @var FileVersionPublishLanguage[] $newPublishLanguageList */
  477. $newPublishLanguageList = [];
  478. /** @var FormatOptions[] $newFormatOptionsArray */
  479. $newFormatOptionsArray = [];
  480. foreach ($this->meta as $meta) {
  481. /* @var FileVersionMeta $meta */
  482. $newMetaList[] = clone $meta;
  483. }
  484. $this->meta->clear();
  485. foreach ($newMetaList as $newMeta) {
  486. $newMeta->setFileVersion($this);
  487. $this->addMeta($newMeta);
  488. if ($newMeta->getLocale() === $defaultMetaLocale) {
  489. $this->setDefaultMeta($newMeta);
  490. }
  491. }
  492. foreach ($this->contentLanguages as $contentLanguage) {
  493. /* @var FileVersionContentLanguage $contentLanguage */
  494. $newContentLanguageList[] = clone $contentLanguage;
  495. }
  496. $this->contentLanguages->clear();
  497. foreach ($newContentLanguageList as $newContentLanguage) {
  498. $newContentLanguage->setFileVersion($this);
  499. $this->addContentLanguage($newContentLanguage);
  500. }
  501. foreach ($this->publishLanguages as $publishLanguage) {
  502. /* @var FileVersionPublishLanguage $publishLanguage */
  503. $newPublishLanguageList[] = clone $publishLanguage;
  504. }
  505. $this->publishLanguages->clear();
  506. foreach ($newPublishLanguageList as $newPublishLanguage) {
  507. $newPublishLanguage->setFileVersion($this);
  508. $this->addPublishLanguage($newPublishLanguage);
  509. }
  510. foreach ($this->formatOptions as $formatOptions) {
  511. /* @var FormatOptions $formatOptions */
  512. $newFormatOptionsArray[] = clone $formatOptions;
  513. }
  514. $this->formatOptions->clear();
  515. foreach ($newFormatOptionsArray as $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. }