src/Entity/Media.php line 95

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\Serializer\Annotation\MaxDepth;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  14. use Ramsey\Uuid\Uuid;
  15. use Ramsey\Uuid\UuidInterface;
  16. /**
  17.  * 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).
  18.  *
  19.  * @see https://schema.org/MediaObject
  20.  *
  21.  * @author Jordi Fernandes Alves <jfadev@gmail.com>
  22.  */
  23. #[ORM\Entity]
  24. #[ApiResource(
  25.     iri'Media',
  26.     itemOperations: [
  27.         'get' => [
  28.             // 'security' => "is_granted('ROLE_USER')",
  29.             'normalization_context' => [
  30.                 'groups' => 'media:item:get',
  31.                 'enable_max_depth' => true
  32.             ]
  33.         ],
  34.         'put' => [
  35.             // 'security' => "is_granted('ROLE_USER')",
  36.             'normalization_context' => [
  37.                 'groups' => 'media:item:put',
  38.                 'enable_max_depth' => true
  39.             ],
  40.             'denormalization_context' => [
  41.                 'groups' => 'media:item:put',
  42.                 'enable_max_depth' => true
  43.             ]
  44.         ],
  45.         'delete' => [
  46.             // 'security' => "is_granted('ROLE_USER')",
  47.         ]
  48.     ],
  49.     collectionOperations: [
  50.         'get' => [
  51.             // 'security' => "is_granted('ROLE_USER')",
  52.             'normalization_context' => [
  53.                 'groups' => ['media:collection:get''createdAt'],
  54.                 'enable_max_depth' => true
  55.             ]
  56.         ],
  57.         'post' => [
  58.             // 'security' => "is_granted('ROLE_USER')",
  59.             'normalization_context' => [
  60.                 'groups' => 'media:collection:post',
  61.                 'enable_max_depth' => true
  62.             ],
  63.             'denormalization_context' => [
  64.                 'groups' => 'media:collection:post',
  65.                 'enable_max_depth' => true
  66.             ],
  67.             'controller' => CreateMediaAction::class,
  68.             'deserialize' => false,
  69.             'validation_groups' => ['Default''create'],
  70.             'openapi_context' => [
  71.                 'requestBody' => [
  72.                     'content' => [
  73.                         'multipart/form-data' => [
  74.                             'schema' => [
  75.                                 'type' => 'object',
  76.                                 'properties' => [
  77.                                     'file' => [
  78.                                         'type' => 'string',
  79.                                         'format' => 'binary'
  80.                                     ]
  81.                                 ]
  82.                             ]
  83.                         ]
  84.                     ]
  85.                 ]
  86.             ],
  87.         ],
  88.     ]
  89. )]
  90. #[Vich\Uploadable]
  91. class Media
  92. {
  93.     use TimestampableEntity;
  94.     
  95.     #[ORM\Id]
  96.     #[ORM\GeneratedValue(strategy'NONE')]
  97.     #[ORM\Column(type'uuid'uniquetrue)]
  98.     #[ApiProperty(iri'https://schema.org/identifier')]
  99.     #[Groups([
  100.         'media:collection:post',
  101.         'media:item:get',
  102.         'media:item:put',
  103.         'endorsement:collection:post',
  104.         'endorsement:collection:get',
  105.         'endorsement:item:get',
  106.         'endorsement:item:put',
  107.         'contract:collection:post',
  108.         'contract:collection:get',
  109.         'contract:item:get',
  110.         'contract:item:put',
  111.         'demand_upload:collection:post',
  112.         'demand_upload:collection:get',
  113.         'demand_upload:item:get',
  114.         'demand_upload:item:put',
  115.         'insured_upload:collection:post',
  116.         'insured_upload:collection:get',
  117.         'insured_upload:item:get',
  118.         'insured_upload:item:put',
  119.         'insured_insurer_global_ccg:collection:post',
  120.         'insured_insurer_global_ccg:collection:get',
  121.         'insured_insurer_global_ccg:item:get',
  122.         'insured_insurer_global_ccg:item:put',
  123.         'user:collection:get',
  124.         'user:collection:post',
  125.         'user:item:get',
  126.         'user:item:put',
  127.     ])]
  128.     private ?UuidInterface $id null;
  129.     /**
  130.      * The name of the item.
  131.      *
  132.      * @see https://schema.org/name
  133.      */
  134.     #[ORM\Column(type'string'nullablefalse)]
  135.     #[ApiProperty(iri'https://schema.org/name')]
  136.     #[Groups([
  137.         'media:collection:post',
  138.         'media:item:get',
  139.         'media:item:put',
  140.         'endorsement:collection:get',
  141.         'endorsement:item:get',
  142.         'contract:collection:get',
  143.         'contract:item:get',
  144.         'demand_upload:collection:get',
  145.         'demand_upload:item:get',
  146.         'insured_upload:collection:get',
  147.         'insured_upload:item:get',
  148.         'insured_insurer_global_ccg:collection:get',
  149.         'insured_insurer_global_ccg:item:get',
  150.         'user:collection:get',
  151.         'user:item:get',
  152.         'insured:item:get',
  153.         'policy:item:get',
  154.         'demand:item:get',
  155.     ])]
  156.     private ?string $name '';
  157.     /**
  158.      * Actual bytes of the media object, for example the image file or video file.
  159.      *
  160.      * @see https://schema.org/contentUrl
  161.      */
  162.     #[ApiProperty(iri'https://schema.org/contentUrl')]
  163.     #[Groups([
  164.         'media:collection:post',
  165.         'media:item:get',
  166.         'endorsement:collection:get',
  167.         'endorsement:item:get',
  168.         'contract:collection:get',
  169.         'contract:item:get',
  170.         'demand_upload:collection:get',
  171.         'demand_upload:item:get',
  172.         'insured_upload:collection:get',
  173.         'insured_upload:item:get',
  174.         'insured_insurer_global_ccg:collection:get',
  175.         'insured_insurer_global_ccg:item:get',
  176.         'user:collection:get',
  177.         'user:item:get'
  178.     ])]
  179.     private ?string $contentUrl null;
  180.     /**
  181.      * File size in (mega/kilo) bytes.
  182.      *
  183.      * @see https://schema.org/contentSize
  184.      */
  185.     #[ORM\Column(type'text'nullabletrue)]
  186.     #[ApiProperty(iri'https://schema.org/contentSize')]
  187.     #[Assert\Type('string')]
  188.     #[Groups([
  189.         'media:collection:post',
  190.         'media:item:get',
  191.         'media:item:put',
  192.         'endorsement:collection:get',
  193.         'endorsement:item:get',
  194.         'contract:collection:get',
  195.         'contract:item:get',
  196.         'demand_upload:collection:get',
  197.         'demand_upload:item:get',
  198.         'insured_upload:collection:get',
  199.         'insured_upload:item:get',
  200.         'insured_insurer_global_ccg:collection:get',
  201.         'insured_insurer_global_ccg:item:get',
  202.         'user:collection:get',
  203.         'user:item:get'
  204.     ])]
  205.     private ?string $contentSize null;
  206.     /**
  207.      * 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.
  208.      *
  209.      * @see https://schema.org/encodingFormat
  210.      */
  211.     #[ORM\Column(type'text'nullabletrue)]
  212.     #[ApiProperty(iri'https://schema.org/encodingFormat')]
  213.     #[Assert\Type('string')]
  214.     #[Groups([
  215.         'media:collection:post',
  216.         'media:item:get',
  217.         'media:item:put',
  218.         'endorsement:collection:get',
  219.         'endorsement:item:get',
  220.         'contract:collection:get',
  221.         'contract:item:get',
  222.         'demand_upload:collection:get',
  223.         'demand_upload:item:get',
  224.         'insured_upload:collection:get',
  225.         'insured_upload:item:get',
  226.         'insured_insurer_global_ccg:collection:get',
  227.         'insured_insurer_global_ccg:item:get',
  228.         'user:collection:get',
  229.         'user:item:get'
  230.     ])]
  231.     private ?string $encodingFormat null;
  232.     /**
  233.      * Date when this media object was uploaded to this site.
  234.      *
  235.      * @see https://schema.org/uploadDate
  236.      */
  237.     #[ORM\Column(type'date'nullabletrue)]
  238.     #[ApiProperty(iri'https://schema.org/uploadDate')]
  239.     #[Assert\Type(\DateTimeInterface::class)]
  240.     #[Groups([
  241.         'media:collection:post',
  242.         'media:item:get',
  243.         'media:item:put',
  244.     ])]
  245.     private ?\DateTimeInterface $uploadDate null;
  246.     /**
  247.      * Genre of the creative work, broadcast channel or group.
  248.      *
  249.      * @see https://schema.org/genre
  250.      */
  251.     #[ORM\Column(type'string'nullabletrue)]
  252.     #[ApiProperty(iri'https://schema.org/genre')]
  253.     #[Groups([
  254.         'media:collection:post',
  255.         'media:item:get',
  256.         'media:item:put',
  257.         'endorsement:collection:get',
  258.         'endorsement:item:get',
  259.         'contract:collection:get',
  260.         'contract:item:get',
  261.         'demand_upload:collection:get',
  262.         'demand_upload:item:get',
  263.         'insured_upload:collection:get',
  264.         'insured_upload:item:get',
  265.         'insured_insurer_global_ccg:collection:get',
  266.         'insured_insurer_global_ccg:item:get',
  267.         'user:collection:get',
  268.         'user:item:get'
  269.     ])]
  270.     private ?string $genre null;
  271.     #[Assert\NotNull(groups: ['create'])]
  272.     #[Vich\UploadableField(
  273.         mapping'medias'
  274.         fileNameProperty'filePath'
  275.         size'contentSize'
  276.         mimeType'encodingFormat'
  277.         originalName'name'
  278.     )]
  279.     private ?File $file null;
  280.     #[ORM\Column(type'string'length255nullabletrue)]
  281.     #[Groups([
  282.         'media:collection:post',
  283.         'media:item:get',
  284.         'media:item:put',
  285.     ])]
  286.     private ?string $filePath null;
  287.     // #[ORM\ManyToOne(targetEntity: Insured::class, inversedBy: 'uploads')]
  288.     // private $insured;
  289.     #[ORM\ManyToOne(targetEntityContract::class, inversedBy'documents')]
  290.     private ?Contract $contract null;
  291.     // #[ORM\OneToOne(mappedBy: 'upload', targetEntity: PolicyUpload::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
  292.     // private $policyUpload;
  293.     // #[ORM\OneToOne(mappedBy: 'upload', targetEntity: DemandUpload::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
  294.     // private $demandUpload;
  295.     
  296.     // #[ORM\OneToOne(mappedBy: 'upload', targetEntity: Endorsement::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
  297.     // private $endorsement;
  298.     // #[ORM\OneToOne(mappedBy: 'upload', targetEntity: InsuredUpload::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
  299.     // private $insuredUpload;
  300.     // #[ORM\OneToOne(mappedBy: 'upload', targetEntity: InsuredInsurerGlobalCcg::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
  301.     // private $insuredInsurerGlobalCcg;
  302.     public function __construct()
  303.     {
  304.         $this->id Uuid::uuid4();
  305.     }
  306.     public function getId(): ?UuidInterface
  307.     {
  308.         return $this->id;
  309.     }
  310.     // public function setContentUrl(?string $contentUrl): void
  311.     // {
  312.     //     $this->contentUrl = $contentUrl;
  313.     // }
  314.     public function getContentUrl(): ?string
  315.     {
  316.         return $this->genre === 'avatar' '/api/avatar/' $this->id '/api/download/' $this->id;
  317.     }
  318.     public function setContentSize(?string $contentSize): void
  319.     {
  320.         $this->contentSize $contentSize;
  321.     }
  322.     public function getContentSize(): ?string
  323.     {
  324.         return $this->contentSize;
  325.     }
  326.     public function setEncodingFormat(?string $encodingFormat): void
  327.     {
  328.         $this->encodingFormat $encodingFormat;
  329.     }
  330.     public function getEncodingFormat(): ?string
  331.     {
  332.         return $this->encodingFormat;
  333.     }
  334.     public function setUploadDate(?\DateTimeInterface $uploadDate): void
  335.     {
  336.         $this->uploadDate $uploadDate;
  337.     }
  338.     public function getUploadDate(): ?\DateTimeInterface
  339.     {
  340.         return $this->uploadDate;
  341.     }
  342.     public function setGenre(?string $genre): void
  343.     {
  344.         $this->genre $genre;
  345.     }
  346.     public function getGenre(): ?string
  347.     {
  348.         return $this->genre;
  349.     }
  350.     // public function getInsured(): ?Insured
  351.     // {
  352.     //     return $this->insured;
  353.     // }
  354.     // public function setInsured(?Insured $insured): self
  355.     // {
  356.     //     $this->insured = $insured;
  357.     //     return $this;
  358.     // }
  359.     // public function getDemand(): ?Demand
  360.     // {
  361.     //     return $this->demand;
  362.     // }
  363.     // public function setDemand(?Demand $demand): self
  364.     // {
  365.     //     $this->demand = $demand;
  366.     //     return $this;
  367.     // }
  368.     // public function getPolicy(): ?Policy
  369.     // {
  370.     //     return $this->policy;
  371.     // }
  372.     // public function setPolicy(?Policy $policy): self
  373.     // {
  374.     //     $this->policy = $policy;
  375.     //     return $this;
  376.     // }
  377.     public function getName(): ?string
  378.     {
  379.         return $this->name;
  380.     }
  381.     public function setName(?string $name): self
  382.     {
  383.         $this->name $name;
  384.         return $this;
  385.     }
  386.     // public function getEndorsement(): ?Endorsement
  387.     // {
  388.     //     return $this->endorsement;
  389.     // }
  390.     // public function setEndorsement(?Endorsement $endorsement): self
  391.     // {
  392.     //     // unset the owning side of the relation if necessary
  393.     //     if (null === $endorsement && null !== $this->endorsement) {
  394.     //         $this->endorsement->setUpload(null);
  395.     //     }
  396.     //     // set the owning side of the relation if necessary
  397.     //     if (null !== $endorsement && $endorsement->getUpload() !== $this) {
  398.     //         $endorsement->setUpload($this);
  399.     //     }
  400.     //     $this->endorsement = $endorsement;
  401.     //     return $this;
  402.     // }
  403.     // public function getPolicyUpload(): ?PolicyUpload
  404.     // {
  405.     //     return $this->policyUpload;
  406.     // }
  407.     // public function setPolicyUpload(?PolicyUpload $policyUpload): self
  408.     // {
  409.     //     // unset the owning side of the relation if necessary
  410.     //     if (null === $policyUpload && null !== $this->policyUpload) {
  411.     //         $this->policyUpload->setUpload(null);
  412.     //     }
  413.     //     // set the owning side of the relation if necessary
  414.     //     if (null !== $policyUpload && $policyUpload->getUpload() !== $this) {
  415.     //         $policyUpload->setUpload($this);
  416.     //     }
  417.     //     $this->policyUpload = $policyUpload;
  418.     //     return $this;
  419.     // }
  420.     // public function getDemandUpload(): ?DemandUpload
  421.     // {
  422.     //     return $this->demandUpload;
  423.     // }
  424.     // public function setDemandUpload(?DemandUpload $demandUpload): self
  425.     // {
  426.     //     // unset the owning side of the relation if necessary
  427.     //     if (null === $demandUpload && null !== $this->demandUpload) {
  428.     //         $this->PolicyUpload->setUpload(null);
  429.     //     }
  430.     //     // set the owning side of the relation if necessary
  431.     //     if (null !== $demandUpload && $demandUpload->getUpload() !== $this) {
  432.     //         $demandUpload->setUpload($this);
  433.     //     }
  434.     //     $this->demandUpload = $demandUpload;
  435.     //     return $this;
  436.     // }
  437.     // public function getInsuredUpload(): ?InsuredUpload
  438.     // {
  439.     //     return $this->insuredUpload;
  440.     // }
  441.     // public function setInsuredUpload(?InsuredUpload $insuredUpload): self
  442.     // {
  443.     //     // unset the owning side of the relation if necessary
  444.     //     if (null === $insuredUpload && null !== $this->insuredUpload) {
  445.     //         $this->insuredUpload->setUpload(null);
  446.     //     }
  447.     //     // set the owning side of the relation if necessary
  448.     //     if (null !== $insuredUpload && $insuredUpload->getUpload() !== $this) {
  449.     //         $insuredUpload->setUpload($this);
  450.     //     }
  451.     //     $this->insuredUpload = $insuredUpload;
  452.     //     return $this;
  453.     // }
  454.     public function getContract(): ?Contract
  455.     {
  456.         return $this->contract;
  457.     }
  458.     public function setContract(?Contract $contract): self
  459.     {
  460.         $this->contract $contract;
  461.         return $this;
  462.     }
  463.     public function getFile(): ?File
  464.     {
  465.         return $this->file;
  466.     }
  467.     public function setFile(?File $file): self
  468.     {
  469.         $this->file $file;
  470.         if (null !== $file) {
  471.             $this->uploadDate = new \DateTimeImmutable();
  472.         }
  473.         return $this;
  474.     }
  475.     public function getFilePath(): ?string
  476.     {
  477.         return $this->filePath;
  478.     }
  479.     public function setFilePath(?string $filePath): self
  480.     {
  481.         $this->filePath $filePath;
  482.         return $this;
  483.     }
  484. }