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. private string $origin = 'unknown';
  108. private bool $aiDisclosureDisabled = false;
  109. private string $aiDisclosureIconVariant = 'auto';
  110. /**
  111. * Constructor.
  112. */
  113. public function __construct()
  114. {
  115. $this->contentLanguages = new ArrayCollection();
  116. $this->publishLanguages = new ArrayCollection();
  117. $this->meta = new ArrayCollection();
  118. $this->formatOptions = new ArrayCollection();
  119. $this->tags = new ArrayCollection();
  120. $this->categories = new ArrayCollection();
  121. $this->targetGroups = new ArrayCollection();
  122. }
  123. /**
  124. * Set name.
  125. *
  126. * @param string $name
  127. *
  128. * @return FileVersion
  129. */
  130. public function setName($name)
  131. {
  132. $this->name = $name;
  133. return $this;
  134. }
  135. /**
  136. * Get name.
  137. *
  138. * @return string
  139. */
  140. public function getName()
  141. {
  142. return $this->name;
  143. }
  144. /**
  145. * Set version.
  146. *
  147. * @param int $version
  148. *
  149. * @return FileVersion
  150. */
  151. public function setVersion($version)
  152. {
  153. $this->version = $version;
  154. return $this;
  155. }
  156. /**
  157. * Get version.
  158. *
  159. * @return int
  160. */
  161. public function getVersion()
  162. {
  163. return $this->version;
  164. }
  165. /**
  166. * Increases the subversion. Required for cache busting on certain operations which change the image without
  167. * creating a new file version.
  168. *
  169. * @return FileVersion
  170. */
  171. public function increaseSubVersion()
  172. {
  173. ++$this->subVersion;
  174. return $this;
  175. }
  176. /**
  177. * Get subVersion.
  178. *
  179. * @return int
  180. */
  181. public function getSubVersion()
  182. {
  183. return $this->subVersion;
  184. }
  185. /**
  186. * Set size.
  187. *
  188. * @param int $size
  189. *
  190. * @return FileVersion
  191. */
  192. public function setSize($size)
  193. {
  194. $this->size = $size;
  195. return $this;
  196. }
  197. /**
  198. * Get size.
  199. *
  200. * @return int
  201. */
  202. public function getSize()
  203. {
  204. return $this->size;
  205. }
  206. /**
  207. * Set mimeType.
  208. *
  209. * @param string $mimeType
  210. *
  211. * @return FileVersion
  212. */
  213. public function setMimeType($mimeType)
  214. {
  215. $this->mimeType = $mimeType;
  216. return $this;
  217. }
  218. /**
  219. * Get mimeType.
  220. *
  221. * @return string|null
  222. */
  223. public function getMimeType()
  224. {
  225. return $this->mimeType;
  226. }
  227. /**
  228. * Get extension.
  229. *
  230. * @return null|string
  231. */
  232. public function getExtension()
  233. {
  234. $pathInfo = \pathinfo($this->getName());
  235. $extension = MimeTypes::getDefault()->getExtensions($this->getMimeType() ?? '')[0] ?? null;
  236. if ($extension) {
  237. return $extension;
  238. } elseif (isset($pathInfo['extension'])) {
  239. return $pathInfo['extension'];
  240. }
  241. return null;
  242. }
  243. /**
  244. * @param array<string, string|null> $storageOptions
  245. *
  246. * @return FileVersion
  247. */
  248. public function setStorageOptions(array $storageOptions)
  249. {
  250. $serializedText = \json_encode($storageOptions);
  251. if (false === $serializedText) {
  252. return $this;
  253. }
  254. $this->storageOptions = $serializedText;
  255. return $this;
  256. }
  257. /**
  258. * @return array<string, string|null>
  259. */
  260. public function getStorageOptions(): array
  261. {
  262. /** @var array<string, string|null>|false $storageOptions */
  263. $storageOptions = \json_decode($this->storageOptions ?? '', true);
  264. if (!$storageOptions) {
  265. return [];
  266. }
  267. return $storageOptions;
  268. }
  269. /**
  270. * Set downloadCounter.
  271. *
  272. * @param int $downloadCounter
  273. *
  274. * @return FileVersion
  275. */
  276. public function setDownloadCounter($downloadCounter)
  277. {
  278. $this->downloadCounter = $downloadCounter;
  279. return $this;
  280. }
  281. /**
  282. * Get downloadCounter.
  283. *
  284. * @return int
  285. */
  286. public function getDownloadCounter()
  287. {
  288. return $this->downloadCounter;
  289. }
  290. /**
  291. * Get id.
  292. *
  293. * @return int
  294. */
  295. public function getId()
  296. {
  297. return $this->id;
  298. }
  299. /**
  300. * Add contentLanguages.
  301. *
  302. * @return FileVersion
  303. */
  304. public function addContentLanguage(FileVersionContentLanguage $contentLanguages)
  305. {
  306. $this->contentLanguages[] = $contentLanguages;
  307. return $this;
  308. }
  309. /**
  310. * Remove contentLanguages.
  311. */
  312. public function removeContentLanguage(FileVersionContentLanguage $contentLanguages)
  313. {
  314. $this->contentLanguages->removeElement($contentLanguages);
  315. }
  316. /**
  317. * Get contentLanguages.
  318. *
  319. * @return DoctrineCollection<int, FileVersionContentLanguage>
  320. */
  321. public function getContentLanguages()
  322. {
  323. return $this->contentLanguages;
  324. }
  325. /**
  326. * Add publishLanguages.
  327. *
  328. * @return FileVersion
  329. */
  330. public function addPublishLanguage(FileVersionPublishLanguage $publishLanguages)
  331. {
  332. $this->publishLanguages[] = $publishLanguages;
  333. return $this;
  334. }
  335. /**
  336. * Remove publishLanguages.
  337. */
  338. public function removePublishLanguage(FileVersionPublishLanguage $publishLanguages)
  339. {
  340. $this->publishLanguages->removeElement($publishLanguages);
  341. }
  342. /**
  343. * Get publishLanguages.
  344. *
  345. * @return DoctrineCollection<int, FileVersionPublishLanguage>
  346. */
  347. public function getPublishLanguages()
  348. {
  349. return $this->publishLanguages;
  350. }
  351. /**
  352. * Add meta.
  353. *
  354. * @return FileVersion
  355. */
  356. public function addMeta(FileVersionMeta $meta)
  357. {
  358. $this->meta[] = $meta;
  359. return $this;
  360. }
  361. /**
  362. * Remove meta.
  363. */
  364. public function removeMeta(FileVersionMeta $meta)
  365. {
  366. $this->meta->removeElement($meta);
  367. }
  368. /**
  369. * Get meta.
  370. *
  371. * @return DoctrineCollection<int, FileVersionMeta>
  372. */
  373. public function getMeta()
  374. {
  375. return $this->meta;
  376. }
  377. /**
  378. * Adds a format-options entity to the file-version.
  379. *
  380. * @return FileVersion
  381. */
  382. public function addFormatOptions(FormatOptions $formatOptions)
  383. {
  384. $this->formatOptions[$formatOptions->getFormatKey()] = $formatOptions;
  385. return $this;
  386. }
  387. /**
  388. * Get formatOptions.
  389. *
  390. * @return DoctrineCollection<string, FormatOptions>
  391. */
  392. public function getFormatOptions()
  393. {
  394. return $this->formatOptions;
  395. }
  396. /**
  397. * Set file.
  398. *
  399. * @return FileVersion
  400. */
  401. public function setFile(?File $file = null)
  402. {
  403. $this->file = $file;
  404. return $this;
  405. }
  406. /**
  407. * Get file.
  408. *
  409. * @return File
  410. */
  411. public function getFile()
  412. {
  413. return $this->file;
  414. }
  415. /**
  416. * Add tags.
  417. *
  418. * @return FileVersion
  419. */
  420. public function addTag(TagInterface $tags)
  421. {
  422. $this->tags[] = $tags;
  423. return $this;
  424. }
  425. /**
  426. * Remove tags.
  427. */
  428. public function removeTag(TagInterface $tags)
  429. {
  430. $this->tags->removeElement($tags);
  431. }
  432. /**
  433. * Remove all tags.
  434. */
  435. public function removeTags()
  436. {
  437. foreach ($this->tags as $tag) {
  438. $this->tags->removeElement($tag);
  439. }
  440. }
  441. /**
  442. * Get tags.
  443. *
  444. * @return DoctrineCollection<int, TagInterface>
  445. */
  446. public function getTags()
  447. {
  448. return $this->tags;
  449. }
  450. /**
  451. * Set defaultMeta.
  452. *
  453. * @return FileVersion
  454. */
  455. public function setDefaultMeta(?FileVersionMeta $defaultMeta = null)
  456. {
  457. $this->defaultMeta = $defaultMeta;
  458. return $this;
  459. }
  460. /**
  461. * Get defaultMeta.
  462. *
  463. * @return FileVersionMeta|null
  464. */
  465. public function getDefaultMeta()
  466. {
  467. return $this->defaultMeta;
  468. }
  469. /**
  470. * don't clone id to create a new entities.
  471. */
  472. public function __clone()
  473. {
  474. if ($this->id) {
  475. $this->id = null;
  476. /** @var FileVersionMeta[] $newMetaList */
  477. $newMetaList = [];
  478. $defaultMetaLocale = $this->getDefaultMeta()?->getLocale();
  479. /** @var FileVersionContentLanguage[] $newContentLanguageList */
  480. $newContentLanguageList = [];
  481. /** @var FileVersionPublishLanguage[] $newPublishLanguageList */
  482. $newPublishLanguageList = [];
  483. /** @var FormatOptions[] $newFormatOptionsArray */
  484. $newFormatOptionsArray = [];
  485. foreach ($this->meta as $meta) {
  486. /* @var FileVersionMeta $meta */
  487. $newMetaList[] = clone $meta;
  488. }
  489. $this->meta->clear(); // @phpstan-ignore-line disallowed.method it saved to call here as it a none persisted entity
  490. foreach ($newMetaList as $newMeta) {
  491. $newMeta->setFileVersion($this);
  492. $this->addMeta($newMeta);
  493. if ($newMeta->getLocale() === $defaultMetaLocale) {
  494. $this->setDefaultMeta($newMeta);
  495. }
  496. }
  497. foreach ($this->contentLanguages as $contentLanguage) {
  498. /* @var FileVersionContentLanguage $contentLanguage */
  499. $newContentLanguageList[] = clone $contentLanguage;
  500. }
  501. $this->contentLanguages->clear(); // @phpstan-ignore-line disallowed.method it saved to call here as it a none persisted entity
  502. foreach ($newContentLanguageList as $newContentLanguage) {
  503. $newContentLanguage->setFileVersion($this);
  504. $this->addContentLanguage($newContentLanguage);
  505. }
  506. foreach ($this->publishLanguages as $publishLanguage) {
  507. /* @var FileVersionPublishLanguage $publishLanguage */
  508. $newPublishLanguageList[] = clone $publishLanguage;
  509. }
  510. $this->publishLanguages->clear(); // @phpstan-ignore-line disallowed.method it saved to call here as it a none persisted entity
  511. foreach ($newPublishLanguageList as $newPublishLanguage) {
  512. $newPublishLanguage->setFileVersion($this);
  513. $this->addPublishLanguage($newPublishLanguage);
  514. }
  515. foreach ($this->formatOptions as $formatOptions) {
  516. /* @var FormatOptions $formatOptions */
  517. $newFormatOptionsArray[] = clone $formatOptions;
  518. }
  519. $this->formatOptions->clear(); // @phpstan-ignore-line disallowed.method it saved to call here as it a none persisted entity
  520. foreach ($newFormatOptionsArray as $newFormatOptions) {
  521. $newFormatOptions->setFileVersion($this);
  522. $this->addFormatOptions($newFormatOptions);
  523. }
  524. }
  525. }
  526. /**
  527. * Is active.
  528. *
  529. * @return bool
  530. */
  531. public function isActive()
  532. {
  533. return $this->version === $this->file->getVersion();
  534. }
  535. /**
  536. * @return mixed
  537. */
  538. public function getProperties()
  539. {
  540. return \json_decode($this->properties ?? '', true);
  541. }
  542. /**
  543. * @return self
  544. */
  545. public function setProperties(array $properties)
  546. {
  547. $serializedText = \json_encode($properties);
  548. if (false === $serializedText) {
  549. return $this;
  550. }
  551. $this->properties = $serializedText;
  552. return $this;
  553. }
  554. /**
  555. * Add categories.
  556. *
  557. * @return self
  558. */
  559. public function addCategory(CategoryInterface $categories)
  560. {
  561. $this->categories[] = $categories;
  562. return $this;
  563. }
  564. /**
  565. * Remove categories.
  566. */
  567. public function removeCategories()
  568. {
  569. foreach ($this->categories as $category) {
  570. $this->categories->removeElement($category);
  571. }
  572. }
  573. /**
  574. * Get categories.
  575. *
  576. * @return DoctrineCollection<int, CategoryInterface>
  577. */
  578. public function getCategories()
  579. {
  580. return $this->categories;
  581. }
  582. /**
  583. * Add a target group.
  584. */
  585. public function addTargetGroup(TargetGroupInterface $targetGroup)
  586. {
  587. $this->getTargetGroups()[] = $targetGroup;
  588. }
  589. /**
  590. * Remove all target groups.
  591. */
  592. public function removeTargetGroups()
  593. {
  594. foreach ($this->getTargetGroups() as $targetGroup) {
  595. $this->getTargetGroups()->removeElement($targetGroup);
  596. }
  597. }
  598. /**
  599. * @return DoctrineCollection<int, TargetGroupInterface>
  600. */
  601. public function getTargetGroups()
  602. {
  603. // Lazy initialization needed because targetGroups is not mapped in Doctrine
  604. // (AudienceTargetingBundle is optional). Doctrine won't initialize this
  605. // collection when loading entities from the database.
  606. if (!isset($this->targetGroups)) { // @phpstan-ignore-line isset.initializedProperty
  607. $this->targetGroups = new ArrayCollection();
  608. }
  609. return $this->targetGroups;
  610. }
  611. /**
  612. * Returns the x coordinate of the focus point.
  613. *
  614. * @return int|null
  615. */
  616. public function getFocusPointX()
  617. {
  618. return $this->focusPointX;
  619. }
  620. /**
  621. * Sets the x coordinate of the focus point.
  622. *
  623. * @param int $focusPointX
  624. */
  625. public function setFocusPointX($focusPointX)
  626. {
  627. $this->focusPointX = $focusPointX;
  628. }
  629. /**
  630. * Returns the y coordinate of the focus point.
  631. *
  632. * @return int|null
  633. */
  634. public function getFocusPointY()
  635. {
  636. return $this->focusPointY;
  637. }
  638. /**
  639. * Sets the y coordinate of the focus point.
  640. *
  641. * @param int $focusPointY
  642. */
  643. public function setFocusPointY($focusPointY)
  644. {
  645. $this->focusPointY = $focusPointY;
  646. }
  647. public function getOrigin(): string
  648. {
  649. return $this->origin;
  650. }
  651. /**
  652. * @return $this
  653. */
  654. public function setOrigin(string $origin): self
  655. {
  656. $this->origin = $origin;
  657. return $this;
  658. }
  659. public function getAiDisclosureDisabled(): bool
  660. {
  661. return $this->aiDisclosureDisabled;
  662. }
  663. /**
  664. * @return $this
  665. */
  666. public function setAiDisclosureDisabled(bool $aiDisclosureDisabled): self
  667. {
  668. $this->aiDisclosureDisabled = $aiDisclosureDisabled;
  669. return $this;
  670. }
  671. public function getAiDisclosureIconVariant(): string
  672. {
  673. return $this->aiDisclosureIconVariant;
  674. }
  675. /**
  676. * @return $this
  677. */
  678. public function setAiDisclosureIconVariant(string $aiDisclosureIconVariant): self
  679. {
  680. $this->aiDisclosureIconVariant = $aiDisclosureIconVariant;
  681. return $this;
  682. }
  683. }