src/Entity/Media.php line 88

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use App\Controller\CreateMediaAction;
  7. use App\Trait\TimestampableEntity;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  13. use Ramsey\Uuid\Uuid;
  14. use Ramsey\Uuid\UuidInterface;
  15. #[ORM\Entity]
  16. #[ApiResource(
  17.     iri'Media',
  18.     itemOperations: [
  19.         'get' => [
  20.             'security' => "is_granted('ROLE_USER')",
  21.             'normalization_context' => [
  22.                 'groups' => 'media:item:get',
  23.                 'enable_max_depth' => true
  24.             ]
  25.         ],
  26.         'put' => [
  27.             'security' => "is_granted('ROLE_USER')",
  28.             'normalization_context' => [
  29.                 'groups' => 'media:item:put',
  30.                 'enable_max_depth' => true
  31.             ],
  32.             'denormalization_context' => [
  33.                 'groups' => 'media:item:put',
  34.                 'enable_max_depth' => true
  35.             ]
  36.         ],
  37.         'delete' => [
  38.             'security' => "is_granted('ROLE_USER')",
  39.         ]
  40.     ],
  41.     collectionOperations: [
  42.         'get' => [
  43.             'security' => "is_granted('ROLE_USER')",
  44.             'normalization_context' => [
  45.                 'groups' => ['media:collection:get''createdAt'],
  46.                 'enable_max_depth' => true
  47.             ]
  48.         ],
  49.         'post' => [
  50.             'security' => "is_granted('ROLE_USER')",
  51.             'normalization_context' => [
  52.                 'groups' => 'media:collection:post',
  53.                 'enable_max_depth' => true
  54.             ],
  55.             'denormalization_context' => [
  56.                 'groups' => 'media:collection:post',
  57.                 'enable_max_depth' => true
  58.             ],
  59.             'controller' => CreateMediaAction::class,
  60.             'deserialize' => false,
  61.             'validation_groups' => ['Default''create'],
  62.             'openapi_context' => [
  63.                 'requestBody' => [
  64.                     'content' => [
  65.                         'multipart/form-data' => [
  66.                             'schema' => [
  67.                                 'type' => 'object',
  68.                                 'properties' => [
  69.                                     'file' => [
  70.                                         'type' => 'string',
  71.                                         'format' => 'binary'
  72.                                     ]
  73.                                 ]
  74.                             ]
  75.                         ]
  76.                     ]
  77.                 ]
  78.             ],
  79.         ],
  80.     ]
  81. )]
  82. #[Vich\Uploadable]
  83. class Media
  84. {
  85.     use TimestampableEntity;
  86.     
  87.     #[ORM\Id]
  88.     #[ORM\GeneratedValue(strategy'NONE')]
  89.     #[ORM\Column(type'uuid'uniquetrue)]
  90.     #[ApiProperty()]
  91.     #[Groups([
  92.         'media:collection:post',
  93.         'media:item:get',
  94.         'media:item:put',
  95.         'contract:collection:post',
  96.         'contract:collection:get',
  97.         'contract:item:get',
  98.         'contract:item:put',
  99.         // 'user:collection:get',
  100.         'user:collection:post',
  101.         'user:item:get',
  102.         'user:item:put',
  103.     ])]
  104.     private ?UuidInterface $id null;
  105.     #[ORM\Column(type'string'nullablefalse)]
  106.     #[ApiProperty()]
  107.     #[Groups([
  108.         'media:collection:post',
  109.         'media:item:get',
  110.         'media:item:put',
  111.         'contract:collection:get',
  112.         'contract:item:get',
  113.         // 'user:collection:get',
  114.         'user:item:get',
  115.     ])]
  116.     private ?string $name '';
  117.     #[ApiProperty()]
  118.     #[Groups([
  119.         'media:collection:post',
  120.         'media:item:get',
  121.         'contract:collection:get',
  122.         'contract:item:get',
  123.         'user:collection:get',
  124.         'user:item:get'
  125.     ])]
  126.     private ?string $contentUrl null;
  127.     #[ORM\Column(type'text'nullabletrue)]
  128.     #[ApiProperty()]
  129.     #[Assert\Type('string')]
  130.     #[Groups([
  131.         'media:collection:post',
  132.         'media:item:get',
  133.         'media:item:put',
  134.         'contract:collection:get',
  135.         'contract:item:get',
  136.         // 'user:collection:get',
  137.         'user:item:get'
  138.     ])]
  139.     private ?string $contentSize null;
  140.     #[ORM\Column(type'text'nullabletrue)]
  141.     #[ApiProperty()]
  142.     #[Assert\Type('string')]
  143.     #[Groups([
  144.         'media:collection:post',
  145.         'media:item:get',
  146.         'media:item:put',
  147.         'contract:collection:get',
  148.         'contract:item:get',
  149.         // 'user:collection:get',
  150.         'user:item:get'
  151.     ])]
  152.     private ?string $encodingFormat null;
  153.     #[ORM\Column(type'date'nullabletrue)]
  154.     #[ApiProperty()]
  155.     #[Assert\Type(\DateTimeInterface::class)]
  156.     #[Groups([
  157.         'media:collection:post',
  158.         'media:item:get',
  159.         'media:item:put',
  160.     ])]
  161.     private ?\DateTimeInterface $uploadDate null;
  162.     #[ORM\Column(type'string'nullabletrue)]
  163.     #[ApiProperty()]
  164.     #[Groups([
  165.         'media:collection:post',
  166.         'media:item:get',
  167.         'media:item:put',
  168.         'contract:collection:get',
  169.         'contract:item:get',
  170.         // 'user:collection:get',
  171.         'user:item:get'
  172.     ])]
  173.     private ?string $genre null;
  174.     #[Assert\NotNull(groups: ['create'])]
  175.     #[Vich\UploadableField(
  176.         mapping'medias'
  177.         fileNameProperty'filePath'
  178.         size'contentSize'
  179.         mimeType'encodingFormat'
  180.         originalName'name'
  181.     )]
  182.     private ?File $file null;
  183.     #[ORM\Column(type'string'length255nullabletrue)]
  184.     #[Groups([
  185.         'media:collection:post',
  186.         'media:item:get',
  187.         'media:item:put',
  188.     ])]
  189.     private ?string $filePath null;
  190.     #[ORM\ManyToOne(
  191.         targetEntityContract::class, 
  192.         inversedBy'documents'
  193.     )]
  194.     private ?Contract $contract null;
  195.     public function __construct()
  196.     {
  197.         $this->id Uuid::uuid4();
  198.     }
  199.     public function getId(): ?UuidInterface
  200.     {
  201.         return $this->id;
  202.     }
  203.     // public function setContentUrl(?string $contentUrl): void
  204.     // {
  205.     //     $this->contentUrl = $contentUrl;
  206.     // }
  207.     public function getContentUrl(): ?string
  208.     {
  209.         return $this->genre === 'avatar' '/api/avatar/' $this->id '/api/download/' $this->id;
  210.     }
  211.     public function setContentSize(?string $contentSize): void
  212.     {
  213.         $this->contentSize $contentSize;
  214.     }
  215.     public function getContentSize(): ?string
  216.     {
  217.         return $this->contentSize;
  218.     }
  219.     public function setEncodingFormat(?string $encodingFormat): void
  220.     {
  221.         $this->encodingFormat $encodingFormat;
  222.     }
  223.     public function getEncodingFormat(): ?string
  224.     {
  225.         return $this->encodingFormat;
  226.     }
  227.     public function setUploadDate(?\DateTimeInterface $uploadDate): void
  228.     {
  229.         $this->uploadDate $uploadDate;
  230.     }
  231.     public function getUploadDate(): ?\DateTimeInterface
  232.     {
  233.         return $this->uploadDate;
  234.     }
  235.     public function setGenre(?string $genre): void
  236.     {
  237.         $this->genre $genre;
  238.     }
  239.     public function getGenre(): ?string
  240.     {
  241.         return $this->genre;
  242.     }
  243.     public function getName(): ?string
  244.     {
  245.         return $this->name;
  246.     }
  247.     public function setName(?string $name): self
  248.     {
  249.         $this->name $name;
  250.         return $this;
  251.     }
  252.     public function getContract(): ?Contract
  253.     {
  254.         return $this->contract;
  255.     }
  256.     public function setContract(?Contract $contract): self
  257.     {
  258.         $this->contract $contract;
  259.         return $this;
  260.     }
  261.     public function getFile(): ?File
  262.     {
  263.         return $this->file;
  264.     }
  265.     public function setFile(?File $file): self
  266.     {
  267.         $this->file $file;
  268.         if (null !== $file) {
  269.             $this->uploadDate = new \DateTimeImmutable();
  270.         }
  271.         return $this;
  272.     }
  273.     public function getFilePath(): ?string
  274.     {
  275.         return $this->filePath;
  276.     }
  277.     public function setFilePath(?string $filePath): self
  278.     {
  279.         $this->filePath $filePath;
  280.         return $this;
  281.     }
  282. }