<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\CreateMediaAction;
use App\Trait\TimestampableEntity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* A media object, such as an image, video, or audio object embedded in a web page or a downloadable dataset i.e. DataDownload. Note that a creative work may have many media objects associated with it on the same web page. For example, a page about a single song (MusicRecording) may have a music video (VideoObject), and a high and low bandwidth audio stream (2 AudioObject's).
*
* @see https://schema.org/MediaObject
*
* @author Jordi Fernandes Alves <jfadev@gmail.com>
*/
#[ORM\Entity]
#[ApiResource(
iri: 'Media',
itemOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'media:item:get',
'enable_max_depth' => true
]
],
'put' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'media:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'media:item:put',
'enable_max_depth' => true
]
],
'delete' => [
// 'security' => "is_granted('ROLE_USER')",
]
],
collectionOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => ['media:collection:get', 'createdAt'],
'enable_max_depth' => true
]
],
'post' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'media:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'media:collection:post',
'enable_max_depth' => true
],
'controller' => CreateMediaAction::class,
'deserialize' => false,
'validation_groups' => ['Default', 'create'],
'openapi_context' => [
'requestBody' => [
'content' => [
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'file' => [
'type' => 'string',
'format' => 'binary'
]
]
]
]
]
]
],
],
]
)]
#[Vich\Uploadable]
class Media
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'NONE')]
#[ORM\Column(type: 'uuid', unique: true)]
#[ApiProperty(iri: 'https://schema.org/identifier')]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'endorsement:collection:post',
'endorsement:collection:get',
'endorsement:item:get',
'endorsement:item:put',
'contract:collection:post',
'contract:collection:get',
'contract:item:get',
'contract:item:put',
'demand_upload:collection:post',
'demand_upload:collection:get',
'demand_upload:item:get',
'demand_upload:item:put',
'insured_upload:collection:post',
'insured_upload:collection:get',
'insured_upload:item:get',
'insured_upload:item:put',
'insured_insurer_global_ccg:collection:post',
'insured_insurer_global_ccg:collection:get',
'insured_insurer_global_ccg:item:get',
'insured_insurer_global_ccg:item:put',
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
private ?UuidInterface $id = null;
/**
* The name of the item.
*
* @see https://schema.org/name
*/
#[ORM\Column(type: 'string', nullable: false)]
#[ApiProperty(iri: 'https://schema.org/name')]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'endorsement:collection:get',
'endorsement:item:get',
'contract:collection:get',
'contract:item:get',
'demand_upload:collection:get',
'demand_upload:item:get',
'insured_upload:collection:get',
'insured_upload:item:get',
'insured_insurer_global_ccg:collection:get',
'insured_insurer_global_ccg:item:get',
'user:collection:get',
'user:item:get',
'insured:item:get',
'policy:item:get',
'demand:item:get',
])]
private ?string $name = '';
/**
* Actual bytes of the media object, for example the image file or video file.
*
* @see https://schema.org/contentUrl
*/
#[ApiProperty(iri: 'https://schema.org/contentUrl')]
#[Groups([
'media:collection:post',
'media:item:get',
'endorsement:collection:get',
'endorsement:item:get',
'contract:collection:get',
'contract:item:get',
'demand_upload:collection:get',
'demand_upload:item:get',
'insured_upload:collection:get',
'insured_upload:item:get',
'insured_insurer_global_ccg:collection:get',
'insured_insurer_global_ccg:item:get',
'user:collection:get',
'user:item:get'
])]
private ?string $contentUrl = null;
/**
* File size in (mega/kilo) bytes.
*
* @see https://schema.org/contentSize
*/
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/contentSize')]
#[Assert\Type('string')]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'endorsement:collection:get',
'endorsement:item:get',
'contract:collection:get',
'contract:item:get',
'demand_upload:collection:get',
'demand_upload:item:get',
'insured_upload:collection:get',
'insured_upload:item:get',
'insured_insurer_global_ccg:collection:get',
'insured_insurer_global_ccg:item:get',
'user:collection:get',
'user:item:get'
])]
private ?string $contentSize = null;
/**
* Media type typically expressed using a MIME format (see \[IANA site\](http://www.iana.org/assignments/media-types/media-types.xhtml) and \[MDN reference\](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics\_of\_HTTP/MIME\_types)) e.g. application/zip for a SoftwareApplication binary, audio/mpeg for .mp3 etc.). In cases where a \[\[CreativeWork\]\] has several media type representations, \[\[encoding\]\] can be used to indicate each \[\[MediaObject\]\] alongside particular \[\[encodingFormat\]\] information. Unregistered or niche encoding and file formats can be indicated instead via the most appropriate URL, e.g. defining Web page or a Wikipedia/Wikidata entry.
*
* @see https://schema.org/encodingFormat
*/
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/encodingFormat')]
#[Assert\Type('string')]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'endorsement:collection:get',
'endorsement:item:get',
'contract:collection:get',
'contract:item:get',
'demand_upload:collection:get',
'demand_upload:item:get',
'insured_upload:collection:get',
'insured_upload:item:get',
'insured_insurer_global_ccg:collection:get',
'insured_insurer_global_ccg:item:get',
'user:collection:get',
'user:item:get'
])]
private ?string $encodingFormat = null;
/**
* Date when this media object was uploaded to this site.
*
* @see https://schema.org/uploadDate
*/
#[ORM\Column(type: 'date', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/uploadDate')]
#[Assert\Type(\DateTimeInterface::class)]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
])]
private ?\DateTimeInterface $uploadDate = null;
/**
* Genre of the creative work, broadcast channel or group.
*
* @see https://schema.org/genre
*/
#[ORM\Column(type: 'string', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/genre')]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'endorsement:collection:get',
'endorsement:item:get',
'contract:collection:get',
'contract:item:get',
'demand_upload:collection:get',
'demand_upload:item:get',
'insured_upload:collection:get',
'insured_upload:item:get',
'insured_insurer_global_ccg:collection:get',
'insured_insurer_global_ccg:item:get',
'user:collection:get',
'user:item:get'
])]
private ?string $genre = null;
#[Assert\NotNull(groups: ['create'])]
#[Vich\UploadableField(
mapping: 'medias',
fileNameProperty: 'filePath',
size: 'contentSize',
mimeType: 'encodingFormat',
originalName: 'name'
)]
private ?File $file = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
])]
private ?string $filePath = null;
// #[ORM\ManyToOne(targetEntity: Insured::class, inversedBy: 'uploads')]
// private $insured;
#[ORM\ManyToOne(targetEntity: Contract::class, inversedBy: 'documents')]
private ?Contract $contract = null;
// #[ORM\OneToOne(mappedBy: 'upload', targetEntity: PolicyUpload::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// private $policyUpload;
// #[ORM\OneToOne(mappedBy: 'upload', targetEntity: DemandUpload::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// private $demandUpload;
// #[ORM\OneToOne(mappedBy: 'upload', targetEntity: Endorsement::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// private $endorsement;
// #[ORM\OneToOne(mappedBy: 'upload', targetEntity: InsuredUpload::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// private $insuredUpload;
// #[ORM\OneToOne(mappedBy: 'upload', targetEntity: InsuredInsurerGlobalCcg::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// private $insuredInsurerGlobalCcg;
public function __construct()
{
$this->id = Uuid::uuid4();
}
public function getId(): ?UuidInterface
{
return $this->id;
}
// public function setContentUrl(?string $contentUrl): void
// {
// $this->contentUrl = $contentUrl;
// }
public function getContentUrl(): ?string
{
return $this->genre === 'avatar' ? '/api/avatar/' . $this->id : '/api/download/' . $this->id;
}
public function setContentSize(?string $contentSize): void
{
$this->contentSize = $contentSize;
}
public function getContentSize(): ?string
{
return $this->contentSize;
}
public function setEncodingFormat(?string $encodingFormat): void
{
$this->encodingFormat = $encodingFormat;
}
public function getEncodingFormat(): ?string
{
return $this->encodingFormat;
}
public function setUploadDate(?\DateTimeInterface $uploadDate): void
{
$this->uploadDate = $uploadDate;
}
public function getUploadDate(): ?\DateTimeInterface
{
return $this->uploadDate;
}
public function setGenre(?string $genre): void
{
$this->genre = $genre;
}
public function getGenre(): ?string
{
return $this->genre;
}
// public function getInsured(): ?Insured
// {
// return $this->insured;
// }
// public function setInsured(?Insured $insured): self
// {
// $this->insured = $insured;
// return $this;
// }
// public function getDemand(): ?Demand
// {
// return $this->demand;
// }
// public function setDemand(?Demand $demand): self
// {
// $this->demand = $demand;
// return $this;
// }
// public function getPolicy(): ?Policy
// {
// return $this->policy;
// }
// public function setPolicy(?Policy $policy): self
// {
// $this->policy = $policy;
// return $this;
// }
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
// public function getEndorsement(): ?Endorsement
// {
// return $this->endorsement;
// }
// public function setEndorsement(?Endorsement $endorsement): self
// {
// // unset the owning side of the relation if necessary
// if (null === $endorsement && null !== $this->endorsement) {
// $this->endorsement->setUpload(null);
// }
// // set the owning side of the relation if necessary
// if (null !== $endorsement && $endorsement->getUpload() !== $this) {
// $endorsement->setUpload($this);
// }
// $this->endorsement = $endorsement;
// return $this;
// }
// public function getPolicyUpload(): ?PolicyUpload
// {
// return $this->policyUpload;
// }
// public function setPolicyUpload(?PolicyUpload $policyUpload): self
// {
// // unset the owning side of the relation if necessary
// if (null === $policyUpload && null !== $this->policyUpload) {
// $this->policyUpload->setUpload(null);
// }
// // set the owning side of the relation if necessary
// if (null !== $policyUpload && $policyUpload->getUpload() !== $this) {
// $policyUpload->setUpload($this);
// }
// $this->policyUpload = $policyUpload;
// return $this;
// }
// public function getDemandUpload(): ?DemandUpload
// {
// return $this->demandUpload;
// }
// public function setDemandUpload(?DemandUpload $demandUpload): self
// {
// // unset the owning side of the relation if necessary
// if (null === $demandUpload && null !== $this->demandUpload) {
// $this->PolicyUpload->setUpload(null);
// }
// // set the owning side of the relation if necessary
// if (null !== $demandUpload && $demandUpload->getUpload() !== $this) {
// $demandUpload->setUpload($this);
// }
// $this->demandUpload = $demandUpload;
// return $this;
// }
// public function getInsuredUpload(): ?InsuredUpload
// {
// return $this->insuredUpload;
// }
// public function setInsuredUpload(?InsuredUpload $insuredUpload): self
// {
// // unset the owning side of the relation if necessary
// if (null === $insuredUpload && null !== $this->insuredUpload) {
// $this->insuredUpload->setUpload(null);
// }
// // set the owning side of the relation if necessary
// if (null !== $insuredUpload && $insuredUpload->getUpload() !== $this) {
// $insuredUpload->setUpload($this);
// }
// $this->insuredUpload = $insuredUpload;
// return $this;
// }
public function getContract(): ?Contract
{
return $this->contract;
}
public function setContract(?Contract $contract): self
{
$this->contract = $contract;
return $this;
}
public function getFile(): ?File
{
return $this->file;
}
public function setFile(?File $file): self
{
$this->file = $file;
if (null !== $file) {
$this->uploadDate = new \DateTimeImmutable();
}
return $this;
}
public function getFilePath(): ?string
{
return $this->filePath;
}
public function setFilePath(?string $filePath): self
{
$this->filePath = $filePath;
return $this;
}
}