src/Domain/Media/Model/Media.php line 15

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Media\Model;
  3. use Whater\Domain\User\Model\User;
  4. use Sonata\MediaBundle\Entity\BaseMedia;
  5. use Ramsey\Uuid\Uuid;
  6. use Whater\Domain\Common\Exception\InvalidUUIDException;
  7. /**
  8. * Class Media
  9. *
  10. * @package Whater\Domain\Media\Model
  11. */
  12. class Media extends BaseMedia
  13. {
  14. /**
  15. * @var string
  16. */
  17. private $uuid;
  18. /**
  19. * @var int
  20. */
  21. private $position;
  22. /**
  23. * @var User
  24. */
  25. private $user;
  26. /**
  27. * @return string
  28. */
  29. public function getId(): string
  30. {
  31. return $this->uuid;
  32. }
  33. /**
  34. * @return int
  35. */
  36. public function position(): int
  37. {
  38. return $this->position;
  39. }
  40. /**
  41. * @return string
  42. */
  43. public function context(): string
  44. {
  45. return $this->context;
  46. }
  47. /**
  48. * @return User
  49. */
  50. public function user(): ?User
  51. {
  52. return $this->user;
  53. }
  54. /**
  55. * Media constructor.
  56. */
  57. public function __construct(
  58. string $mediaId = null,
  59. string $context,
  60. string $name,
  61. string $providerName,
  62. $binaryContent,
  63. ?bool $enabled = true,
  64. ?int $position = 1,
  65. ?User $user = null
  66. ) {
  67. try {
  68. $this->uuid = Uuid::fromString($mediaId ?: Uuid::uuid4())->toString();
  69. } catch (\InvalidArgumentException $e) {
  70. throw new InvalidUUIDException();
  71. }
  72. $this->setContext($context);
  73. $this->setName($name);
  74. $this->setProviderName($providerName);
  75. $this->setBinaryContent($binaryContent);
  76. $this->setEnabled($enabled);
  77. $this->position = $position;
  78. $this->user = $user;
  79. }
  80. public function updateMedia(
  81. string $name,
  82. ?string $description = null,
  83. int $position,
  84. bool $enabled,
  85. $binaryContent = null
  86. ) {
  87. $this->setName($name);
  88. if ($description != null) {
  89. $this->setDescription($description);
  90. }
  91. $this->position = $position;
  92. $this->setEnabled($enabled);
  93. if (!empty($binaryContent)) {
  94. $this->setBinaryContent($binaryContent);
  95. }
  96. }
  97. }