<?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\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
#[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()]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'contract:collection:post',
'contract:collection:get',
'contract:item:get',
'contract:item:put',
// 'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
private ?UuidInterface $id = null;
#[ORM\Column(type: 'string', nullable: false)]
#[ApiProperty()]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'contract:collection:get',
'contract:item:get',
// 'user:collection:get',
'user:item:get',
])]
private ?string $name = '';
#[ApiProperty()]
#[Groups([
'media:collection:post',
'media:item:get',
'contract:collection:get',
'contract:item:get',
'user:collection:get',
'user:item:get'
])]
private ?string $contentUrl = null;
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty()]
#[Assert\Type('string')]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'contract:collection:get',
'contract:item:get',
// 'user:collection:get',
'user:item:get'
])]
private ?string $contentSize = null;
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty()]
#[Assert\Type('string')]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'contract:collection:get',
'contract:item:get',
// 'user:collection:get',
'user:item:get'
])]
private ?string $encodingFormat = null;
#[ORM\Column(type: 'date', nullable: true)]
#[ApiProperty()]
#[Assert\Type(\DateTimeInterface::class)]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
])]
private ?\DateTimeInterface $uploadDate = null;
#[ORM\Column(type: 'string', nullable: true)]
#[ApiProperty()]
#[Groups([
'media:collection:post',
'media:item:get',
'media:item:put',
'contract:collection:get',
'contract: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: Contract::class,
inversedBy: 'documents'
)]
private ?Contract $contract = null;
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 getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
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;
}
}